Setting 'buildNumber' and 'versionNumber' while building APK from gradle - android

I'm building APKs from command line as follows
./gradlew assembleProductionRelease
Are there any configuration params which will allow to add buildNumber and versionNumber to APK file name automatically? By default APK file will be named as app-production-release.

You can try/adapt this code (add it to your build.gradle):
applicationVariants.all { variant ->
variant.outputs.all { output ->
def sep = '_'
def version = variant.versionName
def build = variant.versionCode
outputFileName = "${rootProject.name}${sep}" +
"${variant.buildType.name}${sep}" +
"${version}${sep}" +
"build${sep}${build}.apk"
}
}

Related

How do I append the build.gradle "versionName" to the release build APK file

In my app level build.gradle file I have the version name, such as
versionName "1.03"
When I build a signed release .apk, in Android Studio, the output file is named app-release.apk
and is stored in the release directory. How can I update the Gradle build so that the output file is named
my_application_1.03.apk
Thanks
If you want to change your apk file name at release build you can do like as follows.
For Example: ( app/build.gradle )
android {
applicationVariants.all { variant ->
if (variant.buildType.name.equals("release")) {
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
def apk_name = "my_application"
def versionName = defaultConfig.versionName
//you can also add version code and date if you like
//def applicationId = defaultConfig.applicationId
//def versionCode = defaultConfig.versionCode
//def date = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
def newName = "${apk_name}_${versionName}.apk"
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
}
}

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

I was using the following code in my gradle script to rename the apks generated with AndroidStudio:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, defaultConfig.versionCode + "_" + output.outputFile.name)
}
}
So it was generating apks with names like: 345-app-release.apk, where 345 is the versionCode.
But after updating to AndroidStudio 3.0 it returns the following error:
Cannot set the value of read-only property 'outputFile' for
ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug,
filters=[]}} of type
com.android.build.gradle.internal.api.ApkVariantOutputImpl.
How can I achieve a similar renaming with the new build tools.
Use output.outputFileName instead of output.outputFile
2019 - Simple Solution for Gradle 3.0+** and 3.1.0
To change the name of the APK in Android
android { //add inside the android {}
......
applicationVariants.all { variant ->
variant.outputs.all {
def flavor = variant.name
def versionName = variant.versionName
outputFileName = "prefix_${flavor}_${versionName}.apk"
}
}
}
prefix_release_1.0.1.apk
Try this code :
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def name = "myapp_v${variant.versionName}(${variant.versionCode}).apk"
output.outputFileName = name
}
}
}
In or After gradle 3.1.0
try below code
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File(
"release_build", // here you can change the name
output.outputFile.name)
}
}
In my case I resolved it by just refusing to update to Gradle 4.4 (in my case).So when Studio asks (when you open your project for the first time) you to update Gradle to support instant run,etc just simply refuse and you should be fine.
What worked for me was to
1. change each to "all"
2. change output.outputFile to "outputFileName"
3. Run $./gradlew clean build in terminal
For example, if your artifacts.gradle settings were this:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def finalVersionCode = 10000 + versionCode
output.versionCodeOverride = finalVersionCode
output.outputFile = new File(
output.outputFile.parent, output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}
}
Then you would want to change it to this:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def finalVersionCode = 10000 + versionCode
output.versionCodeOverride = finalVersionCode
outputFileName = new File(
output.outputFile.parent,
outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}
}
change your code into:-
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "${variant.applicationId}-${variant.versionName}.apk")
}
}

Android. Gradle. How to get flavor property in task

I have an Android Gradle project with several flavors (different styles of the same app). I have a task that downloads some external file and puts it into assets folder. I need to specify an array of strings as a part of each flavor that (urls or filenames) that will be downloaded during the build of a specific flavor.
```
applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
variant.outputs.all {
def currentProductFlavor = variant.productFlavors.name.get(0)
def apkName = rootProject.ext.app.appName + currentProductFlavor + "_" + rootProject.ext.app.appVersionName + ".apk"
println("======apkName:" + apkName)
outputFileName = apkName
}
}
}
```

How to get ABI name in Gradle outputs iterator

I'm composing .apk filename using current app version and flavor name. I'd like to add current ABI split name as well, but only if it's a universal apk.
My relevant build.gradle sections:
buildTypes {
release {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = .... // some code to parse flavor & determine an appropriate string from it
output.outputFile = new File(output.outputFile.parent, "app_" + flavor + "_0" + variant.versionCode + ".apk")
}
}
}
}
productFlavors {
deploy {
splits {
abi {
enable true
reset()
include 'armeabi-v7a' //select ABIs to build APKs for
universalApk true //generate an additional APK that contains all the ABIs
}
}
}
}
Currently this config generates two .apks, but they both have the same file name as I don't know how to get the ABI name, so the one generated later overwrites the one generated before.
So, what is the equivalent variant.productFlavors.get(0) for current ABI split?
That's very strange as flavor and ABI-name is automatically added to build name (if you make corresponding assemble)
can you try completely remove your custom made naming
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = .... // some code to parse flavor & determine an appropriate string from it
output.outputFile = new File(output.outputFile.parent, "app_" + flavor + "_0" + variant.versionCode + ".apk")
}
}
and instead of that try to add to defaultConfig this line
archivesBaseName = "app_${versionCode}"
If this is will not resolve you issues you can try to get abi from output
output.getFilter(com.android.build.OutputFile.ABI)
The equivalent is output.getFilter(com.android.build.OutputFile.ABI).
Note that, starting with Android Studio 3.0, you should use outputFileName and variant.outputs.all instead:
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "app_" + output.getFilter(com.android.build.OutputFile.ABI) + "_0" + variant.versionCode + ".apk"
}
}

Android - Only execute Gradle task on release build variant

I am trying to configure my build.gradle file to only execute a gradle task when the release build variant is selected. So far, my task always gets executed, whether it is in my debug or release build types or signing configs. I have tried adding my task inside an applicationsVariants block and check if it is the release variant, but it just loops through all variants.
applicationVariants.all { variant ->
variant.outputs.each { output ->
...
}
}
I know that both the debug and release tasks always run for whichever build variant you choose. Is it possible to execute some code only when creating a build for release? If so, where does that code go? Thanks!
I have read through every Stackoverflow question on this, but none of the answers really did I am wanting. My end goal is when I select the "release" build variant for a Play Store build, a message is posted to our server. I do not want this to happen when just debugging.
Add doFirst or doLast for the build type you are interested in.
android.applicationVariants.all { variant ->
if ( variant.buildType.name == "release"){
variant.assemble.doLast { // Can also use doFirst here to run at the start.
logger.lifecycle("we have successfully built $v.name and can post a messaage to remote server")
}
}
}
I had to do something like this to check build version:
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each {output ->
def project = "AppName"
def separator = "_"
/*def flavor = variant.productFlavors[0].name*/
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date();
def formattedDate = date.format('yyyyMMdd_HHmm')
if (variant.buildType.name == "release"){
def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + formattedDate + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
if (variant.buildType.name == "debug"){
def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
}
} }

Categories

Resources