Android Gradle3.0以后多渠道打包报错问题解决

从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

第二点非常重要,这一点是上面警告的根源,更改代码之后,警告就消失了。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,168评论 0 10
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 声明:作者翻译论文仅为学习,如有侵权请...
    SnailTyan阅读 10,717评论 0 8
  • 30天的时间眨眼就过去了,在这30天收获最大的就是可以写出很多字了,不管写的内容好与坏,终于不是拿起笔来想半天,也...
    夏日么么茶_f4e5阅读 2,767评论 3 4
  • 单例模式是一种常用的设计模式、也可能是设计模式中代码量最少的设计模式。但是少并不意味着简单、想要用好、用对单例、就...
    阅历笔记阅读 2,934评论 0 1
  • 如果你回到过去,遇见了17岁的自己。有想过对自己说些什么呢?或者你想指引自己走上哪条人生道路呢?也许,你的高考不会...
    努力向上的鑫鑫同学阅读 3,895评论 0 2

友情链接更多精彩内容