我最近升级到Gradle 3.0,现在发现重命名输出APK的功能已更改。我想我可以解决这个问题,但我想知道的是我是否仍然可以选择APK的目标目录。我们现有的软件使用我想要维护的特定APK命名约定和目录结构。有没有办法做到这一点?
这是我当前的gradle构建结构(简化并重命名以保护无辜者):
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.mycompany.myapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 23
versionName "23.23.23"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7 }
signingConfig signingConfigs.config
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.config
}
}
productFlavors.whenObjectAdded { flavor ->
// Add the property 'myCustomProperty' to each product flavor and set the default value to 'customPropertyValue'
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
productFlavors {
MyCompany {
signingConfig signingConfigs.config
directoryPath = mycompany
}
Copper {
applicationId c
signingConfig signingConfigs.config
directoryPath = 'copper'
}
Steel {
applicationId 'com.company2.steel'
signingConfig signingConfigs.config
directoryPath = 'steel'
}
Lead {
applicationId 'com.company3.coal'
signingConfig signingConfigs.config
directoryPath = 'coal'
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def path = "C:/AndroidBuilds/MyBuilds/" + variant.productFlavors[0].directoryPath + "/"
logger.error("Path = " + path)
def SEP = "-"
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
def version = variant.versionCode
def newApkName = path + version + SEP + flavor
logger.error("newApkName = " + newApkName)
output.outputFile = new File(newApkName + ".apk")
}
}
}
我知道现在有一个“Flavor Dimension”我将默认(我删除它只是为了使代码更清晰)。运行此构建的结果应该是生成4个不同的APK并放置在它们自己的目录结构中,前缀为版本号(例如“64-Iron.apk”)。
命名工作是替换为“outputfile”,但目录结构没有。在最新的Gradle下有新的方法吗?
更新(已修复)
感谢所选解决方案提供的信息,为了完整性,这里是最终的gradle配置(再次,清理以保护无辜者):
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.mycompany.myapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 23
versionName "23.23.23"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7 }
signingConfig signingConfigs.config
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.config
}
}
productFlavors.whenObjectAdded { flavor ->
// Add the property 'myCustomProperty' to each product flavor and set the default value to 'customPropertyValue'
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
productFlavors {
MyCompany {
signingConfig signingConfigs.config
directoryPath = mycompany
}
Copper {
applicationId c
signingConfig signingConfigs.config
directoryPath = 'copper'
}
Steel {
applicationId 'com.company2.steel'
signingConfig signingConfigs.config
directoryPath = 'steel'
}
Lead {
applicationId 'com.company3.coal'
signingConfig signingConfigs.config
directoryPath = 'coal'
}
}
applicationVariants.all { variant ->
variant.outputs.all {
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
//add here your logic to customize the name of the apk
outputFileName = "${variant.versionCode}-${flavor}.apk"
}
variant.assemble.doLast { assemble ->
//copy the apk in another directory, add here your
//logic to customize the destination folder
copy {
from variant.outputs*.outputFile
into "C:/AndroidBuilds/MyBuilds//${variant.productFlavors[0].directoryPath}"
}
//if you don't want to delete the file after copying it comment the line below
delete variant.outputs*.outputFile
}
}
}
再次感谢MatPag!
答案 0 :(得分:11)
更新:从Gradle 3.3.0开始assemble
属性已弃用。这是执行此操作的新方法:
applicationVariants.all { variant ->
variant.outputs.all {
//add here your logic to customize the name of the apk
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
variant.assembleProvider.configure { assemble ->
assemble.doLast {
//copy the apk in another directory, add here your
//logic to customize the destination folder
copy {
from variant.outputs*.outputFile
//for Windows
into "C:/my_apks/${variant.dirName}"
}
//if you don't want to delete the file after copying it comment the line below
delete variant.outputs*.outputFile
}
}
}
在macOS / Linux中,你可以使用类似的东西作为目的地 路径:
into "${System.properties['user.home']}/my_apks/${variant.dirName}"
OLD ANSWER(对于AGP <3.3.0): 我使用Gradle 4.2.1和AGP 3.0.0玩了一下,可能的解决方案就是这个
applicationVariants.all { variant ->
variant.outputs.all {
//add here your logic to customize the name of the apk
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
variant.assemble.doLast { assemble ->
//copy the apk in another directory, add here your
//logic to customize the destination folder
copy {
from variant.outputs*.outputFile
into "C:/my_apks/${variant.dirName}"
}
//if you don't want to delete the file after copying it comment the line below
delete variant.outputs*.outputFile
}
}
我认为根据您的需求自定义文件夹是一个很好的起点:)