Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming apk with gradle -> getting same apk twice

I have an app with different flavors - each flavor has two buildTypes.

After renaming some lines in my manifest i also rename the apk. Everything works fine - I just wonder why I get the same apks twice? Once not renamed and once renamed...

Short example of the same app with different Names:

  • "myApp-flavor-buildType.apk" (not renamed)
  • "myApp-appName-buildType-version.apk" (renamed apk)

Here is the code of my build.gradle file:

// *** OVERRIDE data in MANIFEST ***
android.applicationVariants.each { variant ->
    variant.processManifest.doLast {
        overrideDataInManifest(variant)
    }
}

def overrideMapsKey(buildVariant){
    def appName = getAppName(buildVariant)

    // override line ... this is not necessary to this question

    renameAPK(buildVariant, appName)
}

// *** RENAME APK ***
def renameAPK(buildVariant, appName){
    def apk = buildVariant.packageApplication.outputFile;
    def newName = "";

    // get data for apk renaming
    def versionName = android.defaultConfig.versionName 
    def versionNameSuffix = buildVariant.buildType.versionNameSuffix
    if(versionNameSuffix.toString().equals("null"))
        versionNameSuffix = ""
    def buildTypeOfApp= buildVariant.buildType.name 

    if (buildVariant.zipAlign) {
        newName = "etscanner-" + appName + "-" + buildTypeOfApp.toUpperCase() + "-v" + versionName + versionNameSuffix + ".apk"
    }else{
        newName = "etscanner-" + appName + "-" + buildTypeOfApp.toUpperCase() + "-v" + versionName + versionNameSuffix + "-ALIGNED" + ".apk"
    }
    buildVariant.packageApplication.outputFile = new File(apk.parentFile, newName);
}

Just want to know whats going on and if the same task is possible without getting two apks.

like image 938
owe Avatar asked Aug 29 '13 09:08

owe


1 Answers

There will always be two APK files for variant with zipAlign option set to true. At first gradle builds the original APK file, then zipalign it and produces optimised version without deleting the original one.

buildVariant.packageApplication.outputFile is the intermediate product of the building process and in my observation that one is always unaligned APK file.

buildVariant.outputFile is the final output where zipAlign option is applied.

On the other hand you might want to correct the build.gradile file, it actually renames the unaligned version of APK file to "ALIGNED" version and leaves the zipalign APK file untouched. Here's my modified version:

if (buildVariant.zipAlign) {
    newName = "etscanner-" + appName + "-" + buildTypeOfApp.toUpperCase() + "-v" + versionName + versionNameSuffix + "-ALIGNED.apk"
    buildVariant.outputFile = new File(apk.parentFile, newName);
}

newName = "etscanner-" + appName + "-" + buildTypeOfApp.toUpperCase() + "-v" + versionName + versionNameSuffix + "-UNALIGNED" + ".apk"
buildVariant.packageApplication.outputFile = new File(apk.parentFile, newName);
like image 193
shakalaca Avatar answered Sep 21 '22 05:09

shakalaca