6

I've the following app build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "io.gresse.hugo.anecdote"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 12
        versionName "1.0.0"
    }

    buildTypes {
        release {
            archivesBaseName = "anecdote-" + defaultConfig.versionName
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
        debug {
            archivesBaseName = "anecdote-DEBUGDEBUGDEBUG-"
        }
    }
}

When I execute ./gradlew assembleRelease assembleDebug

The output .apk are:
- anecdote-DEBUGDEBUGDEBUG-debug-unaligned.apk
- anecdote-DEBUGDEBUGDEBUG-debug.apk
- anecdote-DEBUGDEBUGDEBUG-release-unaligned.apk
- anecdote-DEBUGDEBUGDEBUG-release.apk

What I wanted:
- anecdote-DEBUGDEBUGDEBUG-debug-unaligned.apk
- anecdote-DEBUGDEBUGDEBUG-debug.apk
- anecdote-1.0.0-release-unaligned.apk
- anecdote-1.0.0-release.apk

Is there any way to apply the archiveBaseName to a specific build types or is it a bug?

Thanks,

Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119

2 Answers2

4

As you may notice, this question is a mess around SO.

Related answer here and here.

Here is what worked for me. I wanted to keep the simple archiveBaseName but it seems deprecated and that it apply to all buildTypes.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "io.gresse.hugo.anecdote"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 12
        versionName "1.0.0"
    }

    project.ext.set("archivesBaseName", "Anecdote");

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if(variant.buildType.name == "release"){
                output.outputFile = new File(
                        output.outputFile.parent,
                        output.outputFile.name.replace(".apk", "-"+variant.versionName + ".apk"))
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Community
  • 1
  • 1
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
  • If anyone wondering how to rename the APK using kotlin gradle, you can see the solutions here: https://stackoverflow.com/a/58035977/3763032 – mochadwi Dec 05 '19 at 09:17
0

I know this is very old but multiple archivesBaseName still seem to overwrite each other even in the latest Gradle version (7.5).

As of Gradle 3.0 the output property which needs to be changed is now called outputFileName and is no longer an absolute path:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        if (variant.buildType.name == "release") {
            output.outputFileName = 'your-release-name.apk'
        }
    }
}
chris
  • 3,019
  • 23
  • 21