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)
}
}
}
Related
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
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
}
}
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".
I have an Android project which uses Gradle for build process. Now I want to add two extra build types staging and production, so my build.gradle contains:
android {
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
staging {
signingConfig signingConfigs.staging
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
production {
signingConfig signingConfigs.production
}
}
}
and my appndVersionNameVersionCode looks like:
def appendVersionNameVersionCode(variant, defaultConfig) {
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}
def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
If I execute task assembleStaging then I get proper name of my apk, but when I execute assembleProduction then I get changed names of my apk (like in staging case). For example:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?
As CommonsWare wrote in his comment, you should call appendVersionNameVersionCode only for staging variants. You can easily do that, just slightly modify your appendVersionNameVersionCode method, for example:
def appendVersionNameVersionCode(variant, defaultConfig) {
//check if staging variant
if(variant.name == android.buildTypes.staging.name){
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}
def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
}
Lecho's solution doesn't work for Android Gradle Plugin 0.14.3+ because of removal of deprecated APIS:
http://tools.android.com/tech-docs/new-build-system
Almost 1.0: removed deprecated properties/methods
...
Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest (use the variant output)
The following works for me:
def appendVersionNameVersionCode(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.outputFile = new File(file.parent, fileName)
}
def file = output.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
For newer android studio Gradle
AppName is your app name you want so replace it
variantName will be default Selected variant or flavor
Date will Today's date, So need to do any changes just paste it
applicationVariants.all { variant ->
variant.outputs.all {
def variantName = variant.name
def versionName = variant.versionName
def formattedDate = new Date().format('dd-MM-YYYY')
outputFileName = "AppName_${variantName}_D_${formattedDate}_V_${versionName}.apk"
}
}
Output:
AppName_release_D_26-04-2021_V_1.2.apk
If you use Kotlin DSL, then here is how you can change the APK name:
applicationVariants.all {
outputs.all {
this as com.android.build.gradle.internal.api.ApkVariantOutputImpl
val buildName = buildType.name
val timestamp = SimpleDateFormat("yyyyMMdd").format(Date())
val apkName = timestamp + "_AppName_" + defaultConfig.versionName + "." + defaultConfig.versionCode + "_" + buildName + ".apk"
outputFileName = apkName
}
}
If you don't want to use the timestamp, remove it. Otherwise, don't forget these lines on top of the file to import java classes:
import java.util.Date
import java.text.SimpleDateFormat
You can also compare the build name in order to specify a config for different builds, like so:
name == "debug"
/* or */
name.contains("release")
I have use little generic version of it. build and install fine.
for example your projectName is "Salad" then for staging apk name will be "Salad-staging-dd-MM-YY" you can also change for debug and release apk. hope my solution will be better.
Change in projectName/app/build.gradle
buildTypes {
debug{
}
staging{
debuggable true
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('dd-MM-yyyy')
def appName = getProjectDir().getParentFile().name
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace(getProjectDir().name +"-staging", "$appName-staging-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
// output.outputFile.name.replace("-debug", "-" + formattedDate)
)
}
}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
to change Android app name ref
Here is what I have done:
def appName
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", appName + "_V" + versionCode + ".apk"))
}
}
i successfully create apk in android studio 4.1v or above.
application level gradle
Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.11'
ext.play_services_version = '16.0.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//..............and another module level gradle within........//
buildTypes{
release {
def versionCode = "4.1.0"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
applicationVariants.all { variant ->
variant.outputs.all() { output ->
variant.productFlavors.each { flavor ->
def appName = "MyAPP" + versionCode + ".apk"
outputFileName = new File("./build/",
appName.replace(".apk", "_${flavor.name}.apk")
)
}
}
}
}
debug {
// ext.enableCrashlytics = false
// ext.alwaysUpdateBuildId = false
}
}
Im using gradle 7.5
my version of the filename consists in :
applicationVariants.all { variant ->
println("Generating variant: " + variant.getName())
variant.outputs.all { output ->
def variantName = variant.name
def versionName = variant.versionName
def formattedDate = new Date().format('dd_MM_YY')
output.versionCodeOverride = defaultConfig.versionCode * 10 + variant.productFlavors.get(0).abiVersionCode
//output.outputFileName = "${defaultConfig.applicationId}.${variantName}_v${versionName}_${defaultConfig.versionCode}.apk"
output.outputFileName = "${variantName}_v${versionName}_${defaultConfig.versionCode}_${formattedDate}.apk"
}
}
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)
}
}