change apk filename in gradle - android

I am trying to use android build tools "com.android.tools.build:gradle:3.0.0-alpha4" in my project. In my build script I rename the output apk which worked fine in the past but does not seem to be supported any more.
applicationVariants.all { variant ->
def filename = "foo-${variant.baseName}-${variant.versionName}-(${android.defaultConfig.versionCode}).apk"
variant.outputs.all { output ->
output.outputFile = new File(
output.outputFile.parent,
filename
)
}
}
Now the propery I am trying to change became immutable:
Error: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=stageDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
Is there a new or an alternate way how to do this?

Change each -> to all
Change output.outputFile -> to outputFileName
before:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def finalVersionCode =v10000 + versionCode
output.versionCodeOverride = finalVersionCode
output.outputFile = new File(
output.outputFile.parent, output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}
}
after:
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"))
}
}

Add e.g. versionName to apk by adding
setProperty("archivesBaseName", archivesBaseName + "-" + versionName)
in defaultConfig closure.

Related

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")
}
}

Generate apk name as package name in Android Studio

Android studio will generate default apk name as app-(release|debug).apk.
How to generate apk file name same as package name of the app like com.example-debug.apk.
You can do it without using another tasks, setting the archivesBaseName.
For example:
defaultConfig {
....
project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName);
}
Output:
MyName-1.0.12-release.apk
In your case:
project.ext.set("archivesBaseName", "com.example" );
Try putting this in your module's build.gradle
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
def appId = android.defaultConfig.applicationId
def fileName = appId + "-" variant.buildType.name +".apk"
output.outputFile = new File(file.parent, fileName)
}
}
you can see this link.
or Illogical option to rename your release|debug.apk with name what you want in file browser.
this code may be useful for you:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def formattedDate = new Date().format('yyyyMMddHHmmss')
def newName = output.outputFile.name
newName = newName.replace("app-", "$rootProject.ext.appName-") //"MyAppName" -> I set my app variables in the root project
newName = newName.replace("-release", "-release" + formattedDate)
//noinspection GroovyAssignabilityCheck
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
debug {
}
}
enjoy your code:)
Create a file named customname.gradle in the top level directory of the project.Put this code into it.
android.applicationVariants.all { variant ->;
def appName
//Check if an applicationName property is supplied; if not use the name of the parent project.
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
variant.outputs.each { output ->;
def newApkName
//If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
if (output.zipAlign) {
newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
} else {
newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
}
output.outputFile = new File(output.outputFile.parent, newApkName)
}}
Then in your app module's gradle add this code
apply from: "../customname.gradle"
This may help you. This code will create app name like applicationId-release.apk or applicationId-debug.apk in which applicationId can be your package name.
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName = output.outputFile.name
newName = newName.replace("app-", applicationId)
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}

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

Get application version from Manifest in Android Studio build.gradle

Is there a way to access the current application version during a build using Android Studio? I'm trying to include the build version string in the filename of the apk.
I'm using the following to change the filename based on the date for a nightly build, but would like to have another flavor for a release build that includes the version name.
productFlavors {
nightly {
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
def date = new Date();
def formattedDate = date.format('yyyy-MM-dd')
output.outputFile = new File(
file.parent,
"App-nightly-" + formattedDate + ".apk"
)
}
}
}
}
Via https://stackoverflow.com/a/19406109/1139908, if you are not defining your version numbers in Gradle, you can access them using the Manifest Parser:
import com.android.builder.core.DefaultManifestParser // At the top of build.gradle
def manifestParser = new com.android.builder.core.DefaultManifestParser()
String versionName = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
Also worth noting is that (per https://stackoverflow.com/a/22126638/1139908) using applicationVariants.all can have unexpected behavior for your default debug build. In my final solution, my buildTypes section looks like this:
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def String fileName;
if(variant.name == android.buildTypes.release.name) {
def manifestParser = new DefaultManifestParser()
def String versionName = manifestParser.getVersionName((File) android.sourceSets.main.manifest.srcFile)
fileName = "App-release-v${versionName}.apk"
} else { //etc }
def File file = output.outputFile
output.outputFile = new File(
file.parent,
fileName
)
}
}
release {
//etc
}
}

Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl

After updating to AS 1.0 RC 1 and plugin 0.14.4 I am having problems with the renaming part of my build.gradle:
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
throws now:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated#67e7625f.
and also I cannot jump to the class ApplicationVariantImpl to look how the property might have been renamed.
Anyone knows workarounds for this?
try this
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
More comprehensively:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
This can occur for few reasons:
1.) First as was said before by #Khalidov, try
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = ...
}
}
2.) Second try update all other plugins.
For example I got this problem for Spoon, that resolved by update Spoon up to:
classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.14.1'
Or where there's only one variant:
def apk = outputs[0].outputFile
Instead of
def apk = variant.outputFile
Make sure you run the latest gradle version (not the plugin, gradle it self).
Check your gradle-wrapper.properties. Are you running gradle 2.1?
More info on compatibility: http://tools.android.com/tech-docs/new-build-system/version-compatibility
I managed to solve as follows:
old:
buildTypes {
libertação {
runProguard false // esta linha tem que ser mudado
proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ), 'proguard-rules.pro'
}
}
new:
buildTypes {
libertação {
minifyEnabled false // nova versão
proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ), 'proguard-rules.pro'
}
}
edited in file buil.gradle of your project as described in this post by ruan65
Error:(26, 0) Gradle DSL method not found: 'runProguard()'
and after edit too this line:
applicationVariants . all { variant ->
variant . outputs . each { output ->
def file = output . outputFile
output . outputFile = new File ( file . parent , file . name . replace ( ".apk" , "-" + defaultConfig . versionName + ".apk" ))
}
}
as it was said up there.
That settled me!

Categories

Resources