【Android】Android Studio kotlin D

2024-05-28  本文已影响0人  天枢破军

前言

新版本的Android Studio修改了gradle.build脚本的写法,从原本的Groovy DSL改成了kotlin DSL,第一次接触有点不知所措。自己摸索了一下,写个记录给后来人指路。

如果你创建项目的时候选的是这个Kotlin DSL才需要本文的指导,如果选的是下面那个就不用看了,那个是旧版的写法。

新建项目时的选项

新老版本差异

其实只变了三个文件,如图所示

Kotlin DSL Groovy DSL

导入依赖包

此处以mmkv为例,Groovy DSL的写法是

implementation 'com.tencent:mmkv-static:1.0.24'

语句由以下几部分组成:

函数名 '组名:模块名:版本号'

函数名 也就是最前面的 implementation,有三种不同的函数,区别如下:
implementation:依赖不传递,打入最终APK中
api:依赖传递,打入最终APK中
compileOnly:仅编译时需要,最终APK中不包含

其中依赖传递的意思是,会不会将这个包传递给依赖自己的对象。

举例来说,假如我们有一个moduleHTTP module,封装了一层OkHttp,这个HTTP module自己依赖了OkHttp,此时如果我们把这个HTTP module给主APP用,如果在导入OkHttp依赖的时候这么写

implementation `com.squareup.okhttp3:okhttp:3.3.0'

那么我们可以在主APP调用HTTP module封装好的内容,但不可以使用OkHttp的内容,这就是依赖不传递
如果想要主APP可以直接调用OkHttp的内容,就需要依赖传递,也就是使用api函数来导包。

api `com.squareup.okhttp3:okhttp:3.3.0'

简单说就是implementation==privateapi==public
如果你的项目就一个module那这俩用哪个都一样。
compileOnly依赖的内容不会被编译进APK,一般用于测试相关的库以及注解相关的库,比如lombok之类的。

Kotlin DSL写法

有亿点麻烦,需要改两个文件,先打开gradle/libs.versions.toml这个文件

libs.versions.toml位置

默认的大概长这个样子

[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]下面的内容是每个包的版本号,键值对形式,前面是包的名字,后面是版本号。
[libraries]里面就是导包语句了,group对应组名,name对应模块名,version.ref对应版本号,可以不写,不写就是自动。
[plugins]下面的内容就等同于以前build.gradle里的plugins函数,上面默认的那段就等同于下面这段:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

现在我们添加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" }

还没完,还有一个文件要改,我们打开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写法

打开项目最外层的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") }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读