无法将 Dagger 2 依赖项添加到 java 模块

Can't add Dagger 2 dependency to java module

我正在尝试将我的 android 应用程序分成几个模块。例如,我想要 2 个额外的模块——Core 和 ViewModels。它们都是纯 java 模块。但是,在将 Dagger 2 依赖项添加到那些 java 模块时,我遇到了麻烦。这是其中一个模块的构建 gradle 文件

apply plugin: 'java-library'


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.dagger:dagger:2.15'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
    implementation 'org.greenrobot:eventbus:3.1.1'
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

但是,当我同步 gradle 时,出现以下错误

Could not find method classpath() for arguments [com.neenbedankt.gradle.plugins:android-apt:1.8] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File

知道为什么会这样吗?

我已经解决了这个问题。这是自定义模块的最终 build.gradle 的样子

plugins {
    id "net.ltgt.apt" version "0.15"
}
apply plugin: 'java-library'



dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.dagger:dagger:2.15'
    apt 'com.google.dagger:dagger-compiler:2.13'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

Dagger 2 成功生成了必要的代码,应用程序运行起来就像变魔术一样。