cashapp / InflationInject

Constructor-inject views during XML layout inflation
Apache License 2.0
904 stars 55 forks source link

error: @AssistedModule's @Module must not include #160

Closed MaximVod closed 3 years ago

MaximVod commented 3 years ago

I have already tried all the approaches proposed before, but:

Task :app:kaptDebugKotlin FAILED
C:\Users\[directory]di\AssistedInjectModule.java:9: error: @AssistedModule's @Module must not include AssistedInject_AssistedInjectModule if @InstallIn is used
public abstract interface AssistedInjectModule {
                ^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

I did everything the same as the Google sunflower application

View Model:

class ExerciseDetailViewModel @AssistedInject constructor(
    private val exercisesRepository: ExercisesRepository,
    @Assisted private val exercise: Exercise)
    : ViewModel() {

.......

    @AssistedInject.Factory
    interface AssistedFactory {
        fun create(exercise: Exercise): ExerciseDetailViewModel
    }

    companion object {
        fun provideFactory(
            assistedFactory: AssistedFactory,
            exercise: Exercise 
        ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                return assistedFactory.create(exercise) as T
            }
        }
    }

In Fragment:

@Inject
    lateinit var exerciseDetailViewModelFactory: ExerciseDetailViewModel.AssistedFactory

    private val mViewModel: ExerciseDetailViewModel by viewModels {
        ExerciseDetailViewModel.provideFactory(
            exerciseDetailViewModelFactory,
            args.Exercise
        )
    }

And:

@InstallIn(FragmentComponent::class)
@AssistedModule
@Module(includes = [AssistedInject_AssistedInjectModule::class])
// Needed until AssistedInject is incorporated into Dagger
interface AssistedInjectModule

Gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "name"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    kapt {
        javacOptions {
            option("-Adagger.validateTransitiveComponentDependencies=DISABLED")
        }
    }

    buildFeatures {
        viewBinding true
    }

    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }

    dataBinding {
        enabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    //Room
    def room_version = "2.2.5"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"

    //Hilt
    implementation "com.google.dagger:hilt-android:2.29.1-alpha"
    implementation "androidx.hilt:hilt-common:1.0.0-alpha02"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
    compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:0.6.0"
    kapt "com.squareup.inject:assisted-inject-processor-dagger2:0.6.0"
    kapt "com.google.dagger:hilt-compiler:2.29.1-alpha"
    kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02"
    androidTestImplementation "com.google.dagger:hilt-android-testing:2.29.1-alpha"
    kaptAndroidTest "com.google.dagger:hilt-compiler:2.29.1-alpha"

    def dagger_version = '2.28.1'
    implementation "com.google.dagger:dagger-android:$dagger_version"
    implementation "com.google.dagger:dagger-android-support:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"

and others....
MaximVod commented 3 years ago

Problem solved Factory and AssistedInjectModule removed and made other versions of dependencies com.squareup.inject:assisted-inject-annotations-dagger2:0.5.2