我们可以进行任何设置,以便它应该提示并询问我要分配给apk的名称(它在eclipse中所做的方式)吗?
我的做法是-在生成后重新命名apk。这不会产生任何错误,但是否有任何真正的方法,使我可以在设置中进行任何更改以获得提示。
注意:
在生成apk时,Android Studio会给我一个提示选择位置(仅限)
release 或 Debug
。这里我将我的构建变量设置为 release
,但您也可以选择 Debug。 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)
)
}
}
}
}
defaultConfig {
applicationId "com.myapp.status"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", "COMU-$versionName")
}
在build.gradle文件中使用Set属性方法 在运行项目之前不要忘记同步gradle 希望这能解决您的问题 :)
Google最近推出了一种处理方法 现在,您可以根据flavor或Variant输出重命名构建 //以下来源于开发者Android文档 有关详细信息,请遵循上面的文档链接
使用Variant API操作变体输出与新插件不兼容。对于简单的任务仍然有效,例如在构建时更改APK名称,如下所示:
// 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"
}
}
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
}
需要上述代码吗?
我在最新版本的Android Studio 3.3.1中观察到以下内容
之前的代码已经完成了.aab捆绑包的重命名,不需要任何重命名任务。
希望这能帮助大家。 :)
如果您使用了最新的Android Gradle插件(3.0),可能会遇到以下错误:
无法设置只读属性“outputFile”的值
根据迁移指南,我们现在应该采用以下方法:
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${applicationName}_${variant.buildType.name}_${defaultConfig.versionName}.apk"
}
}
注意这里的两个主要更改:
all
现在用于迭代变量输出,而不是使用 each
。outputFileName
属性,而非改变文件引用。${applicationName}
解析为BuildType_Decorated{name=applicationName, debuggable=false, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscript...
(为了清晰简明而缩写)。这导致打包失败。对我来说${applicationId}
或${archivesBaseName}
是更好的选择。另外,${defaultConfig.versionName}
在我的情况下为null。因此,我最终选择${applicationId}_${variant.name}-${variant.versionName}
。 - pcdevdefaultConfig{}
块和buildTypes {release{}}
块中,都出现了相同的错误。目前还不确定如何解决此问题。 - CybeXdefaultConfig{}
同级别的 android{}
部分中使用的。但是,你也可以使用 android.applicationVariants.all { }
语法将其用于 android{}
之外。 - Johnny Doeandroid{}
塊的末尾,但仍然出現上面提到的錯誤 ->Gradle在底部顯示錯誤信息 https://pastebin.com/Kk2JwPGA - CybeX这里有一个更简短的方法:
defaultConfig {
...
applicationId "com.blahblah.example"
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
//or so
archivesBaseName = "$applicationId-v$versionCode($versionName)"
}
它会给你一个名为com.blahblah.example-v1(1.0)-debug.apk(在调试模式下)的文件。
Android Studio默认按构建类型名称添加versionNameSuffix,如果您想覆盖此选项,请按以下步骤操作:
buildTypes {
debug {
...
versionNameSuffix "-MyNiceDebugModeName"
}
release {
...
}
}
调试模式下的输出:com.blahblah.example-v1(1.0)-MyNiceDebugModeName.apk
(已编辑以适用于Android Studio 3.0和Gradle 4)
我正在寻找更复杂的APK文件名重命名选项,并编写了以下解决方案,可将APK重命名为以下数据:
您会得到这样的apk:myProject_dev_debug_1.3.6_131016_1047.apk。
您可以在此处找到完整的答案。希望能有所帮助!
在build.gradle中:
android {
...
buildTypes {
release {
minifyEnabled true
...
}
debug {
minifyEnabled false
}
}
productFlavors {
prod {
applicationId "com.feraguiba.myproject"
versionCode 3
versionName "1.2.0"
}
dev {
applicationId "com.feraguiba.myproject.dev"
versionCode 15
versionName "1.3.6"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def project = "myProject"
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def date = new Date();
def formattedDate = date.format('ddMMyy_HHmm')
def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
outputFileName = new File(newApkName)
}
}
}
> groovy.lang.MissingPropertyException: No such property: variantConfiguration for class: com.android.build.gradle.internal.variant.ApplicationVariantData
。为什么不直接改成 buildType = variant.buildType.name
? - soshial我修改了@Abhishek Chaubey的答案,以更改整个文件名:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
project.ext { appName = 'MyAppName' }
def formattedDate = new Date().format('yyyyMMddHHmmss')
def newName = output.outputFile.name
newName = newName.replace("app-", "$project.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 {
}
}
这将生成一个文件名,如:MyAppName-release20150519121617.apk
project.ext { appName = 'MyApplicatioName' }
- miharootProject.name
就可以获取项目名称,就像在 Eclipse 中一样。 - Hiren Dabhioutput.outputFile
是只读的。@johnny-doe的答案是新的解决方案。 - krishh我根据@Fer的答案编写了更通用的解决方案。
它还应该可以处理基于flavor和构建类型的applicationId
、versionName
和versionCode
配置。
在build.gradle中:
android {
...
applicationVariants.all { variant ->
variant.outputs.each { output ->
def appId = variant.applicationId
def versionName = variant.versionName
def versionCode = variant.versionCode
def flavorName = variant.flavorName // e. g. free
def buildType = variant.buildType // e. g. debug
def variantName = variant.name // e. g. freeDebug
def apkName = appId + '_' + variantName + '_' + versionName + '_' + versionCode + '.apk';
output.outputFile = new File(output.outputFile.parentFile, apkName)
}
}
}
示例apk名称:com.example.app_freeDebug_1.0_1.apk
有关variant
变量的更多信息,请参见ApkVariant和BaseVariant接口定义。
在您的应用级 Gradle 文件中添加以下类似于 android.applicationVariants.all
的代码块
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
lintOptions {
disable 'MissingTranslation'
}
signingConfig signingConfigs.release
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${applicationId}_${versionCode}_${variant.flavorName}_${variant.buildType.name}.apk"
}
}
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '_debug'
}
}
2019/03/25 可用。
flavor和split APK
对于APK
文件而言是如何工作的,而不是适用于Bundle / AAB文件
。android {
....
productFlavors {
aFlavor {
applicationId "com.a"
versionCode 5
versionName "1.0.5"
signingConfig signingConfigs.signingA
}
bFlavor {
applicationId "com.b"
versionCode 5
versionName "1.0.5"
signingConfig signingConfigs.signingB
}
cFlavor {
applicationId "com.c"
versionCode 3
versionName "1.0.3"
signingConfig signingConfigs.signingC
}
}
splits {
abi {
enable true
reset()
include 'arm64-v8a', 'x86', 'x86_64'
universalApk false
}
}
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
// New one or Updated one
output.outputFileName = "${variant.getFlavorName()}-${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}-${output.getFilter(com.android.build.OutputFile.ABI)}.apk"
// Old one
// output.outputFileName = "${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
}
}
}
发布版
aFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86_64.apk
aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86.apk
调试版
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86_64.apk
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86.apk
名称类似上面的版本,只是将前缀aFlavor
替换为bFlavor
,例如
bFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
名称类似上面的版本,只是将前缀aFlavor
替换为cFlavor
,
并分别更改versionCode
和versionName
cFlavor-release-v3_1.0.3-16Jan2020_21-26-arm64-v8a.apk
如需了解更多详细信息,请查看此问答线程。
AppName是您想要的应用程序名称,因此请进行替换。
variantName将是默认选择的变体或风味。
Date will Today's date,所以不需要做任何更改,只需粘贴即可。
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"
}
}