我是Android Studio的新手,这可能是一个非常基本的问题,但我想更好地了解正在发生的事情。
我试图从一个示例项目(https://github.com/android/architecture-samples- main分支)中复制一些功能。该代码使用javax.inject库中的@Singleton和@Inject注解。我不明白的是这个库是如何添加到项目中的。
安卓Studio Flamingo| 2022.2.1补丁2
示例项目Gradle版本:7.5
我的项目Gradle版本:8.0
示例项目gradle.build(:app)文件:
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kapt)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
}
android {
compileSdk = libs.versions.compileSdk.get().toInt()
defaultConfig {
applicationId = "com.example.android.architecture.blueprints.main"
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "com.example.android.architecture.blueprints.todoapp.CustomTestRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments += "room.incremental" to "true"
}
}
}
buildTypes {
getByName("debug") {
isMinifyEnabled = false
isTestCoverageEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
testProguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguardTest-rules.pro")
}
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
testProguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguardTest-rules.pro")
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests {
isIncludeAndroidResources = true
all { test ->
with(test) {
testLogging {
events = setOf(
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED,
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_OUT,
org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_ERROR,
)
}
}
}
}
buildFeatures {
compose = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
packagingOptions {
excludes += "META-INF/AL2.0"
excludes += "META-INF/LGPL2.1"
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.androidxComposeCompiler.get()
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
freeCompilerArgs += "-opt-in=kotlin.Experimental"
}
}
}
/*
Dependency versions are defined in the top level build.gradle file. This helps keeping track of
all versions in a single place. This improves readability and helps managing project complexity.
*/
dependencies {
// App dependencies
implementation(libs.androidx.annotation)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.timber)
implementation(libs.androidx.test.espresso.idling.resources)
// Architecture Components
implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.compiler)
implementation(libs.androidx.lifecycle.runtimeCompose)
implementation(libs.androidx.lifecycle.viewModelCompose)
// Hilt
implementation(libs.hilt.android.core)
implementation(libs.androidx.hilt.navigation.compose)
kapt(libs.hilt.compiler)
// Jetpack Compose
val composeBom = platform(libs.androidx.compose.bom)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.compiler)
implementation(composeBom)
implementation(libs.androidx.compose.foundation.core)
implementation(libs.androidx.compose.foundation.layout)
implementation(libs.androidx.compose.animation)
implementation(libs.androidx.compose.material.core)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.lifecycle.runtimeCompose)
implementation(libs.androidx.lifecycle.viewModelCompose)
implementation(libs.accompanist.appcompat.theme)
implementation(libs.accompanist.swiperefresh)
debugImplementation(composeBom)
debugImplementation(libs.androidx.compose.ui.tooling.core)
debugImplementation(libs.androidx.compose.ui.test.manifest)
// Dependencies for local unit tests
testImplementation(composeBom)
testImplementation(libs.junit4)
testImplementation(libs.androidx.archcore.testing)
testImplementation(libs.kotlinx.coroutines.android)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.androidx.navigation.testing)
testImplementation(libs.androidx.test.espresso.core)
testImplementation(libs.androidx.test.espresso.contrib)
testImplementation(libs.androidx.test.espresso.intents)
testImplementation(libs.google.truth)
testImplementation(libs.androidx.compose.ui.test.junit)
// JVM tests - Hilt
testImplementation(libs.hilt.android.testing)
kaptTest(libs.hilt.compiler)
// Dependencies for Android unit tests
androidTestImplementation(composeBom)
androidTestImplementation(libs.junit4)
androidTestImplementation(libs.kotlinx.coroutines.test)
androidTestImplementation(libs.androidx.compose.ui.test.junit)
// AndroidX Test - JVM testing
testImplementation(libs.androidx.test.core.ktx)
testImplementation(libs.androidx.test.ext)
testImplementation(libs.androidx.test.rules)
testImplementation(project(":shared-test"))
// AndroidX Test - Instrumented testing
androidTestImplementation(libs.androidx.test.core.ktx)
androidTestImplementation(libs.androidx.test.ext)
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.room.testing)
androidTestImplementation(libs.androidx.archcore.testing)
androidTestImplementation(libs.androidx.navigation.testing)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(libs.androidx.test.espresso.contrib)
androidTestImplementation(libs.androidx.test.espresso.intents)
androidTestImplementation(libs.androidx.test.espresso.idling.resources)
androidTestImplementation(libs.androidx.test.espresso.idling.concurrent)
androidTestImplementation(project(":shared-test"))
// AndroidX Test - Hilt testing
androidTestImplementation(libs.hilt.android.testing)
kaptAndroidTest(libs.hilt.compiler)
}
我修复了Android Studio抛出的“unresolved reference inject”错误,方法是简单地手动将“implementation 'javax.inject:javax.inject:1'”添加到我项目中的build.gradle(:app)文件中的dependencies块中,但在示例项目的build.gradle文件中没有任何此类内容或任何明确的javax或inject引用,Android Studio也没有找到它。
我还尝试简单地从示例项目中复制所有依赖项,然后在尝试构建项目时得到“cannot get unknown property 'libs'”。我认为这可能取决于这几行代码:
/**
* Retrieves the [libs][org.gradle.accessors.dm.LibrariesForLibs] extension.
*/
val org.gradle.api.Project.`libs`: org.gradle.accessors.dm.LibrariesForLibs get() =
(this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("libs") as org.gradle.accessors.dm.LibrariesForLibs
/**
* Configures the [libs][org.gradle.accessors.dm.LibrariesForLibs] extension.
*/
fun org.gradle.api.Project.`libs`(configure: Action<org.gradle.accessors.dm.LibrariesForLibs>): Unit =
(this as org.gradle.api.plugins.ExtensionAware).extensions.configure("libs", configure)
在C:\Users\Owner.gradle\caches\7.5\kotlin-dsl\accessors\01 fe 2 eed 4467 b292 cb 55 edf 16 f388 a3 c\sources\org\gradle\kotlin\dsl\Accessors 9 lduusevit 01 i1 zyt 8 g4 j1 zrv.kt文件中的某个地方,但我真的不确定这意味着什么。
1条答案
按热度按时间py49o6xq1#
您链接的项目使用Gradle Version Catalog声明依赖项。依赖项在
gradle/libs.version.toml
文件中声明,而不是在build.gradle
文件中声明。我认为你没有正确设置版本目录。请检查this version catalog file并将其添加到您的项目中。javax.inject.Inject
依赖性由Java JDK提供。您不需要为它添加任何依赖项。Hilt在后台使用它进行依赖注入,而您已经有了Hilt dependencies。