如何将最新的稳定依赖项和插件添加到最新版本的 Android Studio Koala

问题描述 投票:0回答:1

我想在我的项目中实现MVVM架构。纽约的 Android Studio 是 Koala。 我的项目级别 gradle 文件如下所示

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
}

我的模块级 gradle 文件看起来像这样

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
}

android {
    namespace = "com.example.mydiceroller"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.mydiceroller"
        minSdk = 26
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.lifecycle.viewmodel.ktx)
    implementation(libs.androidx.lifecycle.runtime.compose)
    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)
}

我的版本目录(libs.version.toml)文件如下所示

`[versions]
agp = "8.5.0"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.3"
activityCompose = "1.9.0"
composeBom = "2024.04.01"

[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" }
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "lifecycleRuntimeKtx"}
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }`

我想使用 MVVM 架构,我正在遵循的教程已有两年了。我想申请最新的 构建依赖关系 匕首 - 刀柄 房间 Room 的 Kotlin 扩展和协程支持 这是示例,但我想应用最新的稳定版本来实现所有这些

    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0"
    implementation "androidx.navigation:navigation-compose:2.4.0-rc01"

    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-android-compiler:2.37"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-beta01'

    // Room
    implementation "androidx.room:room-runtime:2.3.0"
    kapt "androidx.room:room-compiler:2.3.0"

    // Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:2.3.0"

请帮我提供与我的项目兼容的所有最新版本。 我是 Android 新手,花了几乎一整天的时间尝试我能做什么但没有成功。

在我尝试过之后,我的文件看起来像 我的模块级 gradle 文件如下所示

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
//    alias(libs.plugins.devtools.ksp)
    alias(libs.plugins.com.google.dagger)

}

android {
    namespace = "com.example.todolist"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.todolist"
        minSdk = 26
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

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)
    implementation(libs.androidx.lifecycle.viewmodel.compose)
    implementation(libs.androidx.navigation.compose)
    implementation(libs.dagger.hilt.android)
    implementation(libs.dagger.hilt.android.compiler)
    implementation(libs.androidx.room.compiler)
    implementation(libs.androidx.room.runtime)
    implementation(libs.androidx.hilt.lifecycle.viewmodel)
    implementation(libs.androidx.hilt.compiler)
    implementation(libs.androidx.hilt.navigation.compose)
    implementation(libs.androidx.room.ktx)
    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)
}

我的项目级别 gradle 文件如下所示

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
//    alias(libs.plugins.android.library) apply false
}

我的版本目录(libs.versions.toml)看起来像这样

[versions]
agp = "8.5.1"
kotlin = "1.9.23"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.3"
activityCompose = "1.9.0"
composeBom = "2024.06.00"
navVersion = "2.7.7"
hiltAndroid = "2.51.1"
room = "2.6.1"
hiltviewmodel = "1.0.0-alpha03"
hiltcom = "1.2.0"
dagger= "2.51.1"

[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" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx"}
androidx-navigation-compose = {group = "androidx.navigation", name = "navigation-compose", version.ref = "navVersion"}
dagger-hilt-android = {group = "com.google.dagger", name = "hilt-android", version.ref = "hiltAndroid"}
dagger-hilt-android-compiler = {group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hiltAndroid"}
androidx-room-compiler = {group = "androidx.room", name = "room-compiler", version.ref = "room"}
androidx-room-runtime = {group = "androidx.room", name = "room-runtime", version.ref = "room"}
androidx-hilt-lifecycle-viewmodel = {group = "androidx.hilt", name = "hilt-lifecycle-viewmodel",     version.ref = "hiltviewmodel"}
   androidx-hilt-compiler = {group = "androidx.hilt", name = "hilt-compiler", version.ref = "hiltcom"}
   androidx-hilt-navigation-compose = {group = "androidx.hilt", name = "hilt-navigation-compose",        version.ref = "hiltcom" }
   androidx-room-ktx = {group = "androidx.room", name = "room-ktx", version.ref = "room"}

   [plugins]
   android-application = { id = "com.android.application", version.ref = "agp" }
   jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
   com-google-dagger = {id = "com.google.dagger.hilt.android", version.ref = "dagger"}
   #android-library = { id = "com.android.library", version.ref = "agp" }
   #kotlin-kapt = {id = "kapt", version.ref = "kapt" }
   #devtools-ksp = {id = "com.google.devtools.ksp", version.ref = "ksp"}

gradle 成功构建,但是当我运行我的项目时,它失败了,甚至没有显示 hello world。 错误是“发现重复的类 12 秒内构建失败”

kotlin mvvm android-gradle-plugin build.gradle kotlin-coroutines
1个回答
0
投票

这些问题是由错误的 Compose、Hilt 和 Room 设置引起的。但让我们一步一步来。

  1. 首先您应该更新版本目录中的Kotlin 版本。在撰写本文时,情况是:

    [versions]
    kotlin = "2.0.0"
    
  2. 然后您需要更新您的 Compose 设置。 Kotlin 2.0.0 发生了一些变化:

    • Compose 编译器现已与 Kotlin 一起发布,因此您需要将其添加到您的版本目录中:

      [plugins]   
      compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
      
    • 将插件添加到顶级 gradle 文件的

      plugins
      块:

      alias(libs.plugins.compose.compiler) apply false
      
    • 将插件添加到模块级 gradle 文件的

      plugins
      块中:

      alias(libs.plugins.compose.compiler)
      
    • 从模块级 gradle 文件中删除此内容:

      composeOptions {
          kotlinCompilerExtensionVersion = "1.5.1"
      }
      
  3. 然后你需要设置ksp插件。这是在编译时生成代码的其他库所需要的,例如 Hilt 和 Room(ksp 是 kapt 的现代替代品,因此不需要 kapt):

    [versions]
    ksp = "2.0.0-1.0.23" # https://github.com/google/ksp/releases
    
    [plugins]
    devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
    

    您需要确保 ksp 版本的第一部分与您的 Kotlin 版本匹配。请参阅此处以获取可用 ksp 版本的列表:https://github.com/google/ksp/releases

    您现在可以在

    plugins
    块的 gradle 文件中使用此插件。
    项目级别:

    alias(libs.plugins.devtools.ksp) apply false
    

    模块级别:

    alias(libs.plugins.devtools.ksp)
    
  4. 现在您可以设置Hilt。您在版本目录中定义了两次版本号,您应该将

    dagger
    hiltAndroid
    都替换为
    hilt
    (因为这是 Hilt 版本(不是 Dagger)并且该版本不是 Android 特定的)。您还应该将
    hiltcom
    重命名为
    hiltNavigationCompose
    以便更清晰:

    [versions]
    hilt = "2.51.1"
    hiltNavigationCompose = "1.2.0"
    

    您已经正确定义了以下库和插件:

    [libraries]
    androidx-hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
    dagger-hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
    dagger-hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" }
    
    [plugins]
    com-google-dagger = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
    

    但是您需要删除以下两个库(以及版本,以及它们在 gradle 文件中的使用位置):

    [versions]
    hiltviewmodel = "1.0.0-alpha03"
    
    [libraries]
    androidx-hilt-lifecycle-viewmodel = { group = "androidx.hilt", name = "hilt-lifecycle-viewmodel", version.ref = "hiltviewmodel" }
    androidx-hilt-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hilt" }
    

    然后您需要将插件添加到您的 gradle 文件中:

    // project:
    alias(libs.plugins.com.google.dagger) apply false
    // module (which you already have):
    alias(libs.plugins.com.google.dagger)
    

    最后在模块级gradle文件中添加依赖项。您已经拥有这些:

    implementation(libs.dagger.hilt.android)
    implementation(libs.androidx.hilt.navigation.compose)
    

    但是你需要将

    implementation(libs.dagger.hilt.android.compiler)
    更改为:

    ksp(libs.dagger.hilt.android.compiler)
    

    Hilt 现在应该在您的构建文件中正确设置。确保按照此处所述添加其他设置:https://developer.android.com/training/dependency-injection/hilt-android(应用程序类、AndroidManifest.xml、MainActivity 注释)

  5. 对于Room的设置,您的版本目录中已经有了正确的库:

    [versions]
    room = "2.6.1"
    
    [libraries]
    androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
    androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
    

    您不需要

    androidx-room-runtime
    ,可以将其删除。不过,与 Hilt 类似,您需要将 gradle 文件中的
    implementation(libs.androidx.room.compiler)
    替换为:

    ksp(libs.androidx.room.compiler)
    

您的构建文件现在应该没问题了。 gradle 同步后,您可能需要清理并重建您的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.