构建项目

时间:2017-10-04 22:18:38

标签: android dependency-injection kotlin dagger-2

当我尝试构建用于生成Dagger文件的项目时,构建失败并出现异常。 你能告诉我如何解决这个问题,或者至少看看它的方向。

  

E:   /home/hodzi/Projects/StudioProjects/StackViewer/app/build/tmp/kapt3/stubs/debug/com/hodzi/stackviewer/di/AppComponent.java:6:   错误:[com.hodzi.stackviewer.questions.di.QuestionsComponent]   com.hodzi.stackviewer.questions.di.QuestionsComponent有冲突   范围:e:

     

e:公共抽象接口AppComponent {e:^ e:
  com.hodzi.stackviewer.di.AppComponent也有@Singleton e:   java.lang.IllegalStateException:无法分析:   org.jetbrains.kotlin.kapt3.diagnostic.KaptError:错误的同时   注释处理   org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)     在   org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)     在   org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)     在   org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)     在   org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)     在   org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)     在org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)at   org.jetbrains.kotlin.daemon.CompileServiceImpl $编译$ 1 $ 2.invoke(CompileServiceImpl.kt:386)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl $编译$ 1 $ 2.invoke(CompileServiceImpl.kt:96)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl $ doCompile $$内嵌$ ifAlive $拉姆达$ 2.invoke(CompileServiceImpl.kt:889)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl $ doCompile $$内嵌$ ifAlive $拉姆达$ 2.invoke(CompileServiceImpl.kt:96)     在   org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)     在   org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)     在sun.rmi.transport.Transport $ 1.run(Transport.java:200)at   sun.rmi.transport.Transport $ 1.run(Transport.java:197)at   java.security.AccessController.doPrivileged(Native Method)at   sun.rmi.transport.Transport.serviceCall(Transport.java:196)at   sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)     在   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0(TCPTransport.java:826)     在   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.lambda $运行$ 0(TCPTransport.java:683)     在java.security.AccessController.doPrivileged(Native Method)at   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run(TCPTransport.java:682)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)引起:   org.jetbrains.kotlin.kapt3.diagnostic.KaptError:错误的同时   注释处理   org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:90)     在   org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing $默认(annotationProcessing.kt:42)     在   org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:205)     在   org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:166)     在   org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:82)at at   org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM $ analyzeFilesWithJavaIntegration $ 2.invoke(TopDownAnalyzerFacadeForJVM.kt:96)     在   org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:106)     在   org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration $默认(TopDownAnalyzerFacadeForJVM.kt:83)     在   org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler $分析$ 1.invoke(KotlinToJVMBytecodeCompiler.kt:377)     在   org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler $分析$ 1.invoke(KotlinToJVMBytecodeCompiler.kt:68)     在   org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96)     在   org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:368)     在   org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:133)     ......还有30多个

组件:

1)AppComponent

@Singleton
@Component(modules = arrayOf(AppModule::class, NetModule::class))
interface AppComponent {

    fun inject(app: App)

    fun plus(questionsModule: QuestionsModule): QuestionsComponent
}

2)QuestionsComponent

@Singleton
@Subcomponent(modules = arrayOf(QuestionsModule::class))
interface QuestionsComponent {
    fun inject(questionsActivity: QuestionsActivity)
}

模块:

1)AppModule

@Module
class AppModule(val application: Application) {

    @Provides
    @Singleton
    fun provideApplication(): Context = application

    @Provides
    @Singleton
    fun provideSharedPrefs(context: Context): SharedPreferences =
            context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)
}

2)QuestionsModule

@Module
class QuestionsModule {
    @Provides @Singleton
    fun provideQuestionsPresenter(questionsInteractor: QuestionsInteractor): QuestionsPresenter =
        QuestionsPresenter(questionsInteractor)

    @Provides @Singleton
    fun provideQuestionsInteractor(api : Api): QuestionsInteractor = QuestionsInteractor(api)
}

应用

class App : Application() {
    val appComponent: AppComponent by lazy {
        DaggerAppComponent.builder()
            .netModule(NetModule())
            .appModule(AppModule(this))
            .build()

    }

    val questionsComponent: QuestionsComponent by lazy {
        appComponent.plus(QuestionsModule())
    }

    override fun onCreate() {
        super.onCreate()
        Injector.inject(this)
        NetUtils.init(this)
    }
}

如果有帮助,我会附上指向githab

的链接

1 个答案:

答案 0 :(得分:2)

您已将AppComponentQuestionsComponent定义为@Singleton,那么哪一个负责该范围?这正是错误所说的。从其中一个中删除注释。