Android gradle plugin 8.1.0 change apk name
Asked Answered
P

4

7

Old way of apk naming is no longer working, is there another api in AGP 8+?

Here's my kts build.config:

android {
    ...
    applicationVariants.all { variant ->
        variant.outputs
            // default type don't have outputFileName field
            .map { it as com.android.build.gradle.internal.api.ApkVariantOutputImpl } 
            .all { output ->
                output.outputFileName = "MyAwesomeName.apk"
                false
            }
    }
}

I use AGP 8.1.0 and gradle 8.2.1

Pipes answered 28/7, 2023 at 13:44 Comment(0)
P
3

Found the issue: there are more .all() methods, the right one comes from org.gradle.api.DomainObjectCollection and not from _Collections.kt

Basically to get the right behavior we need to just remove the it lambda parameter.

// wrong
applicationVariants.all { variant -> }
// right
applicationVariants.all {  }

Very tricky problem

Pipes answered 28/7, 2023 at 14:34 Comment(5)
or change all to forEachCareer
@Link182, Doesn't help for me. With Android Gradle Plugin 8.0.1 and Gradle 8.0. I just don't see the outputFileName property. Can you copy paste your whole block? Any imports at the top of the file?Cervin
@Cervin this code should work https://mcmap.net/q/337723/-how-to-access-variant-outputfilename-in-kotlinPipes
@Link182, thanks :) I ended up implementing it that way, but that cast to BaseVariantOutputImpl seems like such a hack... Really wanted to avoid using such brute methods, but there doesn't seem to be an alternative right now.Cervin
@Cervin right, I hope in the next versions of kts this api will be improvedPipes
G
4

Gradle: gradle-8.0

Gradle Type: build.gradle.kts

You can add the setProperty inside the productFlavors.

android {
....

flavorDimensions += "version"
    productFlavors {
        create("free") {
            dimension = "version"

            setProperty("archivesBaseName", "free")
        }

        create("pro") {
            dimension = "version"

            setProperty("archivesBaseName", "pro2x")
        }
    }
    
}

it will output the pro2x-pro-debug.apk (archivesBaseName+productFlavor+buildType.apk)

Gause answered 30/9, 2023 at 12:26 Comment(1)
You also could use setProperty() command outside of create lambda but inside productFlavors, to use something like this to setup this once setProperty("archivesBaseName", "App-${defaultConfig.versionName}(${defaultConfig.versionCode})")Disequilibrium
P
3

Found the issue: there are more .all() methods, the right one comes from org.gradle.api.DomainObjectCollection and not from _Collections.kt

Basically to get the right behavior we need to just remove the it lambda parameter.

// wrong
applicationVariants.all { variant -> }
// right
applicationVariants.all {  }

Very tricky problem

Pipes answered 28/7, 2023 at 14:34 Comment(5)
or change all to forEachCareer
@Link182, Doesn't help for me. With Android Gradle Plugin 8.0.1 and Gradle 8.0. I just don't see the outputFileName property. Can you copy paste your whole block? Any imports at the top of the file?Cervin
@Cervin this code should work https://mcmap.net/q/337723/-how-to-access-variant-outputfilename-in-kotlinPipes
@Link182, thanks :) I ended up implementing it that way, but that cast to BaseVariantOutputImpl seems like such a hack... Really wanted to avoid using such brute methods, but there doesn't seem to be an alternative right now.Cervin
@Cervin right, I hope in the next versions of kts this api will be improvedPipes
C
2

To change an APK name in Android gradle 8.1 kts following snippet will help

productFlavors {
    create("free") {
        dimension = "app"
        val appName = "Free App 2.0"
        manifestPlaceholders["appName"] = appName
        applicationIdSuffix = ".demo"
        versionName = "1.0.0"
        versionNameSuffix = ".3"
        versionCode = (versionName + versionNameSuffix).replace(".", "").toInt()
        val apkName = "${appName}_$versionName$versionNameSuffix($versionCode).apk"

        // change app name block below
        buildOutputs.all {
            val variantOutputImpl = this as com.android.build.gradle.internal.api.BaseVariantOutputImpl
            variantOutputImpl.outputFileName =  apkName
        }
    }
}
Cargian answered 2/2, 2024 at 3:12 Comment(1)
medium.com/@arulmani33/… added in the medium thread. Feel free to add more logic there.Cargian
F
2

Here is how to proceed with the new Variant API that is due to replace the old API in mid-2024:

androidComponents {
    onVariants { variant ->
        variant.outputs.forEach { output ->
            if (output is com.android.build.api.variant.impl.VariantOutputImpl) {
                output.outputFileName = "MyAwesomeName.apk"
            }
        }
    }
}

Keep in mind that renaming APKs like this is not supported by the Android Gradle Plugin.

Farro answered 15/4, 2024 at 10:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.