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