前言
新版本的Android Studio
修改了gradle.build
腳本的寫法岸军,從原本的Groovy DSL
改成了kotlin DSL
,第一次接觸有點(diǎn)不知所措瓦侮。自己摸索了一下艰赞,寫個(gè)記錄給后來人指路。
如果你創(chuàng)建項(xiàng)目的時(shí)候選的是這個(gè)Kotlin DSL
才需要本文的指導(dǎo)肚吏,如果選的是下面那個(gè)就不用看了方妖,那個(gè)是舊版的寫法。
新老版本差異
其實(shí)只變了三個(gè)文件罚攀,如圖所示
導(dǎo)入依賴包
此處以mmkv
為例党觅,Groovy DSL
的寫法是
implementation 'com.tencent:mmkv-static:1.0.24'
語句由以下幾部分組成:
函數(shù)名 '組名:模塊名:版本號'
函數(shù)名
也就是最前面的 implementation
,有三種不同的函數(shù)斋泄,區(qū)別如下:
implementation
:依賴不傳遞杯瞻,打入最終APK中
api
:依賴傳遞,打入最終APK中
compileOnly
:僅編譯時(shí)需要炫掐,最終APK中不包含
其中依賴傳遞的意思是魁莉,會不會將這個(gè)包傳遞給依賴自己的對象。
舉例來說募胃,假如我們有一個(gè)module
叫HTTP module
旗唁,封裝了一層OkHttp
,這個(gè)HTTP module
自己依賴了OkHttp
痹束,此時(shí)如果我們把這個(gè)HTTP module
給主APP
用检疫,如果在導(dǎo)入OkHttp
依賴的時(shí)候這么寫
implementation `com.squareup.okhttp3:okhttp:3.3.0'
那么我們可以在主APP
調(diào)用HTTP module
封裝好的內(nèi)容,但不可以使用OkHttp
的內(nèi)容参袱,這就是依賴不傳遞
电谣。
如果想要主APP
可以直接調(diào)用OkHttp
的內(nèi)容,就需要依賴傳遞
抹蚀,也就是使用api
函數(shù)來導(dǎo)包剿牺。
api `com.squareup.okhttp3:okhttp:3.3.0'
簡單說就是implementation
==private
,api
==public
如果你的項(xiàng)目就一個(gè)module
那這倆用哪個(gè)都一樣环壤。
compileOnly
依賴的內(nèi)容不會被編譯進(jìn)APK晒来,一般用于測試相關(guān)的庫以及注解相關(guān)的庫,比如lombok
之類的郑现。
Kotlin DSL寫法
有億點(diǎn)麻煩湃崩,需要改兩個(gè)文件荧降,先打開gradle/libs.versions.toml
這個(gè)文件
默認(rèn)的大概長這個(gè)樣子
[versions]
agp = "8.3.2"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.8.0"
activityCompose = "1.9.0"
composeBom = "2023.08.00"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
[versions]
下面的內(nèi)容是每個(gè)包的版本號,鍵值對形式攒读,前面是包的名字朵诫,后面是版本號。
[libraries]
里面就是導(dǎo)包語句了薄扁,group
對應(yīng)組名剪返,name
對應(yīng)模塊名,version.ref
對應(yīng)版本號邓梅,可以不寫脱盲,不寫就是自動。
[plugins]
下面的內(nèi)容就等同于以前build.gradle
里的plugins
函數(shù)日缨,上面默認(rèn)的那段就等同于下面這段:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
現(xiàn)在我們添加mmkv
的依賴钱反,照著舊的寫法抄
implementation 'com.tencent:mmkv-static:1.0.24'
把版本號抄到[versions]
下面,命名為mmkv
mmkv = "1.2.7"
把組名和模塊名抄到[libraries]
下面匣距,也命名為mmkv
mmkv = { group = "com.tencent", name = "mmkv-static",version.ref = "mmkv" }
改完以后長這樣
[versions]
agp = "8.3.2"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.8.0"
activityCompose = "1.9.0"
composeBom = "2023.08.00"
nanohttpd = "2.3.1"
mmkv = "1.2.7"
fastjson = "1.1.52.android"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
nanohttpd = { group = "org.nanohttpd", name = "nanohttpd",version.ref = "nanohttpd" }
mmkv = { group = "com.tencent", name = "mmkv-static",version.ref = "mmkv" }
fastjson = { group = "com.alibaba", name = "fastjson",version.ref = "fastjson" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
還沒完面哥,還有一個(gè)文件要改,我們打開APP
級別的build.gradle.kt
墨礁,找到最下面的dependencies
幢竹,大概長這樣:
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
在最下面加入這行
implementation(libs.mmkv)
就完事了
添加源
還是先講舊的寫法耳峦,找到最外層的build.gradle
恩静,在repositories
里添加一行
maven { url "源地址" }
比如說添加阿里云倉庫
// Top-level build file where you can add configuration options common to all sub-projects/modules.
//apply from: './config.gradle'
buildscript {
ext.kotlin_version = "1.5.0"
repositories {
mavenLocal()
google()
maven { url "https://maven.aliyun.com/repository/public" }
}
dependencies {
...
}
}
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://maven.aliyun.com/repository/public" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Kotlin DSL寫法
打開項(xiàng)目最外層的settings.gradle.kts
,然后在repositories
里添加一行
maven { setUrl("源地址") }
像這樣
pluginManagement {
repositories {
google {
content {
includeGroupByRegex("com\.android.*")
includeGroupByRegex("com\.google.*")
includeGroupByRegex("androidx.*")
}
}
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { setUrl("https://maven.aliyun.com/repository/public") }
}
}