gpt4 book ai didi

groovy - 从外部文件加载属性到 build.gradle

转载 作者:行者123 更新时间:2023-12-04 03:52:39 25 4
gpt4 key购买 nike

我有这个:

def loadProperties(String sourceFileName) {
def config = new Properties()
def propFile = new File(sourceFileName)
if (propFile.canRead()) {
config.load(new FileInputStream(propFile))
for (Map.Entry property in config) {
ext[property.key] = property.value;
}
}
}

loadProperties 'gradle.properties'

如何在 build.gradle 中引用属性(ndk.dir)?

def ndkBuild = new File("$ndk.dir", 'ndk-build')

有没有更好的方法从文件 gradle.properties 中读取 ndk.dir?我该如何使用它?

def ndkBuild = new File("$ndk.dir", 'ndk-build')

完整代码:

task buildNative(type: Exec) {
loadProperties 'gradle.properties'
if (System.getenv('NDK_HOME') != null || "$ndk.dir" != null) {
if ("$ndk.dir" != null) {
def ndkBuild = new File("$ndk.dir", 'ndk-build')
} else {
def ndkBuild = new File(System.getenv('NDK_HOME'), 'ndk-build')
}
workingDir "jni"
executable ndkBuild
} else {
throw new GradleException('Reason: NDK_HOME not set or ndk.dir is missing in gradle.properties...')
}
}

def loadProperties(String sourceFileName) {
def config = new Properties()
def propFile = new File(sourceFileName)
if (propFile.canRead()) {
config.load(new FileInputStream(propFile))
for (Map.Entry property in config) {
ext[property.key] = property.value;
}
}
}

最佳答案

就像ndk.dir一样,"$ndk.dir"首先获取ndk属性,然后是dir 属性,这不是您想要的。 (这在错误消息中也很明显,它说“找不到属性‘ndk’”。)相反,这应该有效:

def ndkBuild = new File(project.property('ndk.dir'), 'ndk-build')

一个更安全的解决方案是将整个 Properties 对象存储为一个额外的属性:

...
ext.externalProps = config

然后你可以像这样访问外部属性:

def ndkBuild = new File(externalProps['ndk.dir'], 'ndk-build')

关于groovy - 从外部文件加载属性到 build.gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20706161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com