owe
owe

Reputation: 4930

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:

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.

Upvotes: 5

Views: 5251

Answers (4)

M.Hefny
M.Hefny

Reputation: 2745

 buildTypes {
        release {
            debuggable false
            jniDebuggable false
            signingConfig signingConfigs.config
            minifyEnabled false
            zipAlignEnabled true

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if ((variant.buildType.name == 'release') && outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "myFileName_release_" + defaultConfig.versionName + ".apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }


        }
        debug {
            debuggable true
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled false
            zipAlignEnabled true
            signingConfig signingConfigs.config

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if ((variant.buildType.name == 'debug') && outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "myFileName_debug_" + defaultConfig.versionName + ".apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

Upvotes: 1

Ray Hunter
Ray Hunter

Reputation: 15547

For anyone searching for renaming a android library file (aar), here is what I came up with for gradle 2.+ versions:

android.libraryVariants.all { variant ->
    if (variant.buildType.name == 'release') {
        def oldFile = variant.outputs.get(0).getOutputFile()
        if (oldFile.name.contains("release")) {
            def newPath = oldFile.name.replace("release.aar", version.toString() + ".aar")
            variant.outputs.get(0).
                    setOutputFile(new File(oldFile.parentFile.toString(), newPath.toString()))
        }
    }
}

Let me know if you come up with a better way to do it.

Upvotes: 2

Marcelo Benites
Marcelo Benites

Reputation: 873

In case you are using Android Gradle Plugin 0.13 or above the interface has changed a little bit:

android.applicationVariants.all { variant ->

    def name
    variant.outputs.each { output ->

        def apkDirectory = variant.packageApplication.outputFile.parentFile

        if (output.zipAlign) {
            name = "yourAppName.apk"
            output.outputFile = new File(apkDirectory, name);
        }

        name = "yourAppName-UNALIGNED.apk"
        variant.packageApplication.outputFile = new File(apkDirectory, name);
    }    
}

Upvotes: 0

shakalaca
shakalaca

Reputation: 1712

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);

Upvotes: 11

Related Questions