Get application version from Manifest in Android Studio build.gradle - android

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

Related

flutter set version name in apk filename

I tried many options in android/app/build.gradle
but none seems to be working.
for example I tried with below code in default config block
archivesBaseName = "AppName-${versionName}-${new Date().format('yyMMdd')}"
all example seems to be not related to flutter.
You can try this in your gradle file
buildTypes {
release {
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.all {
def appName = "your_app_name_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def newName
if (buildType == 'debug'){
newName = "app-${variant.getFlavorName()}-debug.apk"
} else {
newName = "${appName}${defaultConfig.versionName}_${variant.getFlavorName()}.apk"
}
outputFileName = newName
}
}
}
}
This code works with regular builds and flavors.
Add it in the build.gradle (the source is https://stackoverflow.com/a/58392819/7198006)
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def builtType = variant.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def flavor = variant.flavorName
outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
}
}
You can open pubspec.yaml and search on version and edit it
Version: 1.0.0+1

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

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

Build release apk with customize name format in Android Studio

I want the .apk to be built with the following name format (with timestamp).
How can I set it?
format : {app_name}{yyyymmddhis}.apk
Now, it is fixed with the name {app_name}-{release}.apk
in the build.gradle file, you should change/add buildTypes like this:
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
variant.outputFile = new File(
file.parent,
file.name.replace("-release", "-" + formattedDate)
)
}
}
}
====== EDIT with Android Studio 1.0 ======
If you are using Android Studio 1.0, you will get an error like this:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated#67e7625f.
You should change the build.Types part to this:
buildTypes {
release {
signingConfig signingConfigs.releaseConfig
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("-release", "-" + formattedDate)
)
}
}
}
}
As a side note, I would really like to know where people get the objects structures of gradle build objects.
To construct this I've used some trial and errors, a bit of Googling, and this (which is out of date)
android {
...
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = "default";
if (variant.productFlavors.size() > 0)
flavor = variant.productFlavors.get(0);
def initials = "DefaultFlavor";
if (flavor.name == "flavor1")
initials = "F1";
else if (flavor.name == "flavor2")
initials = "F2";
def build = "Debug";
if (variant.buildType.name == "release")
build = "Release"
def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
output.outputFile = new File(output.outputFile.parent, finalName)
}
}
}
...
}
Ok, So as a side note, if you would like to determine the names of your libs project here is a quick script, make sure you put it in each of your library gradle build files.
android {
...
defaultConfig {
minSdkVersion 10
targetSdkVersion 16
versionCode 1
versionName "1.0.4"
project.version = versionName
}
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "${archivesBaseName}-v${version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
...
}
You should change something like this. Doing dynamically.
project.archivesBaseName = {app_name}{yyyymmddhis};
But I have read that this is going to be deprecated.
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Deprecated dynamic property: "archivesBaseName" on "root project 'myapp'", value: "AnotherName".

Gradle: applicationVariants.all skips one variant

I'm using Gradle to compile my Android project:
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
def fileName = file.name
fileName = fileName.replace(".apk", "-renamed.apk")
variant.outputFile = new File(file.parent, fileName)
}
}
}
Not all output files are renamed, it always skips 1 file. Why?
myapp-debug-unaligned-renamed.apk <-renamed, OK!
myapp-release.apk <-NOT renamed, WRONG!
myapp-release-unaligned-renamed.apk <-renamed, OK!
I solved using this code:
buildTypes {
release {
signingConfig signingConfigs.release
}
applicationVariants.all { variant ->
def apk = variant.packageApplication.outputFile;
def newName = apk.name.replace(".apk", "-renamed.apk");
variant.packageApplication.outputFile = new File(apk.parentFile, newName);
if (variant.zipAlign) {
variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
}
}
}
The block applicationVariants.all {...} is now outside the release {...} block.
I think variant.zipAlign.outputFile makes the difference.
There should be 3 output APK files when using your build.gradle configuration: debug unsigned unaligned, release signed aligned and release signed unaligned. There are two variables for applicationVariant to deal with output files: outputFile and packageApplication.outputFile, the former is used for zipalign and the later is used in general case.
So the proper way to rename all the files will be like this:
android.applicationVariants.all { variant ->
if (variant.zipAlign) {
def oldFile = variant.outputFile;
def newFile = oldFile.name.replace(".apk", "-renamed.apk")
variant.outputFile = new File(oldFile.parent, newFile)
}
def oldFile = variant.packageApplication.outputFile;
def newFile = oldFile.name.replace(".apk", "-renamed.apk")
variant.packageApplication.outputFile = new File(oldFile.parent, newFile)
}
I simplified it by removing one of your lines but essentially you need to change it like so:
android {
buildTypes {
...
}
applicationVariants.all { variant ->
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-renamed".apk")
variant.outputFile = new File(file.parent, fileName)
}
}

Categories

Resources