解决WARNING: API ‘variant.getJavaCompile()’ is obsolete and has been replaced with ‘variant.getJavaCompileProvider()’

  • 内容
  • 评论
  • 相关
最近AndroidStudio升级到3.3版本,Android的Gradle plugin升级到了3.3.0,构建版本的时候出现一些警告。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WARNING: API 'variant.getGenerateBuildConfig()' is obsolete and has been replaced with 'variant.getGenerateBuildConfigProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance
 
WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance
 
WARNING: API 'variant.getMergeResources()' is obsolete and has been replaced with 'variant.getMergeResourcesProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance
 
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
因为app.gradle里的构建代码里使用javaCompiler、mergeResources等android gradle 的Task进行对资源和代码进行动态修改。虽说只是一个警告,但是总有一天会T除的。我就仔细看看了Android的gradle的构建代码,找到了解决方法。
先看一下代码:
注解写的很清楚了,javaCompiler已经@Deprecated了,建议使用javaComplieProvider
我们先看要修改的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
android {
    applicationVariants.all { variant ->
        def isRelease = variant.buildType.name.equalsIgnoreCase('release')
        if (isRelease) {
            variant.mergeResources.doFirst {
                if(variant.flavorName.equalsIgnoreCase('_0')){
                    //deleteAndroidOIcon(defAndroidOIconDir)
                }
            }
 
            variant.javaCompiler.doLast {
                if(variant.flavorName.equalsIgnoreCase('_0')){
                    //addAndroidOIcon(srcAndroidOIconDir)
                }
            }
        }
    }
}
那代码怎么修改呢?
我们从上边的TaskProvider<JavaCompile> getJavaCompileProvider();接口可以看出,返回的是TaskProvider<JavaCompile>,那我再看一下TaskProvider
很简单,到这里是不是知道怎么修改代码了,废话不多说了,我直接告诉大家怎么修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
android {
    applicationVariants.all { variant ->
        def isRelease = variant.buildType.name.equalsIgnoreCase('release')
        if (isRelease) {
            variant.mergeResourcesProvider.configure{
                it.doFirst{
                    if(variant.flavorName.equalsIgnoreCase('_0')){
                        //deleteAndroidOIcon(defAndroidOIconDir)
                    }
                }
            }
            variant.javaCompileProvider.configure {
                it.doLast{
                    if(variant.flavorName.equalsIgnoreCase('_0')){
                        //addAndroidOIcon(srcAndroidOIconDir)
                    }
                }
            }
        }
    }
}
注意一下我标红的代码,就是这么简单,其他类似的警告都这样修改一些,就不再有警告了,而且能正常运行。
Android Studio更新到3.4会出现以下警告:
1
2
3
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
这个警告引起的原因是构建脚本里使用了variant.getPackageLibrary()这个api,如果你搜索自己的项目没有找到使用的地方,那就是其它插件使用了。
据我知道的是kotlin编译的时候就使用了variant.getPackageLibrary(),还会有其它插件或者开源module在使用。例如:React Native、Fabric等。
React Native、Fabric只需升级到最新的版本。
kotlin还在修改当中,实时关注着更新,这个问题我相信kotlin会解决的。
如果帮到你了,请你打赏一下。