Android Groovy 腳本過渡為Kts腳本(2)

讀取gradle.properties 到 BuildConfig

先看看gradle.properties

# -------Gradle--------
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.caching=true
# -------Build parameters--------
# Values may be overridden in CI using gradlew "-Pname=value" param
apiBaseUrl="http://ws.audioscrobbler.com/2.0/"
# Typically we shouldn't store token in public repository, however this is just a sample project, so
# we can favour convenience (app can be compiled and launched after checkout) over security (each person who
# checkouts the project must generate own api key and change app configuration before running it).
# In real-live setup this key could be provided\overriden by CI.
apiToken="70696db59158cb100370ad30a7a705c1"
# -------Kotlin--------
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kapt.use.worker.api=true
# Enable Compile Avoidance, which skips annotation processing if only method bodies are changed in dependencies
# To turn on Compile Avoidance we need to turn off AP discovery in compile path.
kapt.include.compile.classpath=false
# -------Android-------
android.useAndroidX=true
android.enableJetifier=true
android.nonTransitiveRClass=true

從Gradle項(xiàng)目屬性中獲取值,并將其設(shè)置為Android構(gòu)建配置屬性,例如:
設(shè)置中存在apiToken變量。gradle文件將作為BuildConfig進(jìn)行訪問。應(yīng)用程序中的GRADLE_API_TOKEN。

自定義的一個(gè)比較重要的方法和Gradle提供的接口ApplicationDefaultConfig,利用kt的擴(kuò)展函數(shù)

fun ApplicationDefaultConfig.buildConfigFieldFromGradleProperty(gradlePropertyName: String) {
    val propertyValue = project.properties[gradlePropertyName] as? String
    checkNotNull(propertyValue) { "Gradle property $gradlePropertyName is null" }

    val androidResourceName = "GRADLE_${gradlePropertyName.toSnakeCase()}".toUpperCase()
    buildConfigField("String", androidResourceName, propertyValue)
}
還有 67% 的精彩內(nèi)容
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡信或評(píng)論聯(lián)系作者。
支付 ¥3.00 繼續(xù)閱讀

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容