带有Instant App的Dagger 2

时间:2018-06-07 15:20:19

标签: dagger-2 android-instant-apps dagger

我已经在使用即时应用程序几天了。由于Dagger的问题,我还没有把它编译好。

项目分为baseexportpreview作为功能。

export模块

@ExportScope
@Component(
        modules = [DrawExportModule::class],
        dependencies = [AppComponent::class])
interface DrawExportComponent: AndroidInjector<LiveDrawingExportActivity> {

    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<LiveDrawingExportActivity>(){
        abstract fun plus(component: AppComponent): Builder
    }
}

AppComponent.kt

@Singleton
@Component(modules = [
    AndroidInjectionModule::class,
    AppModule::class,
    ActivityBindingModule::class,
    ServiceBindingModule::class])
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(context: Context): Builder

        fun build(): AppComponent
    }

    fun inject(app: LiveMessageApplication)
}

AppModule.kt

   @Module(includes = [(DataModule::class)])
   class AppModule {
   @Singleton
   @Provides
   fun provideStateManager(): StateManager {
        return StateManager.getInstance()
   }
}

编译错误日志

    /Users/*/features/export/build/tmp/kapt3/stubs/debug/*/ui/draw/di/DrawExportComponent.java:8: 
error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] *.ui.StateManager cannot be provided without an @Inject constructor or an @Provides-annotated method.
    public abstract interface DrawExportComponent extends dagger.android.AndroidInjector<com.ctech.livemessage.ui.draw.LiveDrawingExportActivity> {
                    ^
          *.ui.StateManager is injected at
              *.LiveDrawingExportActivity.stateManager
          *.LiveDrawingExportActivity is injected at
              dagger.android.AndroidInjector.inject(T)

在我看来,依赖组件在这种情况下不起作用。我不知道如何前进。

更多信息 碱/的build.gradle

apply plugin: 'com.android.feature'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {

    compileSdkVersion deps.build.compileSdkVersion

    baseFeature = true

    defaultConfig {
        minSdkVersion deps.build.minSdkVersion
        targetSdkVersion deps.build.targetSdkVersion

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

}

dependencies {

    application project(":installed")
    feature project(":features:export")
    feature project(":features:preview")
}

出口/的build.gradle

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

android {

    compileSdkVersion deps.build.compileSdkVersion

    defaultConfig {
        minSdkVersion deps.build.minSdkVersion
        targetSdkVersion deps.build.targetSdkVersion
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {

        release {
            debuggable true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), '../../proguard.pro'
            signingConfig signingConfigs.debug
        }

    }

    sourceSets {
        main {
            jniLibs.srcDirs 'libs'
        }
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

}

dependencies {

    implementation project(':features:base')

    kapt deps.dagger.compiler
    kapt deps.dagger.androidCompiler

}

1 个答案:

答案 0 :(得分:3)

问题是DrawExportComponent没有看到StateManager,因为后者是AppComponent的一部分。

与具有组件依赖关系的子组件不同,依赖组件无法访问所有父组件。您必须通过添加:

明确指定共享内容
fun stateManager(): StateManager

AppComponent

official documentation

中的更多信息