How do I read properties defined in local.properties in gradle.properties
This won't happen, because Java Properties are simple strings, there is no logic applied automatically.
However, you can simply use a gradle.properties
file in the .gradle
folder of your user home directory to define environment-related settings instead of project-related settings. This file won't be uploaded to a VCS.
Gradle property in local.properties
As in https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties described you could use a grade.properties file in your GRADLE_USER_HOME directory (default $HOME/.gradle).
How to read a properties files and use the values in project Gradle script?
If using the default gradle.properties
file, you can access the properties directly from within your build.gradle
file:
gradle.properties
:
applicationName=Admin
projectName=Hello Cool
build.gradle
:
task printProps {
doFirst {
println applicationName
println projectName
}
}
If you need to access a custom file, or access properties which include .
in them (as it appears you need to do), you can do the following in your build.gradle
file:
def props = new Properties()
file("build.properties").withInputStream { props.load(it) }
task printProps {
doFirst {
println props.getProperty("application.name")
println props.getProperty("project.name")
}
}
Take a look at this section of the Gradle documentation for more information.
Edit
If you'd like to dynamically set up some of these properties (as mentioned in a comment below), you can create a properties.gradle
file (the name isn't important) and require it in your build.gradle
script.
properties.gradle
:
ext {
subPath = "some/sub/directory"
fullPath = "$projectDir/$subPath"
}
build.gradle
apply from: 'properties.gradle'
// prints the full expanded path
println fullPath
Read value from local.properties via Kotlin DSL
Okay. I found solutions.
For Android Projects :
- In my build.gradle.kts I create a value that retrieves my key:
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
val key: String = gradleLocalProperties(rootDir).getProperty("key")
- And in the block
buildTypes
I write it:
buildTypes {
getByName("debug") {
buildConfigField("String", "key", key)
}
}
- And in my Activity now I can retrieve this value:
override fun onCreate() {
super.onCreate()
val key = BuildConfig.key
}
For Kotlin Projects:
- We can create an extension that help us to retrieve desired key:
fun Project.getLocalProperty(key: String, file: String = "local.properties"): Any {
val properties = java.util.Properties()
val localProperties = File(file)
if (localProperties.isFile) {
java.io.InputStreamReader(java.io.FileInputStream(localProperties), Charsets.UTF_8).use { reader ->
properties.load(reader)
}
} else error("File from not found")
return properties.getProperty(key)
}
- And use this extension when we would like
task("printKey") {
doLast {
val key = getLocalProperty("key")
println(key)
}
}
setting property read from local.properties inside build.gradle
The error you're getting is correct. To set a property you need to use ext
. Please have a look at the docs.
So the following piece of code will do the job:
File secretPropsFile = file('./local.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
throw new IllegalStateException("secret.properties could not be located for build process")
}
println project.nexusPassword
println project.nexusUsername //property is set in project's scope via ext
Related Topics
Android Sharedpreferences in Fragment
Debugging with Android Studio Stuck at "Waiting for Debugger" Forever
Android Home Screen Widget (Icon, Label - Style)
How to Disable Crashlytics During Development
Error Building Player: Commandinvokationfailure: Unable to Convert Classes into Dex Format
Progress Bar While Loading Image Using Glide
How to Handle One-Shot Operations in Jetpack Compose
Get List of Active Pendingintents in Alarmmanager
How to Launch an Activity Without a Ui
When Should One Use Rxjava Observable and When Simple Callback on Android