【问题标题】:Cannot find symbol ViewModel with Dagger 2使用 Dagger 2 找不到符号 ViewModel
【发布时间】:2018-06-19 21:36:04
【问题描述】:

我正在创建一个新的 Android 项目并添加 Dagger 2,但它失败并出现与 androidx 库相关的错误。

使用 Android Studio 中的模板后,我添加了几行代码,kotlin-kapt,以及 Dagger 2 的依赖项。

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.xxxx.yyyy"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    androidExtensions {
        experimental = true
    }
    kapt {
        generateStubs = true
    }
}



dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'


    implementation 'com.google.android.material:material:1.0.0-alpha1'

    // Image Loading
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'

    // Dagger
    implementation 'com.google.dagger:dagger-android:2.15'
    kapt 'com.google.dagger:dagger-android-processor:2.15'

    // RxJava
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.3'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'

    // Navigation
    implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01'
    implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha01'

    // Pretty Logger
    implementation 'com.orhanobut:logger:1.15'


    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha1'
}

在尝试构建应用程序时,它失败并出现以下错误。

error: cannot find symbol
public final class MainActivityViewModel extends android.arch.lifecycle.ViewModel {
                                                                       ^
    symbol:   class ViewModel
    location: package android.arch.lifecycle


error: cannot find symbol
private final androidx.lifecycle.MediatorLiveData
                                ^
    symbol:   class MediatorLiveData
    location: package androidx.lifecycle


error: cannot find symbol
private androidx.navigation.NavController navController;
                           ^
    symbol:   class NavController
    location: package androidx.navigation


 ...

我尝试过使用 androidx 库和较旧的库,但都失败了。如果我删除 Dagger 2 依赖项,它将构建并运行良好。

我目前使用的是 Android Studio 3.2 Canary 17。

知道出了什么问题吗?

谢谢。

【问题讨论】:

  • 请出示build.gradle(module:app)
  • @LwinMyoAung 我添加了 build.gradle 的其余部分。
  • 当然,您的错误日志与androidxnon-androidx 依赖项混合在一起。我认为您应该检查您的build.gradle(module:app)
  • 你能在 github 上发布一个使用该设置的简单项目吗?我去看看。

标签: android android-support-library dagger-2 androidx


【解决方案1】:

转到设置 -> 构建、执行、部署 -> 编译器, 取消选中“按需配置”选项(如果已设置)。 还要确保您使用的是最新的 gradle 和 Android Gradle 插件

【讨论】:

  • 我使用tools.build:gradle:3.2.0-beta01gradle-4.7gradle-wrapper.properties。我禁用了按需配置,但问题仍然存在。
  • 我当前的配置是com.android.tools.build:gradle:3.1.3 for android gradle plugin 和gradle-4.4-all.zip for gradle
  • 是的,看来它可能与 Android Studios 3.2 结合使用。
  • 所以。有什么适合你的吗?请更新答案,以便其他人参考。
  • 我目前正在研究它,试图找出一些东西。
【解决方案2】:

我设法找到了问题的原因,它与 build.gradle 本身无关,而是与我使用的 Java 版本有关。

在 Android Studios 中,我已将 JDK 更改为使用 Java 10。

但是,将此设置更改回使用嵌入式 JDK 已使应用程序编译成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多