我正在尝试在Android中使用Dagger 2。实施非常简单。以下是与匕首2相关的Component
和Module
s:
ApplicationComponent.kt
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface ApplicationComponent {
fun inject(app: Application)
fun inject(mainActivity: MainActivity)
fun plusMainAcitvityComponent(mainActivityModule: MainActivityModule): MainActivityComponent
}
MainActivityComponent.kt
@Subcomponent(modules = arrayOf(MainActivityModule::class, GoogleApiClientModule::class))
interface MainActivityComponent{
fun inject(mainActivity: MainActivity)
}
AppModule.kt
@Module
class AppModule(val app: Application){
@ApplicationContext
@Provides
fun providesApplicationContext() = app
}
GoogleApiClientModule.kt
@Module
class GoogleApiClientModule{
companion object {
val TAG = "GoogleApiClientModule"
}
@Singleton
@Provides
fun providesGoogleApiClient(@ActivityContext activity: MainActivity, connectionFailedListener: OnConnectionFailedListener) =
GoogleApiClient.Builder(activity)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(activity, connectionFailedListener)
.build()
@Singleton
@Provides
fun providesConnectionFailedListener() = OnConnectionFailedListener {
Log.d(TAG, "GAC connection has failed")
}
}
GooglePlacesClientModule.kt
@Module
class GooglePlacesClientModule {
@Provides
fun providesGeoDataClient(@ActivityContext activity: MainActivity) = Places.getGeoDataClient(activity, null)
}
MainActivityModule.kt
@Module
class MainActivityModule(val activity: MainActivity){
@ActivityContext
@Provides
fun providesActivityContext() = activity
}
以下是我的主要活动,我正在注入上述组件:
class MainActivity : AppCompatActivity(), OnMapReadyCallback {
lateinit var binding: ActivityMainBinding
lateinit var mapFragment: SupportMapFragment
@Inject lateinit var app: App
@Inject lateinit var mainViewModelFactory: MainViewModelFactory
lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Data binding
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//Dagger inject
App.appComponent.inject(this)
app.plustMainAcitvityComponent(this)?.inject(this)
//Viewmodel init
mainViewModel = ViewModelProviders.of(this, mainViewModelFactory).get(MainViewModel::class.java)
//Ui init
setSupportActionBar(binding.toolbarLayout?.toolbar)
setupBottomSheetLayout()
setupGoogleMap()
}
----
---
}
下面是我的App
,其中Dagger已初始化:
class App: Application(){
var mainActivityComponent: MainActivityComponent? = null
companion object {
lateinit var appComponent: ApplicationComponent
}
override fun onCreate() {
super.onCreate()
setupDagger()
}
fun setupDagger(){
appComponent = DaggerApplicationComponent.builder().appModule(AppModule(this)).build()
}
fun plustMainAcitvityComponent(activity: MainActivity) : MainActivityComponent?{
if(mainActivityComponent == null) {
mainActivityComponent = appComponent.plusMainAcitvityComponent(MainActivityModule(activity))
}
return mainActivityComponent
}
fun clearMainActivityComponent(){
mainActivityComponent = null
}
}
当我运行应用程序时,我收到内部编译器错误。以下是我的IDE日志:
2017-11-29 11:54:39,953 [J pool 2/4] WARN - hes.resolve.KotlinCacheService - 找不到正确的模块信息。 原因:分析类型类com.android.tools.idea.databinding.LightGeneratedComponentClass的元素,不包含文件 文本: 空值 2017-11-29 11:54:39,953 [J pool 2/4] WARN - .resolve.jvm.JvmAnalyzerFacade - Java从LibraryInfo引用null(libraryName = com.android.databinding:library-1.3.1) 引用的类是:JavaClassImpl:DATA绑定组件类
注意:MainActivityComponent是ApplicationComponent的SubComponent。尽管ApplicationComponent目前只有一个模块,但将来会添加更多模块
答案 0 :(得分:0)
我现在无法理解错误消息。我可以看到有两件事可以让它在这里出错 1)要将子组件添加到父组件,请将子组件类添加到父组件安装的@Module的子组件属性中。然后可以从父
中请求子组件的构建器
@Module(subcomponents = XYZ.class)
class AppModule(val app: Application){
2)此外,您为子组件和组件提供相同的范围,除非分辨率没有歧义,否则不允许这样做。 请阅读此处的文档 https://google.github.io/dagger/subcomponents.html
答案 1 :(得分:-1)
您的错误与Dagger无关,但与Android的数据绑定库无关。你在用它吗?如果没有,请将其从依赖项中删除,然后重试?