我正在编写一个自定义gradle插件。在我的自定义插件中,如何获取主机项目的compileSdkVersion。
你可以从project.android.compileSdkVersion
检索它
apply plugin: CompileSdkVersionPlugin
...
class CompileSdkVersionPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('compileSdkVersion') {
doLast {
println project.android.compileSdkVersion
}
}
}
}
然后终端中的gradle -q compileSdkVersion
命令将打印此输出:
android-25
另外,我们可以获得compilesdkversion甚至android.jar路径。在java代码中
private static String getAndroidJarPath(Project project) {
AppExtension appExtension = (AppExtension)project.getProperties().get("android");
String sdkDirectory = appExtension.getSdkDirectory().getAbsolutePath();
String compileSdkVersion = appExtension.getCompileSdkVersion();
sdkDirectory = sdkDirectory + File.separator + "platforms" + File.separator
String androidJarPath = sdkDirectory + compileSdkVersion + File.separator + "android.jar";
return androidJarPath;
}