Android - Kotlin - Dagger - 没有这样的方法异常

时间:2017-07-20 18:51:10

标签: android android-gradle kotlin dagger-2

今天我在新的Android Studio Canary(测试版)中创建了项目。 我试图添加Dagger 2框架,但我有一些麻烦这样做。 首先,我添加了一些依赖项:

compile 'com.google.dagger:dagger:2.11'
kapt 'com.google.dagger:dagger-compiler:2.11'
provided 'org.glassfish:javax.annotation:10.0-b28'
apply plugin: 'kotlin-kapt' (top of file)

只是为了我测试的需要等等我创建了类:

class TempClass {}

接下来我做了一些示例模块

@Module
class TempClassModule {
    @Singleton
    @Provides
    fun provideTempClass() : TempClass = TempClass() 
}

和组件:

@Component (modules = arrayOf(TempClassModule::class))
interface TempClassComponent {
    fun inject(mainActivity: MainActivity)
}

我的应用类:

class MyApp : Application() {
    companion object {
        lateinit var tempComponent : TempClassComponent
    }

    override fun onCreate() {
        super.onCreate()
        prepareComponents()
       }

    fun prepareComponents():Unit {
        tempComponent = DaggerTempClassComponent.builder().build()
    }
}

主要活动:

class MainActivity : AppCompatActivity() {

    @Inject lateinit var tempClass : TempClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        MyApp.tempComponent.inject(this)
    }
}

编译gradle文件时一切正常。但是当我尝试重建项目以便Dagger可以构建库时会发生错误:

*Error:Execution failed for task ':app:kaptDebugKotlin'.
> Internal compiler error. See log for more details*

