cod*_*R0y 159 android build gradle apk android-gradle-plugin
我正在尝试在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"))
}
}
Run Code Online (Sandbox Code Playgroud)
我试过上面的代码没有运气.有什么建议?(使用gradle 1.6)
Jon*_*Jon 209
我只需要在一个地方更改版本名称.代码也很简单.
下面的示例将创建名为MyCompany-MyAppName-1.4.8-debug.apk或 MyCompany-MyAppName-1.4.8-release.apk的 apk文件,具体取决于所选的构建变体.
请注意,此解决方案适用于APK和应用程序包(.aab文件).
另请参阅:如何在gradle for Android项目中更改proguard映射文件名
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")
}
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案已使用以下Android Gradle插件版本进行测试:
随着新版本的推出,我会更新这篇文章.
以下解决方案已使用以下Android Gradle插件版本进行测试:
app 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"
}
}
Run Code Online (Sandbox Code Playgroud)
cod*_*R0y 171
这解决了我的问题:使用applicationVariants.all而不是applicationVariants.each
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,似乎这不适用于0.14+版本的android studio gradle插件.
这样就可以了(参考这个问题 ):
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
Fer*_*Fer 41
(已编辑,可与Android Studio 3.0和Gradle 4配合使用)
我正在寻找一个更复杂的apk文件名重命名选项,我写了这个,希望它对任何人都有帮助.它使用以下数据重命名apk:
它花了我一些关于gradle类的研究和一些来自其他答案的复制/粘贴.我正在使用gradle 3.1.3.
在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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你今天(10-10-16)在10:47编译,你会得到以下文件名,具体取决于你选择的风味和构建类型:
注意:未对齐版本的apk名称仍然是默认名称.
Wes*_*ley 18
总而言之,对于那些不知道如何导入包的人build.gradle(比如我),请使用以下内容buildTypes,
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
def manifestParser = new com.android.builder.core.DefaultManifestParser()
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
=====编辑=====
如果您在文件中设置versionCode和versionName,build.gradle就像这样:
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0.0"
}
Run Code Online (Sandbox Code Playgroud)
你应该这样设置:
buildTypes {
release {
signingConfig signingConfigs.releaseConfig
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
======使用Android Studio 1.0编辑======
如果您使用的是Android Studio 1.0,则会出现如下错误:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.
Run Code Online (Sandbox Code Playgroud)
您应该将build.Types部件更改为:
buildTypes {
release {
signingConfig signingConfigs.releaseConfig
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ark*_*ior 17
如果未在defaultConfig块中指定versionName,defaultConfig.versionName则会生成null
要从manifest中获取versionName,您可以在build.gradle中编写以下代码:
import com.android.builder.DefaultManifestParser
def manifestParser = new DefaultManifestParser()
println manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
Run Code Online (Sandbox Code Playgroud)
就我而言,我只是想找到一种方法来自动生成不同的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)
}
}
Run Code Online (Sandbox Code Playgroud)
对于新的Android gradle插件3.0.0,您可以执行以下操作:
applicationVariants.all { variant ->
variant.outputs.all {
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"
}
outputFileName = newName
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生类似于: My_nice_name_3.2.31_dbg.apk
语法已经改变了一点在摇篮4(Android Studio中3+)(来自output.outputFile于outputFileName,从构思这个答案现在是:
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName = outputFileName
newName.replace(".apk", "-${variant.versionName}.apk")
outputFileName = new File(newName)
}
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用以下方法:
String APK_NAME = "appname"
int VERSION_CODE = 1
String VERSION_NAME = "1.0.0"
project.archivesBaseName = APK_NAME + "-" + VERSION_NAME;
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 15
targetSdkVersion 21
versionCode VERSION_CODE
versionName VERSION_NAME
}
.... // Rest of your config
}
Run Code Online (Sandbox Code Playgroud)
这将为您的所有apk输出设置"appname-1.0.0".
按照@Jon答案重命名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')}"
}
Run Code Online (Sandbox Code Playgroud)
或者通过另一种方法可以达到相同的结果
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def formattedDate = new Date().format('yyMMdd')
outputFileName = "${outputFileName.replace(".apk","")}-v${defaultConfig.versionCode}-${formattedDate}.apk"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77200 次 |
| 最近记录: |