Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read value from local.properties via Kotlin DSL

I want to retreive key from local.properties file that looks like :

sdk.dir=C\:\\Users\\i30mb1\\AppData\\Local\\Android\\Sdk
key="xxx"

and save this value in my BuildConfig.java via gradle Kotlin DSL. And later get access to this field from my project.

like image 775
i30mb1 Avatar asked Mar 01 '20 10:03

i30mb1


People also ask

Is Kotlin a DSL?

The Navigation component provides a Kotlin-based domain-specific language, or DSL, that relies on Kotlin's type-safe builders. This API allows you to declaratively compose your graph in your Kotlin code, rather than inside an XML resource. This can be useful if you wish to build your app's navigation dynamically.

Where is Gradle local properties?

Instead, you should store it in the local. properties file, which is located in the root directory of your project and then use the Secrets Gradle Plugin for Android to read the API key.

What is Kotlin Gradle DSL?

Kotlin DSL brings the simplicity of the Kotlin language syntax and rich API set right into the script files on top of that code completion makes it perfect to work with Gradle script files. We will use this to manage our dependencies and project settings configurations more elegantly.


2 Answers

Okay. I found solutions.

For Android Projects :

  1. 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")
  1. And in the block buildTypes I write it:
buildTypes {
 getByName("debug") {
    buildConfigField("String", "key", key)
   }
}
  1. And in my Activity now I can retrieve this value:
override fun onCreate() {
    super.onCreate()
    val key = BuildConfig.key
}

For Kotlin Projects:

  1. 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)
}
  1. And use this extension when we would like
task("printKey") {
   doLast {
       val key = getLocalProperty("key")
       println(key)
   }
}
like image 144
i30mb1 Avatar answered Sep 19 '22 05:09

i30mb1


If you don't have access to gradleLocalProperties (it's only accessible for android projects):

val prop = Properties().apply {
    load(FileInputStream(File(rootProject.rootDir, "local.properties")))
}
println("Property:" + prop.getProperty("propertyName"))

Don't forget imports:

import java.io.File
import java.io.FileInputStream
import java.util.*
like image 34
Michał Klimczak Avatar answered Sep 19 '22 05:09

Michał Klimczak