如何在Android Studio的Java库模块中使用Dagger? [英] How to use Dagger in Java library module in Android Studio?
问题描述
我正在Android Studio项目的Java库模块中使用Dagger,这是我为该模块的 build.gradle
的样子:
I'm using Dagger in a Java library module in an Android Studio project and here's what my build.gradle
for the module looks like:
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.dagger:dagger:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
}
sourceCompatibility = "7"
targetCompatibility = "7"
我可以看到Dagger正在正确生成实现,并且它们存在于 build/generated/sources/annotationProcessor
中,但是由于某些原因,我无法在代码中访问它们.另外,生成的文件在 package
语句中显示一条错误,指出:
I can see that the Dagger is properly generating implementations and they are present in build/generated/sources/annotationProcessor
but for some reason I cannot access them in the code. Also, the generated files shows an error at the package
statement that states:
包名称"com.example.javamodule"与文件路径"java.main.com.example.javamodule"不对应
Package name "com.example.javamodule" does not correspond to the file path "java.main.com.example.javamodule"
我在这里有两个问题.首先,如何在我的Java模块代码中访问Dagger生成的类,其次,如何从生成的类中删除上述错误?
I have two questions here. First, how can I access the Dagger generated classes in my java module code and second, how to remove the above-mentioned error from the generated classes?
推荐答案
在您的Java库的gradle文件中:
In your java library's gradle file:
plugins {
id 'java-library'
id 'kotlin'
id 'kotlin-kapt'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//Dependency injection
implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.24'
}
然后创建一个类及其依赖项:
Then create a class and its dependencies:
class First
@Inject
constructor(
private val second: Second,
private val third: Third
) {
fun firstFunction() {
print(second.secondMessage())
print(third.name)
}
}
class Second(
private val name: String
) {
fun secondMessage(): String {
return name
}
}
class Third(
val name: String
)
然后创建您的匕首模块:
Then create your dagger module:
@Module
class ModuleUtil {
@Provides
fun providesSecond(): Second {
return Second("second")
}
@Provides
fun providesThird(): Third {
return Third("third")
}
}
然后创建您的匕首组件:
Then create your dagger component:
@Singleton
@Component(modules = [
ModuleUtil::class
])
interface MainComponent {
fun maker(): First
}
用于处理生成的组件的对象:
An object to handle the generated component:
object DaggerWrapper {
lateinit var mainComponent: MainComponent
fun initComponent() {
mainComponent = DaggerMainComponent
.builder()
.build()
}
}
最后在您的应用android模块中(例如,在Activity中):
And finally in your app android module(eg. inside an Activity):
DaggerWrapper.initComponent()
val mainComponent = DaggerWrapper.mainComponent
val first = mainComponent.maker()
first.firstFunction()
这篇关于如何在Android Studio的Java库模块中使用Dagger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!