更新build.gradle 3.3.0中断applicationVariants.all apk程序集

时间:2019-01-30 20:32:07

标签: gradle android-gradle

我刚刚升级了Android项目的build.gradle以使用3.3.0版

com.android.tools.build:gradle:3.3.0

这样做会创建以下警告:

WARNING: API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'.
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 variant.getAssemble(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace.
Affected Modules: app

这是由应用build.gradle中的以下代码引起的:

android.applicationVariants.all { variant ->

    variant.assemble.doLast {
        variant.outputs.each { output ->
            def apk = variant.outputs[0].outputFile
            File versionInfo = new File(apk.parent, "versionInfo.properties")
            versionInfo.text = "versionCode=${android.defaultConfig.versionCode}\nversionName=${android.defaultConfig.versionName}"
            copy {
                from "${versionInfo}"
                into "$project.projectDir/temp_archive"
            }
        }
    }
}

我找不到在哪里可以重写此内容以使警告消失。我尝试遵循this Stack Overflow post,但没有取得任何进展。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

found a potential solution已解决该警告。下面是我正在使用的代码。如果有更好的解决方案,请暂时留出问题。

android.applicationVariants.all { variant ->

    variant.getAssembleProvider().get().doLast {
        variant.outputs.each { output ->
            def apk = variant.outputs[0].outputFile
            File versionInfo = new File(apk.parent, "versionInfo.properties")
            versionInfo.text = "versionCode=${android.defaultConfig.versionCode}\nversionName=${android.defaultConfig.versionName}"
            copy {
                from "${versionInfo}"
                into "$project.projectDir/temp_archive"
            }
        }
    }
}