2017-07-28 74 views
16

我目前正在使用InstantApps进行试验,并且希望将匕首加入到我的项目中。使用Dagger的应用程序组件构建Android即时应用程序

我正面临设置应用程序AppComponent的问题。我的应用程序组件包括我的应用程序的所有功能匕首模块。

我主要有:

  • 一个基本应用模块含有我的应用程序类
  • 多个特征每个活动每个匕首模块,全部用碱作为依赖性。
  • 一个应用程序模块和即时模块都导入所有功能和基本应用程序模块。

我想在添加即时应用程序模块之前弄清楚设置。

来自InstantApps文档和项目示例。看起来应用程序类需要在Base中。从Dagger文档到安装匕首:

DaggerYourAppComponent.create().inject(this); 

应包含在您的应用程序类中。然而,这似乎是不可能的,因为AppComponent需要引用所有的功能匕首模块。

问题是:

  • 我应该在哪里加我AppComponent匕首模块?
  • 我是否应该将应用程序保留在应用程序模块中而不是在Base中?
  • 任何GitHub回购或Dagger附带Instant应用程序的文档?

谢谢

+0

从这[InstantApps兼容图书馆](https://developer.android.com/topic/instant-apps/prepare.html#identify_tested_compatible_libraries)链接,也许匕首不仅仅支持... –

+0

该链接仅适用于Google图书馆,不适用于第三方-派对。 – TWL

+0

你可以检查这个网址https://github.com/ragdroid/instant-app-dagger它可以帮助你。 –

回答

9
  • Dagger2是非常有即时的应用程序支持。您可以为每个要素模块和相应的Dagger提供者类创建组件类,以公开每个要素模块的组件类实例。
  • 每个模块组件类都可以为仅包含在该特征模块中的类声明注入方法。
  • 此外,您还可以在 基本模块中的应用程序组件类中进行应用程序范围注入。
  • 应用程序组件类可以在包含在基本模块中的 应用程序类中实例化,并通过应用程序类中的静态方法公开给其他 功能模块。

下面是一个示例代码Dagger2注入与即时应用程序,使事情更清晰。 https://github.com/willowtreeapps/android-instant-apps-demo

+0

感谢您使用@Vishy。我认为你是对的,我需要每个功能一个组件。不过,我仍然在寻找一个使用android dagger支持的示例,以及来自[here](https://google.github.io/dagger/android)的'AndroidInjection.inject(MyActivity.this)'和AndroidInjector。 HTML)。有关更新您的样品的任何计划? –

+0

更具体地说,在[tutorial](https://google.github.io/dagger/android.html#injecting-activity-objects)的第3步: '@Component(modules = {... ,YourActivityModule.class}) 接口YourApplicationComponent {}' 这一步不可行,因为我的应用程序组件对ActivityModule没有可见性,因为它在特征模块中。 –

0

我写了一篇关于这方面的文章,其中有很多细节:Dagger2 for Modular Architecture,但下面简短的回答。

你必须以不同的方式使用Dagger2。您无需为每个功能模块使用模块或子组件,而需要使用具有对基本AppComponent的依赖关系的组件。

在一个模块中,我们通常是这样做:

@Singleton 
@Component(modules = arrayOf(NetworkModule::class, RepositoryModule::class, 
        SubcomponentModule::class)) 
interface ApplicationComponent : AndroidInjector<MyApplication> { 
    val userRepository: UserRepository 
    val apiService: ApiService 
} 

@Module 
object NetworkModule { 
    @Provides 
    @Singleton 
    @JvmStatic 
    fun provideApiService(okHttp: OkHttp): ApiService { 
    return ApiSerive(okHttp) 
    } 
} 

但正如你说你没有访问SubComponentModule这可能是在其他功能模块的另一个模块或引用匕首模块。

您只需创建一个功能模块中根据ApplicationComponent这样一个新的匕首模块:

@Browser 
@Component(modules = [(BrowserModule::class)], 
     dependencies = [(AppComponent::class)]) 
interface BrowserComponent : AndroidInjector<AppCompatActivity> { 
    @Component.Builder 
    abstract class Builder: AndroidInjector.Builder<AppCompatActivity>(){ 
    /** 
    * explicity declare to Dagger2 
    * that this builder will accept an AppComponent instance 
    **/ 
    abstract fun plus(component: AppComponent): Builder 
    } 
} 

以及相应的功能活动将构建组件:

class BrowserActivity : AppCompatActivity() { 
    override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    DaggerBrowserComponent 
     .builder() 
     /** 
     * we have to provide an instance of the AppComponent or 
     * any other dependency for this component 
     **/ 
     .plus((application as MyApplication).component) 
     .build() 
     .inject(this) 
    } 
}