Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename output file in the new Android Plugin 3.0.0-alpha1

I was using this in the previous gradle plugin and it was working fine.

applicationVariants.all { v ->
v.outputs.each { output ->
    output.outputFile = new File(
            output.outputFile.parent,
            output.outputFile.name.replace("app-release.apk", "companyName-app-v${variant.versionName}.apk"))
}}

With the recent update to 3.0.0-alpha1 it shows error.

I read the link https://developer.android.com/studio/preview/features/new-android-plugin-migration.html but was unable to find the exact source of error.

Is there a fix to this I am missing?

like image 995
Chaks Avatar asked May 18 '17 05:05

Chaks


2 Answers

To change the APK name, you can change it to:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "companyName-app-v${variant.versionName}.apk"
    }
}

If you use each() to iterate through the variant objects, you need to start using all(). That's because each() iterates through only the objects that already exist during configuration time — but those object don't exist at configuration time with the new model. However, all() adapts to the new model by picking up object as they are added during execution.

Source

like image 157
James McCracken Avatar answered Nov 05 '22 07:11

James McCracken


I'm on Canary 3, and this is working for me. But see the kludge for the ABI name? I couldn't figure out where to get the ABI in the new structure, so extracted it from the default name (sucks). If anyone knows a better way to get the ABI name when using splits (to change the versionCode per ABI), I'd love to hear. Maybe its time to post a separate question. Anyway, this works on prod APK builds.

def ext = rootProject.extensions.getByName("ext")
ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'x86': 8]

android.applicationVariants.all { variant ->
    if (variant.buildType.name != "debug") {
        variant.outputs.all { vOutput ->
            // there has got to be a better way to get the ABI name than this kludge
            def nameTokens = vOutput.name.split("-");
            def abiName = nameTokens[1]
            abiName = nameTokens[2].contains("v7a") ? abiName + "-v7a" : abiName
            def versionNumber = ext.versionCodes.get(abiName) * 1000 + variant.versionCode
            def newApkName = "${getAppName()}-${variant.flavorName}-${variant.versionName}-${versionNumber}.apk"
            outputFileName = newApkName
        }
    }
}

Resulting file names produced from "Build APKs" for versionCode 1, versionName "1.0.0" with armeabi-v7a and x86 ABIs respectively:

AppName-Prod-1.0.0-2001.apk
AppName-Prod-1.0.0-8001.apk

I couldn't find any doc yet on ABI names in the new setup. The old way to get them is obviously broken :-)

like image 39
SKOlson Avatar answered Nov 05 '22 06:11

SKOlson