【问题标题】:Cannot find symbol class DataBindingComponent caused by java.util.NoSuchElementException找不到由 java.util.NoSuchElementException 引起的符号类 DataBindingComponent
【发布时间】:2018-10-27 08:52:21
【问题描述】:

我有一个项目,我在 android studio 3.3 Beta 1 中使用匕首版本 2.18 (implementation "com.google.dagger:dagger:2.18") 但是当我运行它时,它失败并出现以下错误

e/../../databinding/ActivityHomeBinding.java:46: error: cannot find symbol
      @Nullable DataBindingComponent component) {
                ^
  symbol:   class DataBindingComponent
  location: class ActivityHomeBinding
i: Note: [1] Wrote GeneratedAppGlideModule with: []
e: [kapt] An exception occurred: java.util.NoSuchElementException
        at com.sun.tools.javac.util.List$2.next(List.java:432)
        at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:302)
        at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:254)
        at dagger.android.processor.AndroidMapKeys.mapKeyValue(AndroidMapKeys.java:75)
        at dagger.android.processor.AndroidMapKeys.lambda$annotationsAndFrameworkTypes$5(AndroidMapKeys.java:56)
        at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1321)
        at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
        at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419)

为了解决问题,我试过了

What went wrong: Execution failed for task ':app:kaptGenerateStubsDebugKotlin'. Could not resolve all files for configuration ':app:kapt'. Failed to transform file 'jetifier-core-1.0.0-alpha10.jar' to match attributes {artifactType=processed-jar} using transform JetifyTransform Failed to transform '/home/s/.gradle/caches/modules-2/files-2.1/com.android.tools.build.jetifier/jetifier-core/1.0.0-alpha10/9eb7027c383061de12f93aae7a22cbeb97832d2a/jetifier-core-1.0.0-alpha10.jar' using Jetifier. Reason: The given artifact contains a string literal with a package reference 'android/support/v4' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.. (Run with --stacktrace for more details.) To disable Jetifier, set android.enableJetifier=false in your gradle.properties file.

  • 使用匕首版本 2.16 仍然失败并出现奇怪的错误

    e: /home/s/G/app/src/main/java/com/edijae/crusar/sample/di/modules/ApiModule.kt: (11, 16): Unresolved reference: logging
    e: /home/s/G/app/src/main/java/com/edijae/crusar/sample/di/modules/ApiModule.kt: (40, 40): Unresolved reference: HttpLoggingInterceptor
    e: /home/s/G/app/src/main/java/com/edijae/crusar/sample/di/modules/ApiModule.kt: (41, 35): Unresolved reference: HttpLoggingInterceptor
    

然而,当我导航到 ApiModule.kt 时,没有错误。那就是import okhttp3.logging.HttpLoggingInterceptor 没有以红色突出显示,我可以去那堂课。

以下是我的主要build.gradle

 buildscript {
        ext.kotlin_version = '1.2.71'
        ext.realmVersion ='5.4.2'
        repositories {
            google()
            jcenter()
            maven {
                url "https://maven.google.com"
            }
            maven { url "https://jitpack.io" }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "io.realm:realm-gradle-plugin:$realmVersion"

            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            google()
            jcenter()
            maven {
                url "https://maven.google.com"
            }
            maven { url "https://jitpack.io" }
        }
    }

    /**
     * Configure common tasks on all the submodules
     */
    allprojects { project ->

        // Get versions, this is where we store things
        // like the list of submodules or the version
        project.apply from: "$rootDir/versions.gradle"

        gradle.projectsEvaluated {
            tasks.withType(JavaCompile.class) {
                options.compilerArgs << "-Xmaxerrs" << "10000"
            }
        }
        afterEvaluate {
            if (project.plugins.hasPlugin("kotlin-kapt")) {
                kapt {
                    javacOptions {
                        option("-Xmaxerrs", 10000)
                    }
                }
            }
        }
    }
    task clean(type: Delete) {
        delete rootProject.buildDir
    }  

