升级了Android Studio和Gradle之后,提示下面的警告:
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.
To determine what is calling variant.getJavaCompile(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace.
Affected Modules: asyncokhttp
之前gradle插件中,为了打包javadoc
,需要把工程中所有的classpath都找出来,因此调用了variant.javaCompile
,这是报错的原因。
如何解决
根据提示,我们只要把variant.javaCompile
=>variant.javaCompileProvider
就好了。
但是 variant.javaCompile.destinationDir
这种换了之后会报错,因为javaCompileProvider()
中没有destinationDir
这个方法;
经过查证,我们我们只可以这么做:
variant.javaCompile.destinationDir
=> it.destinationDir
variant.javaCompile.classpath.files
=> it.classpath.files
替换玩之后就不会报错了:
image.png
不报警告了,但是打包javadoc的时候,提示各种类找不到
it.classpath.files
是能获取到所有calsspath的,这没问题,问题出现在调用时序上,因此我们修改一下代码就好了:
task javadoc(type: Javadoc) {
options.encoding = "utf-8"
source = android.sourceSets.main.java.srcDirs
classpath += files(android.bootClasspath)
options {
//links "http://docs.oracle.com/javase/7/docs/api/"
linksOffline "https://developer.android.com/reference","${android.sdkDirectory}/docs/reference"
encoding = "UTF-8"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
android.libraryVariants.all { variant ->
variant.javaCompileProvider.configure{
javadoc.classpath += files(it.classpath.files)
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
这是完整的可用代码:
https://github.com/andforce/release-android-lib-to-jcenter/blob/master/android-tasks.gradle
网友评论