如何在Android Studio中生成具有自定义名称的输出APK文件?

时间:2018-08-17 05:04:16

标签: android android-studio android-gradle build.gradle

我想使用自定义名称生成输出apk文件。例如。默认情况下,Android Studio会生成“ app-debug.apk”文件。但我希望它是-“ MyAppName_myCurrentProdFlavour_vMyVersionName.apk” 如何使用模块的 build.gradle 文件

执行此操作

4 个答案:

答案 0 :(得分:1)

对于debugrelease构建,您可以分别使用以下机制,这将允许您为每个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'
        }
    }

很乐意为您服务!

答案 1 :(得分:1)

最简单的方法

app_debug.apk

之前

com.pcvark.jobtest-v1(1.0)-debug.apk 之后

f

提示,您还可以为不同的android { .. defaultConfig { ... setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")") } 和变体(如果有)设置versionNameSuffix。这将为buildTypedebug创建不同的命名apk。

release

进一步阅读

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

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

答案 2 :(得分:1)

您可以使用以下方法自定义apk文件名称:

转到应用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")
    }

}

答案 3 :(得分:0)

生成具有自定义名称的APK的步骤:

//更新更新版本的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
        }