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

问题描述 投票:0回答:2

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

android gradle groovy
2个回答
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

0
投票

另外,我们可以获得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;
}
© www.soinside.com 2019 - 2024. All rights reserved.