当然,导入像DaggerTempClassComponent这样的类是不可能的,因为它不存在。当我在MainActivity类中对变量进行注释时,一切顺利,并且有DaggerTempClassComponent。 (//@Inject lateinit var tempClass : TempClass

我还检查了日志,看看发生了什么,但在我看来那里没有多少:

2017-07-20 20:33:21,295 [se-884-b01]   INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar] 
2017-07-20 20:33:21,306 [thread 262]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from /Applications/Android Studio 3.0 Preview.app/Contents/jre/jdk/Contents/Home 
2017-07-20 20:33:21,423 [thread 262]   INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n  buildscript {\n    repositories {\n      maven { url '/Applications/Android Studio 3.0 Preview.app/Contents/gradle/m2repository'}\n    }\n  }\n  repositories {\n      maven { url '/Applications/Android Studio 3.0 Preview.app/Contents/gradle/m2repository'}\n  }\n}\n" 
2017-07-20 20:33:21,423 [thread 262]   INFO - ild.invoker.GradleBuildInvoker - Build command line options: [--configure-on-demand, -Pandroid.injected.invoked.from.ide=true, -Pandroid.injected.generateSourcesOnly=true, --init-script, /private/var/folders/jh/wjxwkfzx2cgd0z5pqhksy_sw0000gn/T/sync.local.repo3273.gradle] 
2017-07-20 20:33:21,423 [thread 262]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: --configure-on-demand -Pandroid.injected.invoked.from.ide=true -Pandroid.injected.generateSourcesOnly=true --init-script /private/var/folders/jh/wjxwkfzx2cgd0z5pqhksy_sw0000gn/T/sync.local.repo3273.gradle 
2017-07-20 20:33:23,196 [se-884-b01]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished in 1s 888ms 
2017-07-20 20:33:23,236 [se-884-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2017-07-20 20:33:23,267 [thread 259]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:23,296 [thread 259]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 29ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:23,548 [se-884-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2017-07-20 20:33:24,050 [thread 264]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:24,066 [thread 264]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 16ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:24,066 [thread 264]   INFO - indexing.UnindexedFilesUpdater - Unindexed files update started: 7 files to update 
2017-07-20 20:33:24,168 [thread 264]   INFO - .diagnostic.PerformanceWatcher - Unindexed files update took 102ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:27,871 [se-884-b01]   INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [clean, :app:assembleDebug] 
2017-07-20 20:33:27,876 [thread 264]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from /Applications/Android Studio 3.0 Preview.app/Contents/jre/jdk/Contents/Home 
2017-07-20 20:33:27,982 [thread 264]   INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n  buildscript {\n    repositories {\n      maven { url '/Applications/Android Studio 3.0 Preview.app/Contents/gradle/m2repository'}\n    }\n  }\n  repositories {\n      maven { url '/Applications/Android Studio 3.0 Preview.app/Contents/gradle/m2repository'}\n  }\n}\n" 
2017-07-20 20:33:27,983 [thread 264]   INFO - ild.invoker.GradleBuildInvoker - Build command line options: [--configure-on-demand, -Pandroid.injected.invoked.from.ide=true, --init-script, /private/var/folders/jh/wjxwkfzx2cgd0z5pqhksy_sw0000gn/T/sync.local.repo2419.gradle] 
2017-07-20 20:33:27,983 [thread 264]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: --configure-on-demand -Pandroid.injected.invoked.from.ide=true --init-script /private/var/folders/jh/wjxwkfzx2cgd0z5pqhksy_sw0000gn/T/sync.local.repo2419.gradle 
2017-07-20 20:33:30,823 [thread 253]   INFO - roid.sdk.MessageBuildingSdkLog - com.android.ide.common.blame.Message.<init>(com.android.ide.common.blame.Message$Kind, java.lang.String, java.lang.String, com.google.common.collect.ImmutableList) 
java.lang.NoSuchMethodException: com.android.ide.common.blame.Message.<init>(com.android.ide.common.blame.Message$Kind, java.lang.String, java.lang.String, com.google.common.collect.ImmutableList)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper$simpleMessageConstructor$2.invoke(KotlinOutputParserHelper.kt:171)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper$simpleMessageConstructor$2.invoke(KotlinOutputParserHelper.kt:143)
    at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.getSimpleMessageConstructor(KotlinOutputParserHelper.kt)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createNewMessage(KotlinOutputParserHelper.kt:272)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createMessage(KotlinOutputParserHelper.kt:250)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createMessage$default(KotlinOutputParserHelper.kt:244)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelperKt.parse(KotlinOutputParserHelper.kt:66)
    at org.jetbrains.kotlin.android.KotlinOutputParser.parse(KotlinOutputParser.java:28)
    at com.android.ide.common.blame.parser.ToolOutputParser.parseToolOutput(ToolOutputParser.java:86)
    at com.android.tools.idea.gradle.output.parser.BuildOutputParser.parseGradleOutput(BuildOutputParser.java:43)
    at com.android.tools.idea.gradle.project.build.invoker.GradleTasksExecutorImpl.lambda$collectMessages$5(GradleTasksExecutorImpl.java:487)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.run(ApplicationImpl.java:334)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
2017-07-20 20:33:30,824 [thread 253]   INFO - roid.sdk.MessageBuildingSdkLog - Exception from KotlinOutputParser 
2017-07-20 20:33:30,824 [thread 253]   INFO - roid.sdk.MessageBuildingSdkLog - com.android.ide.common.blame.Message.<init>(com.android.ide.common.blame.Message$Kind, java.lang.String, java.lang.String, com.google.common.collect.ImmutableList) 
java.lang.NoSuchMethodException: com.android.ide.common.blame.Message.<init>(com.android.ide.common.blame.Message$Kind, java.lang.String, java.lang.String, com.google.common.collect.ImmutableList)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper$simpleMessageConstructor$2.invoke(KotlinOutputParserHelper.kt:171)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper$simpleMessageConstructor$2.invoke(KotlinOutputParserHelper.kt:143)
    at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.getSimpleMessageConstructor(KotlinOutputParserHelper.kt)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createNewMessage(KotlinOutputParserHelper.kt:272)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createMessage(KotlinOutputParserHelper.kt:250)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelper.createMessage$default(KotlinOutputParserHelper.kt:244)
    at org.jetbrains.kotlin.android.KotlinOutputParserHelperKt.parse(KotlinOutputParserHelper.kt:41)
    at org.jetbrains.kotlin.android.KotlinOutputParser.parse(KotlinOutputParser.java:28)
    at com.android.ide.common.blame.parser.ToolOutputParser.parseToolOutput(ToolOutputParser.java:86)
    at com.android.tools.idea.gradle.output.parser.BuildOutputParser.parseGradleOutput(BuildOutputParser.java:43)
    at com.android.tools.idea.gradle.project.build.invoker.GradleTasksExecutorImpl.lambda$collectMessages$5(GradleTasksExecutorImpl.java:487)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.run(ApplicationImpl.java:334)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
2017-07-20 20:33:30,825 [thread 253]   INFO - roid.sdk.MessageBuildingSdkLog - Exception from KotlinOutputParser 
2017-07-20 20:33:30,831 [se-884-b01]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished with 1 error(s) in 2s 948ms 
2017-07-20 20:33:30,883 [se-884-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2017-07-20 20:33:30,912 [thread 264]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:30,945 [thread 264]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 32ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:31,143 [se-884-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2017-07-20 20:33:31,681 [thread 267]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:31,706 [thread 267]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 25ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:33:31,706 [thread 267]   INFO - indexing.UnindexedFilesUpdater - Unindexed files update started: 2 files to update 
2017-07-20 20:33:31,824 [thread 267]   INFO - .diagnostic.PerformanceWatcher - Unindexed files update took 118ms; general responsiveness: ok; EDT responsiveness: ok 
2017-07-20 20:37:26,379 [J pool 1/4]   INFO - attrs.AttributeDefinitionsImpl - Found tag with unknown parent: AndroidManifest.AndroidManifestSupportsInput 
2017-07-20 20:37:26,379 [J pool 1/4]   INFO - attrs.AttributeDefinitionsImpl - Found tag with unknown parent: AndroidManifest.AndroidManifestCompatibleScreens 

运行./gradlew build clean --stacktrace

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:243)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:236)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:225)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:124)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:80)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:105)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:99)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:99)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
  Caused by: org.gradle.api.GradleException: Internal compiler error. See log for more details
    at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:9)
    at org.jetbrains.kotlin.gradle.internal.KaptTask.compile(KaptTask.kt:53)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.doExecute(DefaultTaskClassInfoStore.java:141)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:121)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:711)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:694)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:122)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:111)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    ... 27 more

有没有人有类似的情况或者可能知道发生了什么? 我还下载了一些带有kotlin的工作匕首2的示例应用程序 - 除语言版本外,一切看起来都一样,而且它在那里工作。

Android Studio Canary发布7 Dagger 2版本:2.11 Kotlin版本:&#39; 1.1.3-2&#39;

建议

 //                          download your android tools by SDK manager
//                v--------- check the version here
compileSdkVersion 23// |
buildToolsVersion     "23.0.3"

0 个答案:

没有答案