Android Gradle3.0以后多渠道打包报错问题解决
2019-05-31 本文已影响0人
Jaesoon
从gradle2.x升级到4.x之后,会收到这样一个警告。虽然不影响使用,但是看起来确实挺糟心的。
WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace.
Affected Modules: app
之前是这样的
android.applicationVariants.all { variant ->
if (variant.buildType.name == "sit") {
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
def task = project.tasks.create("copyAndRename${variant.name}Apk", Copy)
def fileName = output.outputFile.name
fileName = fileName.replace("-sit", "")
output.outputFile = new File(output.outputFile.parent + '/sit/', fileName)
task.doFirst {
println "Copying ${fileName}" + " to apk directory..."
}
task.doLast {
println "Done with sit"
}
task.from(output.outputFile)
task.into("deploy-sit")
task.dependsOn "assemble${variant.name.capitalize()}"
doSitTask.dependsOn task
}
}
}
}
改造之后是这样的
android.applicationVariants.all { variant ->
if (variant.buildType.name == "sit") {
variant.outputs.all {
outputFileName = outputFileName.replace("-sit", "")
def task = project.tasks.create("copyAndRename${variant.name}Apk", Copy)
task.doFirst {
println "Copying ${outputFileName}" + " to apk directory..."
}
task.doLast {
println "Done with sit"
}
task.from(variant.getPackageApplicationProvider().get().outputDirectory)
task.into("deploy-sit")
task.dependsOn "assemble${variant.name.capitalize()}"
doSitTask.dependsOn task
}
}
}
两个最重要的点:
- 更换输出安装包名称由设置output.outputFile改成设置outputFileName
- 将输出apk复制到指定目录,由原来的复制output.outputFile,改成复制variant.getPackageApplicationProvider().get().outputDirectory
第二点非常重要,这一点是上面警告的根源,更改代码之后,警告就消失了。