项目评估后build.gradle访问sdk.dir值

编程入门 行业动态 更新时间:2024-10-27 04:33:40
本文介绍了项目评估后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从android.googlesource/platform/tools/build/+/d69964104aed4cfae5052028b5c5e57580441ae8/gradle/src/main/groovy/com/android/build/gradle/internal/Sdk.groovy手动阅读它。至于更换的android-19,如果我读androidpileSdkVersion我得到的确切字符串。我不知道,这是一个发布的API要么,所以它可能会有所变动和破损在Android摇篮插件的未来版本。

I don't see a published API to expose sdk.dir to your build file, so I just cribbed the code from android.googlesource/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 androidpileSdkVersion 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/" + androidpileSdkVersion + "/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值

本文发布于:2023-07-28 00:16:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   build   gradle   sdk   dir

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!