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

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

问题描述

我有做字节code转换的类文件得到dex'd看起来像这样之前,我build.gradle文件自定义任务:

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()
        }
    }
}

与上面的问题是类路径sdk.dir + ... 行,其中 sdk.dir ISN T适当的评估。为了得到这个工作,我现在有到c的路径的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到您的构建文件,所以我只是cribbed了code从<一个href="https://android.googlesource.com/platform/tools/build/+/d69964104aed4cfae5052028b5c5e57580441ae8/gradle/src/main/groovy/com/android/build/gradle/internal/Sdk.groovy">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摇篮插件的未来版本。

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,这code是要失败的。如果你关心这种情况下,您可以复制多个code从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天全站免登陆