Skip to content

Instantly share code, notes, and snippets.

@twocity
Last active December 11, 2021 09:13
Show Gist options
  • Save twocity/8121849 to your computer and use it in GitHub Desktop.
Save twocity/8121849 to your computer and use it in GitHub Desktop.
android gradle change output apk file name.
android.applicationVariants.all { variant ->
println "*********" + variant.description + "**********";
def variants = variant.baseName.split("-");
def apkName = "ricebook-";
apkName += variants[0];
apkName += "-v" + android.defaultConfig.versionName;
if (!variant.zipAlign) {
apkName += "-unaligned";
}
if (variant.buildType.name == "release") {
apkName += "-RELEASE.apk";
} else {
apkName += "-SNAPSHOT.apk";
}
println "*********" + "$project.buildDir/apk/" + apkName + "**********";
variant.outputFile = file("$project.buildDir/apk/" + apkName)
}
@nelo686
Copy link

nelo686 commented Oct 27, 2015

Where do you place this piece of code inside build.grade? Because I tried inside buildTypes and inside each build type (debug, release, ...) and always there is something that AS doesn't recognize.

If I place it inside a build type, such as debug, AS doesn't recognize applicationVariants and defaultConfig, but if I place it inside buldTypes, AS doesn't recognize any attribute from variants (like description, baseName, zipAlign, buildType and outputFile) and defaultConfig.

Can you help me?
Thx.

@SMARTsky-soft
Copy link

android {

//....

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
    world {
        versionName "world"
    }
    local {
        versionName "local"
    }
}

applicationVariants.all { variant ->
    println("Iterating variant: " + variant.getName());

    if (variant.getName().contains("world")) {
        variant.buildConfigField "String", "URL_API_PATH", '"http://..."'
    } else {
        variant.buildConfigField "String", "URL_API_PATH", '"http://192.168...."'
    }
    variant.mergedFlavor.versionName = android.defaultConfig.versionName + "-" + variant.mergedFlavor.versionName;

    variant.outputs.each { output ->
        def apkName = "somename_" + android.defaultConfig.versionCode;

        if (variant.buildType.name.equals("release"))
            apkName += "_" + variant.buildType.name;

        apkName += ".apk";
        println "*********" + "$project.buildDir/apk/" + apkName + "**********";
        output.outputFile = file("$project.buildDir/apk/" + apkName)
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment