Programing

build.gradle의 local.properties에 정의 된 속성을 읽는 방법

crosscheck 2020. 10. 12. 07:14
반응형

build.gradle의 local.properties에 정의 된 속성을 읽는 방법


내가 설정 한 sdk.dirndk.dir에서 local.properties.

어떻게에 정의 된 값 읽습니까 sdk.dirndk.dir에서 build.gradle파일을?


다음과 같이 할 수 있습니다.

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

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

속성 파일이 동일한 하위 프로젝트 디렉토리에있는 경우 project.


local.properties

default.account.iccid=123

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", "")
}

코드에서는 리소스에서 다른 문자열로 가져옵니다.

resources.getString(R.string.default_account_iccid);

@rciovati의 대답은 확실히 정확하지만 sdk.dir값을 읽는 다른 방법도 있습니다 ndk.dir.

Gaku Ueda (Getting ndk directory) 의이 블로그 항목 에서 지적했듯이 BasePlugin클래스는 getNdkFolder()및에 대한 메서드를 제공합니다 getSdkFolder().

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

참고 : 라이브러리를 구축하는 경우 로 변경 com.android.application해야 com.android.library할 수 있습니다.

이것은 폴더 값을 읽는 더 우아한 방법 일 수 있습니다. @rciovati가 제공하는 대답은 속성 파일의 모든 값을 읽을 수 있으므로 더 유연하다고 말해야합니다.


위에서 local.properties를 수동으로로드하는 대답은 분명히 작동하며 어떤 플러그인이 적용되었는지 알아야하는 다음 대답도 작동합니다.

이러한 접근 방식은 응용 프로그램, 테스트 또는 라이브러리 플러그인을 사용하는지 여부에 관계없이 작동하기 때문에 더 일반적이기 때문에 일부에게는 조금 더 좋을 수 있습니다. 이 스 니펫은 또한 모든 Android 플러그인 구성 (제품 버전, 빌드 도구 버전 등)에 대한 완전한 프로그래밍 방식 액세스를 제공합니다.

Android Gradle 플러그인을 사용하는 build.gradle 파일에 액세스해야하는 경우 이제 직접 사용할 수 있으므로 Android DSL에 직접 액세스하기 만하면됩니다.

project.android.sdkDirectory

더 긴 형식 (아래)은 사용자 지정 Gradle Tasks 클래스 또는 플러그인을 만들거나 사용 가능한 속성을 확인하려는 경우에 유용합니다.

// 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}");

이 게시 당시에 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 execute method of a Task or place it AFTER the android DSL declaration in an Android app/libraries' build.gradle file).

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).


I think it's more elegant way.

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

it works on android gradle 1.5.0 .


I have set sdk.dir and ndk.dir in local.properties.

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

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

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

참고URL : https://stackoverflow.com/questions/21999829/how-do-i-read-properties-defined-in-local-properties-in-build-gradle

반응형