我正在尝试在gradle自动生成的APK文件名中设置特定的版本号。
现在gradle生成myapp-release.apk
,但我希望它看起来像myapp-release-1.0.apk
。
我尝试重命名看起来很混乱的选项。有没有简单的方法可以做到这一点?
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.each { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
我没有运气尝试过上面的代码。有什么建议么?(使用gradle 1.6)
我只需要在一个地方更改版本名称。代码也很简单。
下面的示例将创建名为MyCompany-MyAppName-1.4.8-debug.apk或MyCompany-MyAppName-1.4.8-release.apk的APK文件,具体取决于所选的构建变体。] >
请注意,此解决方案可同时在APK和App Bundles (.aab files)上使用
。另请参见:How to change the proguard mapping file name in gradle for Android project
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.company.app"
minSdkVersion 13
targetSdkVersion 21
versionCode 14 // increment with every release
versionName '1.4.8' // change with every release
setProperty("archivesBaseName", "MyCompany-MyAppName-$versionName")
}
}
以上解决方案已通过以下Android Gradle插件版本进行了测试:
随着新版本的发布,我将更新此帖子。
以下解决方案已通过以下Android Gradle插件版本进行了测试:
应用程序gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.company.app"
minSdkVersion 13
targetSdkVersion 21
versionCode 14 // increment with every release
versionName '1.4.8' // change with every release
archivesBaseName = "MyCompany-MyAppName-$versionName"
}
}
有很多答案完整或经过一些修改都是正确的。但是我还是要添加我的,因为我所有的人都遇到了问题,因为我正在使用脚本通过挂钩preBuild
任务来动态生成VersionName和VersionCode。
如果您使用的是类似方法,则此代码将起作用:
project.android.applicationVariants.all { variant ->
variant.preBuild.doLast {
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
output.outputFile.name.replace(".apk", "-${variant.versionName}@${variant.versionCode}.apk"))
}
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
output.outputFileName = output.outputFileName.replace(".apk", "-${variant.versionName}.apk")
}
}
就我而言,我通过这种方式解决了此错误
对于最新的gradle版本,您可以使用以下代码段:
首先设置您的应用程序清单位置
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
{
}
自Android Studio 1.1.0起,我发现此组合在build.gradle
文件的android主体中有效。如果您不知道如何导入清单xml文件数据。我希望它能得到Android Studio的更多支持,但请尝试使用这些值,直到获得所需的apk名称输出为止:
defaultConfig {
applicationId "com.package.name"
minSdkVersion 14
targetSdkVersion 21
versionCode 6
versionName "2"
}
signingConfigs {
release {
keyAlias = "your key name"
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "appName_" + versionName + ".apk"))
}
}
}
}
这解决了我的问题:使用applicationVariants.all
而不是applicationVariants.each
(已编辑,可与Android Studio 3.0和Gradle 4一起使用
总结起来,对于那些不知道如何在build.gradle
中导入包的人(像我一样,请使用以下buildTypes
,
如果未在defaultConfig块中指定versionName,则defaultConfig.versionName
将导致null
就我而言,我只是想找到一种方法来自动生成apk
和release
变体的不同debug
名称。通过将此代码段作为android
的子代码,我设法轻松地做到了这一点:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def appName = "My_nice_name_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def newName
if (buildType == 'debug'){
newName = "${appName}${defaultConfig.versionName}_dbg.apk"
} else {
newName = "${appName}${defaultConfig.versionName}_prd.apk"
}
output.outputFile = new File(output.outputFile.parent, newName)
}
}
另一种替代方法是使用以下方法:
根据@Jon answer重命名apk的正确方法>
defaultConfig {
applicationId "com.irisvision.patientapp"
minSdkVersion 24
targetSdkVersion 22
versionCode 2 // increment with every release
versionName "0.2" // change with every release
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//add this line
archivesBaseName = "AppName-${versionName}-${new Date().format('yyMMdd')}"
}