找不到由java.util.NoSuchElementException引起的符号类DataBindingComponent

时间:2018-10-27 08:52:21

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

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

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'

     }

2 个答案:

答案 0 :(得分:3)

我设法解决了错误

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

  • 在我的主要build.gradle中添加喷射器处理器

    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'

     }

答案 1 :(得分:0)

我遇到了同样的问题
证明此错误与Jetifier错误有关
参见:
https://github.com/google/dagger/issues/1245
https://issuetracker.google.com/issues/115738511

解决方案:暂时坚持使用匕首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"