下面是我应用的build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'maven'
apply plugin: 'realm-android'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled true
    }
        compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    //kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //glide
    kapt "com.github.bumptech.glide:compiler:4.8.0"
    implementation "com.github.bumptech.glide:glide:4.8.0"

    //dagger
    implementation "com.google.dagger:dagger:2.18"
    kapt "com.google.dagger:dagger-compiler:2.18"
    implementation "com.google.dagger:dagger-android:2.18"
    implementation "com.google.dagger:dagger-android-support:2.18"

    // if you use the support libraries
    kapt "com.google.dagger:dagger-android-processor:2.18"

    //retrofit
    implementation "com.squareup.retrofit2:retrofit:2.4.0"
    implementation "com.squareup.retrofit2:converter-gson:2.4.0"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
    debugImplementation "com.squareup.okhttp3:logging-interceptor:3.8.0"


     testImplementation 'junit:junit:4.12'
     androidTestImplementation "androidx.test:runner:1.0.0"
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

     }

【问题讨论】:

    标签: android gradle kotlin dagger-2 kotlin-android-extensions


    【解决方案1】:

    我设法解决了这个错误

    • debugImplementation "com.squareup.okhttp3:logging-interceptor:3.8.0" 更改为implementation "com.squareup.okhttp3:logging-interceptor:3.8.0"。这解决了Unresolved reference: HttpLoggingInterceptor 错误。

    • 在我的主build.gradle添加jetifier处理器

      classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02'

    • 在我的 app's build.gradle 中添加 jentifier 核心

      kapt 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02' annotationProcessor 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'

    主级 build.gradle

    buildscript {
        ext.kotlin_version = '1.3.0'
        ext.realmVersion ='5.4.2'
        repositories {
            google()
            jcenter()
            maven {
                url "https://maven.google.com"
            }
            maven { url "https://jitpack.io" }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
            classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
            maven {
                url "https://maven.google.com"
            }
            maven { url "https://jitpack.io" }
        }
    }
    

    应用的 build.gradle

    apply plugin: 'com.android.application'
    
    apply plugin: 'kotlin-android'
    
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'
    apply plugin: 'maven'
    apply plugin: 'realm-android'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
            vectorDrawables.useSupportLibrary = true
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            renderscriptTargetApi 19
            renderscriptSupportModeEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        dataBinding {
            enabled true
        }
            compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
    
        kapt 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'
        annotationProcessor 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'
    
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    
        kapt "com.github.bumptech.glide:compiler:4.8.0"
        implementation "com.github.bumptech.glide:glide:4.8.0"
    
        implementation "com.google.dagger:dagger:2.18"
        kapt "com.google.dagger:dagger-compiler:2.18"
        implementation "com.google.dagger:dagger-android:2.18"
        implementation "com.google.dagger:dagger-android-support:2.18"
    
        // if you use the support libraries
        kapt "com.google.dagger:dagger-android-processor:2.18"
    
        implementation "com.squareup.retrofit2:retrofit:2.4.0"
        implementation "com.squareup.retrofit2:converter-gson:2.4.0"
        implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
        implementation "com.squareup.okhttp3:logging-interceptor:3.8.0"
    
    
         testImplementation 'junit:junit:4.12'
         androidTestImplementation "androidx.test:runner:1.0.0"
         androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    
         }
    

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题
      原来这个错误与 Jetifier 错误有关
      见:
      https://github.com/google/dagger/issues/1245
      https://issuetracker.google.com/issues/115738511

      解决方案:暂时坚持使用 dagger 2.16

      implementation "com.google.dagger:dagger-android:2.16"  
      implementation "com.google.dagger:dagger-android-support:2.16"  
      kapt "com.google.dagger:dagger-compiler:2.16"  
      kapt "com.google.dagger:dagger-android-processor:2.16"  
      

      【讨论】:

      • 我正在使用匕首 2.16,但事情仍在分崩离析
      • 将此添加到您的 gradle.properties android.enableJetifier=true android.useAndroidX=true org.gradle.jvmargs=-Xmx1024m。如果仍然无法正常工作,请在此处发布您所有应用的 build.gradle
      • 我已经在我的 gradle.properties 中设置了这些。我已经在问题中包含了我的app's build.gradle 的缺失部分。
      • 删除行实现“com.google.dagger:dagger:2.18”并将它们全部转换为2.16
      • 那行不通。它产生了我的问题中列出的其他错误
      猜你喜欢
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2019-01-06
      • 1970-01-01
      • 2019-06-27
      • 2011-11-04
      相关资源
      最近更新 更多