自定义了一个gradle插件,在里面用如下代码获取compileSdkVersion
val plugin = project.plugins.getPlugin(AppPlugin::class.java)
val sdkDirectory = plugin.baseExtension.sdkDirectory
println("androidExtension compileSdkVersion:${plugin.baseExtension.compileSdkVersion}")
val androidJarPath = "platforms/${plugin.baseExtension.compileSdkVersion}/android.jar"
发现plugin.baseExtension.compileSdkVersion的值为null,然后换一种方式获取
val androidExtension = project.extensions.getByName("android") as BaseExtension
val androidJarPath = "platforms/${androidExtension.compileSdkVersion}/android.jar"
一样为null,查来查去不知道为什么
后来发现是apply plugin: 的地方不对,必须下面代码之后,apply,因为的逻辑是在apply里面做的
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
因为gradle文件代码执行自上而下,必须先执行了设置compileSdkVersion,在apply插件,获取的值才没问题