如何在我的自定义gradle插件中获取compileSdkVersion

时间:2017-03-26 07:35:10

标签: android gradle groovy

我正在编写自定义gradle插件。 在我的自定义插件中,如何获取主机项目的compileSdkVersion。

2 个答案:

答案 0 :(得分:0)

您可以从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

答案 1 :(得分:0)

此外,我们甚至可以获得android.jar路径的compilesdkversion。用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;
}