项目评估后访问build.gradle中的sdk.dir值 [英] Access sdk.dir value in build.gradle after project evaluation

查看:20
本文介绍了项目评估后访问build.gradle中的sdk.dir值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 build.gradle 文件中有一个自定义任务,它在获取 dex 之前对类文件进行字节码转换,如下所示:

I have a custom task in my build.gradle file that does bytecode transformations on class files before getting dex'd that looks like this:

task droidcook(type: JavaExec) {
    main 'org.tsg.android.asm.Main'
}

afterEvaluate { project ->
    android.applicationVariants.each { variant ->
        variant.javaCompile.doLast {
            project.tasks.droidcook.configure {
                classpath variant.javaCompile.classpath
                classpath "build/classes/" + variant.dirName
                classpath sdk.dir + "/platforms/android-19/android.jar"
                classpath "compile-libs/droidcook.jar"
                args "build/classes/" + variant.dirName
                args "com.example"
                // args "-debug"
                // args "-asmifier"
            }
            project.tasks.droidcook.execute()
        }
    }
}

上面的问题是 classpath sdk.dir + ... 行,其中 sdk.dir 没有得到适当的评估.为了使其正常工作,我目前必须对 android.jar 的路径进行硬编码.

The issue with the above is the classpath sdk.dir + ... line where sdk.dir isn't evaluated appropriately. To get this working, I currently have to hard code the path to the android.jar.

如果您能回答一个适当的值来替换 android-19 以根据项目配置访问特定于平台的 android.jar ,则可以获得奖励积分.:D

Bonus points if you can answer with an appropriate value to replace android-19 with for accessing a platform specific android.jar based on project configuration. :D

推荐答案

我没有看到已发布的 API 可以将 sdk.dir 公开给您的构建文件,因此我只是从 https://android.googlesource.com/platform/tools/build/+/d69964104aed4cfae5052028b5c5e57580441ae8/gradle/src/main/groovy/com/android/build/gradle/internal/Sdk.groovy 手动阅读.至于替换 android-19,如果我阅读 android.compileSdkVersion,我会得到那个确切的字符串.我也不确定这是否是已发布的 API,因此它可能会在 Android Gradle 插件的未来版本中发生更改和损坏.

I don't see a published API to expose sdk.dir to your build file, so I just cribbed the code from https://android.googlesource.com/platform/tools/build/+/d69964104aed4cfae5052028b5c5e57580441ae8/gradle/src/main/groovy/com/android/build/gradle/internal/Sdk.groovy to read it manually. As for replacing android-19, if I read android.compileSdkVersion I get that exact string. I'm not sure that this is a published API either, so it may be subject to change and breakage in future versions of the Android Gradle plugin.

话虽如此,这对我有用:

With that said, this works for me:

afterEvaluate {
    def rootDir = project.rootDir
    def localProperties = new File(rootDir, "local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        def androidJarPath = sdkDir + "/platforms/" + android.compileSdkVersion + "/android.jar"
        print androidJarPath + "\n"
    }
}

如果没有local.properties,这段代码就会失败.如果您关心这种情况,您可以从 Sdk.groovy 复制更多代码以模仿其行为,试图找到合理的默认值.

If there's no local.properties, this code is going to fail. If you care about that case, you can copy more code from Sdk.groovy to mimic its behavior in trying to find a reasonable default.

这篇关于项目评估后访问build.gradle中的sdk.dir值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