"app-release.apk"如何更改此默认生成的 apk 名称 [英] " app-release.apk" how to change this default generated apk name

查看:68
本文介绍了"app-release.apk"如何更改此默认生成的 apk 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在 Android Studio 中生成签名的 apk 时,默认情况下它都会将名称命名为 app-release.apk...

我们可以做任何设置,以便它提示并询问需要分配给apk的名称(在eclipse中的方式)

我所做的是 - 在生成后重命名 apk.这不会出现任何错误,但是否有任何真正的方法可以让我对设置进行任何更改以获得提示.

注意::

在生成 apk android studio 时提示我选择位置(仅)

解决方案

是的,我们可以改变这种情况,但要多加注意

现在将其添加到您项目的 build.gradle 中,同时确保您已检查项目的 build 变体,例如 release 或 Debug所以在这里我将我的构建变体设置为 release 但你也可以选择 Debug .

 buildTypes {释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'签名配置 getSigningConfig()applicationVariants.all { 变体 ->variant.outputs.each { 输出 ->def date = new Date();def formattedDate = date.format('yyyyMMddHHmmss')output.outputFile = new File(output.outputFile.parent,output.outputFile.name.replace("-release", "-" + formattedDate)//对于调试使用 output.outputFile = new File(output.outputFile.parent,//output.outputFile.name.replace("-debug", "-" + formattedDate))}}}}

<块引用>

你可以用不同的方法来做到这一点

 defaultConfig {applicationIdcom.myapp.status"minSdk 版本 16目标SDK版本23版本代码 1版本名称1.0"setProperty("archivesBaseName", "COMU-$versionName")}

在 build.gradle 中使用 Set 属性方法并且不要忘记在运行项目之前同步gradle希望它能解决你的问题:)

<块引用>

最近由谷歌更新添加的处理此问题的新方法您现在可以根据风格或变体输出重命名您的构建//以下源代码来自开发者android文档有关更多详细信息,请访问上述文档链接
新插件破坏了使用 Variant API 操作变体输出的功能.它仍然适用于简单的任务,例如在构建期间更改 APK 名称,如下所示:

//如果使用 each() 遍历变体对象,//你需要开始使用 all().那是因为 each() 迭代//仅通过配置期间已经存在的对象—//但这些对象在新模型的配置时不存在.//但是, all() 通过按原样拾取对象来适应新模型//在执行期间添加.android.applicationVariants.all { 变体 ->变量.输出.所有{outputFileName = "${variant.name}-${variant.versionName}.apk";}}

<块引用>

重命名 .aab 包这是很好的David Medenjak 的回答

tasks.whenTaskAdded { task ->如果(task.name.startsWith(捆绑")){def renameTaskName = "rename${task.name.capitalize()}Aab";def flavor = task.name.substring("bundle".length()).uncapitalize()任务.创建(重命名任务名称,复制){def path = "${buildDir}/outputs/bundle/${flavor}/";从(路径)包括app.aab"目标目录文件(${buildDir}/outputs/renamedBundle/")重命名app.aab",${flavor}.aab";}task.finalizedBy(renameTaskName)}//@credit to David Medenjak 此代码块}

<块引用>

是否需要上面的代码

我在最新版本的 android studio 3.3.1 中观察到了什么

.aab 包的重命名由前面的代码完成,根本不需要任何任务重命名.

希望对大家有所帮助.:)

Whenever I generate a signed apk in Android Studio, by default it gives the name as app-release.apk...

Can we do any settings so that it should prompt and ask me the name which need to be assigned to the apk(the way it do in eclipse)

What I do is - rename the apk after it generated. This doesn't give any errors but is there any genuine way so that i can do any changes in settings to get a prompt.

Note::

while generating apk android studio is giving me a prompt to select the location(only)

解决方案

Yes we can change that but with some more attention

Now add this in your build.gradle in your project while make sure you have checked the build variant of your project like release or Debug so here I have set my build variant as release but you may select as Debug as well.

    buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig getSigningConfig()
                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)
    //for Debug use output.outputFile = new File(output.outputFile.parent,
   //                             output.outputFile.name.replace("-debug", "-" + formattedDate)
                        )
                    }
                }
            }
        }

You may Do it With different Approach Like this

 defaultConfig {
        applicationId "com.myapp.status"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        setProperty("archivesBaseName", "COMU-$versionName")
    }

Using Set property method in build.gradle and Don't forget to sync the gradle before running the projects Hope It will solve your problem :)

A New approach to handle this added recently by google update You may now rename your build according to flavor or Variant output //Below source is from developer android documentation For more details follow the above documentation link
Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time, as shown below:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

Renaming .aab bundle This is nicely answered by David Medenjak

tasks.whenTaskAdded { task ->
    if (task.name.startsWith("bundle")) {
        def renameTaskName = "rename${task.name.capitalize()}Aab"
        def flavor = task.name.substring("bundle".length()).uncapitalize()
        tasks.create(renameTaskName, Copy) {
            def path = "${buildDir}/outputs/bundle/${flavor}/"
            from(path)
            include "app.aab"
            destinationDir file("${buildDir}/outputs/renamedBundle/")
            rename "app.aab", "${flavor}.aab"
        }

        task.finalizedBy(renameTaskName)
    }
//@credit to David Medenjak for this block of code
}

Is there need of above code

What I have observed in the latest version of the android studio 3.3.1

The rename of .aab bundle is done by the previous code there don't require any task rename at all.

Hope it will help you guys. :)

这篇关于&quot;app-release.apk"如何更改此默认生成的 apk 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