MetroX Android¶
Core Android support for Metro. This artifact specifically focuses on integration with AppComponentFactory, which requires API 28+. As a result, this artifact requires min SDK 28 since it’s useless on older versions.
Usage¶
For simple cases, all you need to do is
-
Depend on this artifact
dependencies { implementation("dev.zacsweers.metro:metrox-android:x.y.z") }
2. Make yourAppGraph(or equivalent) implementMetroAppComponentProviders.
@DependencyGraph(AppScope::class) interface AppGraph : MetroAppComponentProviders
3. Make yourApplicationsubclass implementMetroApplicationand implementappComponentProviders.
class MyApp : Application(), MetroApplication { private val appGraph by lazy { createGraph<AppGraph>() } override val appComponentProviders: MetroAppComponentProviders get() = appGraph }
Injecting Android app components¶
To constructor inject Android app components (Activity, Service, etc), annotate them with the corresponding map key and contribute them into the maps used by MetroAppComponentFactory.
For example, use @ActivityKey for an injected Activity:
@ContributesIntoMap(AppScope::class, binding<Activity>())
@ActivityKey(MainActivity::class)
class MainActivity(private val fragmentFactory: FragmentFactory): AppCompatActivity()
For other types of components, use @BroadcastReceiverKey, @ContentProviderKey, and @ServiceKey the same way.
Note that the built-in MetroAppComponentFactory uses the base Activity, Service, etc types. If you want to use your own bases (ComponentActivity, etc), you’ll need to create your own AppComponentFactory + relevant multibindings typed to the key you need.
Advanced¶
If you have your own custom AppComponentFactory, you will need to exclude the MetroX implementation in your AndroidManifest.xml via tools:replace attribute.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
...
android:appComponentFactory="your.custom.AppComponentFactory"
tools:replace="android:appComponentFactory"
>
</application>
</manifest>
Then you can replicate what MetroAppComponentFactory does or subclass it in your custom factory.