Last active
January 21, 2023 13:49
-
-
Save Flying--Dutchman/bda88268ef890c50ed6432bff189c82c to your computer and use it in GitHub Desktop.
Easily read a property from gradle.properties and local.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From my Codeberg page: https://codeberg.org/FlyingDutchman/GradleReadProperty/src/branch/main/getSetting | |
// Simply copy following code in your gradle.build.kts file | |
// Also use the following imports: java.io.File and import java.util.* | |
fun getSetting(key: String) = project.getSetting(key) | |
fun Project.getSetting(key: String): String { | |
// Check if local properties | |
val localProperty = rootProject.file("local.properties") | |
if (localProperty.isFile) { | |
val props = Properties().apply { load(localProperty.inputStream())} | |
if (props.containsKey(key)) | |
return props[key].toString() | |
} | |
// Not in local, check gradle properties | |
if (!project.properties.containsKey(key)) | |
throw Exception("Key $key not found in gradle.properties") | |
return project.properties[key].toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment