我在AndroidStudio中创建了一个简单的项目,并添加了依赖项。 Android Studio 刺猬 | 2023.1.1 Canary 16 Java 17.0.7 也适用于gradle
libs.versions.toml
[versions]
agp = "8.1.0"
kotlin = "1.8.22"
ksp = "1.9.0-1.0.13"
hilt = "2.47"
core-ktx = "1.10.1"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.9.0"
[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
daggerHilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kotlinKsp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
项目中的build.gradle.kts
Top-level build file where you can add configuration options common to all sub-projects/modules.
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlinKsp) apply false
alias(libs.plugins.daggerHilt) apply false
}
true // Needed to make the Suppress annotation work for the plugins block
在 app 模块中的 build.gradle.kts 文件
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.kotlinKsp)
alias(libs.plugins.daggerHilt)
}
android {
namespace = "com.example"
compileSdk = 34
defaultConfig {
applicationId = "com.example"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}
dependencies {
implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
Material3
implementation "androidx.compose.material:material:$compose_version"
implementation("androidx.compose.material:material-icons-extended-android:1.5.0")
Compose
implementation("androidx.compose.ui:ui:1.5.0")
implementation("androidx.compose.ui:ui-tooling-preview:")
debugImplementation("androidx.compose.ui:ui-tooling:1.5.0")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.5.0")
implementation("androidx.activity:activity-compose:1.7.2")
lifecycler
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
implementation("androidx.navigation:navigation-compose:2.7.0")
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
Tests
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.5.0")
Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
//Dagger - Hilt
implementation("androidx.hilt:hilt-android:2.47")
ksp("com.google.dagger:hilt-android-compiler:2.47")
Room Database
implementation("androidx.room:room-ktx:2.5.2")
ksp("androidx.room:room-compiler:2.5.2")
Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:2.5.2")
androidTestImplementation("androidx.room:room-testing:2.5.2")
}
settings.gradle.kts
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
>! }
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
>! }
}
>!
rootProject.name = "Example"
include(":app")
>!
gradle.properties:
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
kotlin.code.style=official
android.nonTransitiveRClass=true
gradle-wrapper.properties:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
同步项目时出现错误:
无法加载类'org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptionsHelper' org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptionsHelper Gradle的依赖缓存可能已损坏(有时在网络连接超时后会发生此情况)。 重新下载依赖项并同步项目(需要网络) Gradle构建进程(守护程序)的状态可能已损坏。停止所有Gradle守护程序可能会解决此问题。 停止Gradle构建进程(需要重新启动) 您的项目可能正在使用与项目中的其他插件或项目请求的Gradle版本不兼容的第三方插件。 在Gradle进程损坏的情况下,您还可以尝试关闭IDE,然后终止所有Java进程。
./gradlew cleanBuildCache
但是出现了另一个错误:
Variant 'apiElements' capability com.android.tools.build:gradle:8.1.0 declares a library, packaged as a jar, and its dependencies declared externally:
Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
Other compatible attribute:
Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
Variant 'javadocElements' capability com.android.tools.build:gradle:8.1.0 declares a component for use during runtime, and its dependencies declared externally:
Incompatible because this component declares documentation and the consumer needed a library
Other compatible attributes:
Doesn't say anything about its target Java version (required compatibility with Java 8)
Doesn't say anything about its elements (required them packaged as a jar)
Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
Variant 'runtimeElements' capability com.android.tools.build:gradle:8.1.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
Other compatible attribute:
Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
Variant 'sourcesElements' capability com.android.tools.build:gradle:8.1.0 declares a component for use during runtime, and its dependencies declared externally:
Incompatible because this component declares documentation and the consumer needed a library
Other compatible attributes:
Doesn't say anything about its target Java version (required compatibility with Java 8)
Doesn't say anything about its elements (required them packaged as a jar)
Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
2) 也尝试了降级 Gradle 和 AGP 版本。 3) 清除了 Gradle 缓存,然后完全删除了 .gradle 文件夹。 4) 重启了 Android Studio 并清除了所有缓存。 5) 尝试将版本从 1.8 升级到 17。 6) 添加了 Gradle 工具、Maven 中心和 Google 依赖项。 7) 我在 Stack Overflow、GitHub 和其他网站上搜索了答案。