Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify android assembled library file name on gradle 5.1

Gradle changed it's behavior when I updated from 5.0 to 5.1.

Lets assume that we have an android project with single module named library. On Gradle 5.0 (and previous versions) when I executed ./gradlew assembleRelease or ./gradlew assembleDebug generated output was library-release.aar or library-debug.aar respectively.

After I updated to Gradle 5.1 (I also tried 5.1.1) it generates only library.aar for any build type without any build-type classifier in output file name.

So my question is: how can I force Gradle 5.1 to set correct output file naming for different build types like it was before? Below is my library module's build.gradle.kts, but I doesn't think that there is something wrong with it:

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    id("com.android.library")
    id("kotlin-android")
    id("maven-publish")
}

val libraryVersion = "1.5.0"

android {
    compileSdkVersion(28)

    defaultConfig {
        minSdkVersion(16)
        targetSdkVersion(28)
        versionCode = 1
        versionName = libraryVersion
    }

    lintOptions {
        isAbortOnError = false
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        }
    }
}

dependencies {
    implementation(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "libs")))
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
}

UPDATE:

If I add a following code to build.gradle.kts:

afterEvaluate {
    android.libraryVariants.forEach { libraryVariant ->
        libraryVariant.outputs.forEach { output ->
            println(output.outputFile.name)
        }
    }
}

It will print:

library-debug.aar
library-release.aar

Now it starts looking like a bug in gradle 5.1

like image 285
Andrei Vinogradov Avatar asked Jan 24 '19 11:01

Andrei Vinogradov


People also ask

Where is the .gradle file Android?

gradle file is located inside your project folder under app/build. gradle.

How many build gradle file is there in one Android project?

An Android Studio project usually has two build. gradle files, one at the project level and one at the “module” level (e.g., in the app/ directory).

What is Android library file?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.


2 Answers

So as we can see in https://github.com/gradle/gradle/issues/8328, it is a bug which is fixed in Android Gradle Plugin 3.4.

like image 72
Majkeee Avatar answered Nov 15 '22 01:11

Majkeee


libraryVariants.all { variant ->
    variant.outputs.all { output ->
        def filePrefix = "$buildDir/outputs/aar/$archivesBaseName"
        outputFileName = "${archivesBaseName}-${version}.aar"
        def fileSuffix = "aar"
        def originalFile = file("$filePrefix-${variant.buildType.name}.$fileSuffix")
        def renamedFile = "$filePrefix-${variant.buildType.name}-$version.$fileSuffix"
        tasks.named("assemble").configure {
            doLast {
                originalFile.renameTo(renamedFile)
            }
        }
    }
}

where archivesBaseName and version can defined in default block of libraries build.gradle

like image 44
vivekg Avatar answered Nov 15 '22 01:11

vivekg