Android 如何读取 build.gradle 中 local.properties 中定义的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21999829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:24:43  来源:igfitidea点击:

How do I read properties defined in local.properties in build.gradle

androidandroid-gradle-plugin

提问by Vikram

I have set sdk.dirand ndk.dirin local.properties.

我已经设置sdk.dirndk.dirlocal.properties.

How do I read the values defined in sdk.dirand ndk.dirin the build.gradlefile?

如何阅读中定义的值sdk.dirndk.dirbuild.gradle文件?

回答by rciovati

You can do that in this way:

你可以这样做:

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def sdkDir = properties.getProperty('sdk.dir')
def ndkDir = properties.getProperty('ndk.dir')

Use project.rootProjectif you are reading the properties file in a sub-project build.gradle:

使用project.rootProject,如果你正在阅读一个子项目的属性文件build.gradle

.
├── app
│?? ├── build.gradle <-- You are reading the local.properties in this gradle build file
│?? └── src
├── build.gradle
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── local.properties

In case the properties file is in the same sub-project directory you can use just project.

如果属性文件位于同一个子项目目录中,您可以只使用project.

回答by Dmitrijs

local.properties

本地属性

default.account.iccid=123

build.gradle -

build.gradle -

def Properties properties = new Properties()
properties.load(project.rootProject.file("local.properties").newDataInputStream())

defaultConfig {

    resValue "string", "default_account_iccid", properties.getProperty("default.account.iccid", "")
}

and in code you get it as other string from Resources -

在代码中,您将它作为来自资源的其他字符串 -

resources.getString(R.string.default_account_iccid);

回答by super-qua

Although @rciovati's answer is certainly correct, there is also an alternative way of reading the values for sdk.dirand ndk.dir.

虽然@ rciovati的答案当然是正确的,也有阅读价值的替代方式sdk.dirndk.dir

As pointed out in this blog entry by Gaku Ueda(Getting ndk directory) the BasePluginclass offers methods for getNdkFolder()and getSdkFolder():

正如Gaku Ueda(获取 ndk 目录)在这篇博客文章中指出的,BasePlugin该类提供了用于getNdkFolder()和的方法getSdkFolder()

def ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
def sdkDir = project.plugins.findPlugin('com.android.application').getSdkFolder()

Note: You may have to change com.android.applicationto com.android.libraryif you are building a library

注意:如果您正在构建库com.android.applicationcom.android.library则可能需要更改为

This is maybe a more elegant way of reading the folder values. Although it has to be said that the answer provided by @rciovati is more flexible, as one could read any value in the properties file.

这可能是一种更优雅的读取文件夹值的方式。尽管不得不说@rciovati 提供的答案更灵活,因为可以读取属性文件中的任何值。

回答by PaulR

The answer that loads local.properties manually above obviously works, and the next one that requires you to know which plugin was applied should work as well.

上面手动加载 local.properties 的答案显然有效,下一个要求您知道应用了哪个插件的答案也应该有效。

These approaches might be a little better for some since they are more generic because they work regardless of whether you're using the Application, Test, or Library plugin. These snippets also give you full programmatic access to all of the Android plugin config (Product Flavors, Build Tools version, and much more):

这些方法对某些人来说可能会更好一些,因为它们更通用,因为无论您使用的是应用程序、测试还是库插件,它们都可以工作。这些片段还使您可以完全编程访问所有 Android 插件配置(产品风格、构建工具版本等):

If you need access in a build.gradle file that is using the Android Gradle Plugin simply access the Android DSL directly as it's now available directly:

如果您需要访问使用 Android Gradle 插件的 build.gradle 文件,只需直接访问 Android DSL,因为它现在可以直接使用:

project.android.sdkDirectory

The longer form (below) of this is handy if you're creating custom Gradle Tasks classes or Plugins or simply want to view which properties are available.

如果您正在创建自定义 Gradle 任务类或插件,或者只是想查看哪些属性可用,则较长的形式(如下)会很方便。

// def is preferred to prevent having to add a build dependency.
def androidPluginExtension = project.getExtensions().getByName("android");

// List available properties.
androidPluginExtension.properties.each { Object key, Object value ->
    logger.info("Extension prop: ${key} ${value}")
}
String sdkDir = androidPluginExtension.getProperties().get("sdkDirectory");
System.out.println("Using sdk dir: ${sdkDir}");

At the time of this posting there is also a handy adbExeproperty that is definitely worth noting.

在发布这篇文章时,还有一个非常adbExe值得一提的便利属性。

This code has to execute AFTER the Android Gradle Plugin is configured per the Gradle livecycle. Typically this means you put it in the executemethod of a Taskor place it AFTER the androidDSL declaration in an Android app/libraries' build.gradlefile).

此代码必须在根据 Gradle 生命周期配置 Android Gradle 插件后执行。通常,这意味着您将其放在 a 的execute方法中Task或将其android放在 Android 应用程序/库build.gradle文件中的DSL 声明之后)。

These snippets also come with the caveat that as you upgrade Android Gradle Plugin versions these properties can change as the plugin is developed so simply test when moving between versions of the Gradle and Android Gradle plugin as well as Android Studio (sometimes a new version of Android Studio requires a new version of the Android Gradle Plugin).

这些片段还附带警告,当您升级 Android Gradle 插件版本时,这些属性可能会随着插件的开发而改变,因此在 Gradle 和 Android Gradle 插件以及 Android Studio(有时是新版本的 Android Studio 需要新版本的 Android Gradle 插件)。

回答by Victor Choy

I think it's more elegant way.

我认为这是更优雅的方式。

println "${android.getSdkDirectory().getAbsolutePath()}"

it works on android gradle 1.5.0 .

它适用于 android gradle 1.5.0 。

回答by serv-inc

I have set sdk.dirand ndk.dirin local.properties.

我已经设置sdk.dirndk.dirlocal.properties.

You might reconsider if you want to manually set values in local.propertiesas that is already in use by Android Studio (for the root project), and

您可能会重新考虑是否要手动设置值,local.properties因为Android Studio 已在使用该(对于根项目),并且

you should not modify this file manuallyor check it into your version control system.

不应手动修改此文件或将其签入您的版本控制系统。

but see the specific exemption about cmake listed in the comments.

但请参阅评论中列出的有关 cmake 的特定豁免。