Home > Back-end >  Dagger throwing errors after Kotlin integration in a java code
Dagger throwing errors after Kotlin integration in a java code

Time:11-17

Android project Code base is full java and dagger 2 is implemented in java as well. I am integrating kotlin in the code and each time I rebuild, Dagger gives errors. (Added below)

code:

build.gradle (project)

buildscript {
    ext {
        kotlin_version = "1.7.10"
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (app)

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-kapt'
    
    android {
        compileSdkVersion 32
    
        defaultConfig {
            applicationId "co.xyz.abc"
            minSdkVersion 21
            targetSdkVersion 32
            versionCode 15
            versionName "1.1.1"
            multiDexEnabled true
            renderscriptTargetApi 19
            renderscriptSupportModeEnabled true
            vectorDrawables.useSupportLibrary = true
        }
    
    
        buildTypes {
         // ...
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = '1.8'
        }
        buildFeatures {
            viewBinding true
        }
    }
    
    dependencies {
        // AndroidX
       // ....

        //kotlin
        implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
        implementation 'androidx.core:core-ktx:1.9.0'
    
        // ViewModel
        def lifecycle_version = "2.4.1"
        implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    
        // Dagger2
        def daggerVersion = "2.35.1"
        api "com.google.dagger:dagger:$daggerVersion"
        annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
    
        // Dagger Android
        api "com.google.dagger:dagger-android-support:$daggerVersion"
        api "com.google.dagger:dagger-android:$daggerVersion"
        annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
    
        // AssistedInject
        def assistedInject = '0.6.0'
        compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:$assistedInject"
        annotationProcessor "com.squareup.inject:assisted-inject-processor-dagger2:$assistedInject"
    
        androidTestImplementation 'junit:junit:4.13.2'
}

gradle.properties

    android.enableJetifier=true
    android.useAndroidX=true
    org.gradle.jvmargs=-Xmx1536m
    kotlin.code.style=official

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip 

Errors after Rebuild:

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:checkStagingLocalDevAarMetadata'. A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction 3 issues were found when checking AAR metadata:
  1. Dependency 'androidx.core:core-ktx:1.9.0' requires libraries and applications that depend on it to compile against version 33 or later of the Android APIs.

    :app is currently compiled against android-32.

    Also, the maximum recommended compile SDK version for Android Gradle plugin 7.2.1 is 32.

    Recommended action: Update this project's version of the Android Gradle plugin to one that supports 33, then update this project to use compileSdkVerion of at least 33.

    Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on).

  2. Dependency 'androidx.core:core:1.9.0' requires libraries and applications that depend on it to compile against version 33 or later of the Android APIs.

    :app is currently compiled against android-32.

    Also, the maximum recommended compile SDK version for Android Gradle plugin 7.2.1 is 32.

    Recommended action: Update this project's version of the Android Gradle plugin to one that supports 33, then update this project to use compileSdkVerion of at least 33.

    Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on).

  3. Dependency 'androidx.annotation:annotation-experimental:1.3.0' requires libraries and applications that depend on it to compile against version 33 or later of the Android APIs.

    :app is currently compiled against android-32.

    Also, the maximum recommended compile SDK version for Android Gradle plugin 7.2.1 is 32.

    Recommended action: Update this project's version of the Android Gradle plugin to one that supports 33, then update this project to use compileSdkVerion of at least 33.

    Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on).

When I change compileSdk to 33 :

Task :app:compileStagingLocalDevJavaWithJavac

error: cannot find symbol import co.abc.client.di.components.DaggerABCApplicationComponent; ^ symbol: class DaggerABCApplicationComponent location: package co.abc.client.di.components

Now, if I downgrade the core-ktx version to 1.8.0 and change annotationProcessor to kapt :

Task :app:kaptStagingLocalDevKotlin /StudioProjects/project/app/src/main/java/co/abc/client/di/modules/account/AssistedModule.java:8: error: cannot find symbol @Module(includes = AssistedInject_AssistedModule.class) ^ symbol: class AssistedInject_AssistedModule /StudioProjects/project/app/src/main/java/co/abc/client/di/modules/account/AssistedModule.java:9: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code. public interface AssistedModule {} ^ /StudioProjects/project/app/src/main/java/co/abc/client/di/components/ABCApplicationComponent.java:40: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code. public interface ABCApplicationComponent extends AndroidInjector { ^

Task :app:kaptStagingLocalDevKotlin FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:kaptStagingLocalDevKotlin'. A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction java.lang.reflect.InvocationTargetException (no error message)

What could be the issue here? Should the Dagger-related code be written in Kotlin? Is there a source I could refer to for kotlin integration in java code base for android when dagger is there?

So far, I have tried these which didn't work:

  1. upgrading and downgrading versions for kotlin and dagger.
  2. changing all annotationProcessors to kapt
  3. adding this to gradle - -Pandroid.incrementalJavaCompile=false
  4. android { compileOptions.incremental = false }
  5. restart ide

Also, I have another project(java code base and kotlin integration setup) which is working with these versions so the issue doesn't seem to be with that. only difference is, dagger is not in my other project and everything runs fine there.

CodePudding user response:

try using the latest version of the dagger in both Gradle

//Hilt
implementation 'com.google.dagger:hilt-android:2.42'
kapt 'com.google.dagger:hilt-android-compiler:2.42'
kapt "androidx.hilt:hilt-compiler:1.0.0"

in App level

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'

CodePudding user response:

Maybe it is a version issue

downgrade your kotlin version and then check it

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.30'
implementation 'androidx.core:core-ktx:1.8.0'
  • Related