How to generate output APK file with Customised name in Android Studio?

≡放荡痞女 提交于 2020-01-23 09:46:26

问题


I want to generate output apk file using customized name. Eg. By default, android studio generates "app-debug.apk" file. But I want it to be - "MyAppName_myCurrentProdFlavour_vMyVersionName.apk" How to do this using build.gradle file of module


回答1:


Easiest way

Before app_debug.apk

After com.pcvark.jobtest-v1(1.0)-debug.apk

android {
    ..
    defaultConfig {
        ...
        setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
    }

Tip You can also set versionNameSuffix for different buildType and variant (if you have). This will make different named apk for both debug and release.

buildTypes {
        debug {
          versionNameSuffix "-T"
        }
        release {
          versionNameSuffix "-R"
        }
    }

Further reading

https://stackoverflow.com/a/20660274/6891563

https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html




回答2:


You can use the following mechanism as seperately for both debug and release build which will allow you to provide different names for each build.

buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'MY-CUSTOM-APP' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }
            signingConfig signingConfigs.MyApp
        }
        release {
            debuggable false
            jniDebuggable false
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'MY-CUSTOM-APP' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }
            signingConfig signingConfigs.MyApp
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Happy to help!!!




回答3:


You can customize apk file name by using this:

Go to app Gradle

  setProperty("archivesBaseName", "yourapp-packagename-$versionName")

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        setProperty("archivesBaseName", "yourapp-packagename-$versionName")
    }

}




回答4:


Steps to generate apk with custom name :

// Update code for Newer version of Android

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 == "prod")
                initials = "prod";
            else if (flavor.name == "qa")
                initials = "qa";
            else if (flavor.name == "dev")
                initials = "dev";
            else if (flavor.name == "local")
                initials = "local";
            else if (flavor.name == "mock")
                initials = "mock";



            def build = "Debug";
            if (variant.buildType.name == "release")
                build = "Release"


            //def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
            def finalName = "FA-Shriram"+"_"+build+"_"+initials+"-v" + flavor.versionName+".apk";
            //output.outputFile = new File(output.outputFile.parent, finalName)
            output.outputFileName = finalName
        }


来源:https://stackoverflow.com/questions/51888667/how-to-generate-output-apk-file-with-customised-name-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!