Android 用Gradle更改apk名称
我有一个Android项目,它使用Gradle进行构建过程。现在我想添加两个额外的生成类型staging和production,因此我的build.gradle包含:Android 用Gradle更改apk名称,android,gradle,apk,android-gradle-plugin,Android,Gradle,Apk,Android Gradle Plugin,我有一个Android项目,它使用Gradle进行构建过程。现在我想添加两个额外的生成类型staging和production,因此我的build.gradle包含: android { buildTypes { release { runProguard false proguardFile getDefaultProguardFile('proguard-android.txt') }
android {
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
staging {
signingConfig signingConfigs.staging
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
production {
signingConfig signingConfigs.production
}
}
}
我的AppnVersionNameVersionCode看起来像:
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)
}
若我执行TaskAssembleStaging,那个么我会得到我的apk的正确名称,但当我执行assembleProduction时,我会得到我的apk的更改名称(就像在登台案例中一样)。例如:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
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)
}
}
看起来生产中的生成类型是执行appendVersionNameVersionCode的。我怎样才能避免它呢?正如Commonware在他的评论中写道的那样,您应该只为登台变体调用
appendVersionNameVersionCode
。您可以轻松做到这一点,只需稍微修改appendVersionNameVersionCode
方法,例如:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
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的解决方案不适用于Android Gradle插件0.14.3+,因为删除了不推荐的API: 几乎1.0:删除了不推荐使用的属性/方法
- Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest(使用变量输出)
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)
}
}
我使用了它的一些通用版本。构建和安装良好。 例如,您的项目名称为“salar”,那么对于staging apk,名称将为“salar staging dd MM YY”,您还可以更改为debug和release apk。希望我的解决方案会更好。
更改
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'
}
要更改Android应用程序名称,我已经做了以下工作:
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"))
}
}
我在android studio 4.1v或更高版本中成功创建了apk。 应用级梯度 顶级生成文件,您可以在其中添加所有子项目/模块通用的配置选项
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
}
}
对于较新的android studio Gradle
AppName是您想要的应用程序名称,请将其替换variantName将是默认选择的变体或风味
日期将是今天的日期,因此需要进行任何更改,只需将其粘贴
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"
}
}
输出:
AppName_release_D_26-04-2021_V_1.2.apkI怀疑您不想迭代
应用程序变体
,而只想迭代那些与登台
相关的变体。不过,我对Gradle for Android对象模型还不够熟悉,无法具体告诉您将使用什么。谢谢,现在它可以工作了。我只是对这个条件做了一些小的修改。现在它看起来像:if(variant.buildType.name==android.buildTypes.staging.name)。您在哪里找到新的变体api(即输出属性)的文档(或任何提示/解释)?我在谷歌上搜索了它,发现了这个:;)我有最新的gradle和android studio版本,但有趣的是,我得到了错误消息“找不到属性”输出“在尝试上述代码片段时,AS 1.1中有进一步的更改,您可以在这里找到一个更新的脚本:呃,有人能告诉这些开发人员保持API的一致性几个月以上吗?”!在最新版本中被弃用。。。[4]