Skip to content

Commit

Permalink
Expose AppCtxInitializer and update its doc
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Aug 6, 2021
1 parent 74cf244 commit b2560ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions modules/appctx/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ kotlin {
androidMain.dependencies {
api(AndroidX.annotation)
implementation(AndroidX.startup.runtime)
implementation(splitties("experimental"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,38 @@
package splitties.init

import android.app.Application
import android.content.ContentProvider
import android.content.Context
import androidx.startup.InitializationProvider
import androidx.annotation.Keep
import androidx.startup.Initializer
import splitties.experimental.ExperimentalSplittiesApi

/**
* Initializes [appCtx] so it can be used where any [Context] can be used.
*
* If you use [appCtx] in another process than the default one. This will not be invoked.
* The library will attempt to initialize [appCtx] using reflection instead. If you want to avoid
* this, you can declare a subclass of this class in your manifest with the name of the process
* properly specified to get it Automatically initialized. Alternatively, you can in your properly
* declared custom [Application] call [injectAsAppCtx] passing `this` reference from the constructor
* or the [Application.onCreate] method.
* If you use [appCtx] in an AndroidX App Startup [Initializer], directly or not,
* make sure to add this class among its list of [dependencies].
*
* If you use [appCtx] in another process than the default one this will not be invoked.
*
* If you use [appCtx] in a [ContentProvider] that is initialized before
* the one from AndroidX App Startup ([androidx.startup.InitializationProvider][InitializationProvider]),
* this will not be invoked in time.
*
* The easiest way to fix that is, in your properly declared custom [Application], to call [injectAsAppCtx],
* passing `this` reference from the constructor or the [Application.onCreate] method.
*
* In the case of a [ContentProvider], calling [injectAsAppCtx] in its [ContentProvider.onCreate] function
* will also work, but we strongly recommend you to use
* [AndroidX App Startup instead](https://developer.android.com/topic/libraries/app-startup),
* for performance reasons since each [ContentProvider] delays app startup, and these add-up,
* resulting in a poor UX.
*/
@Keep // Needed because there's no default R8/proguard rules for App Startup Initializers.
@Suppress("unused") // False positive. Used in the manifest, but tooling analysis is incomplete.
internal class AppCtxInitializer : Initializer<AppCtxInitializer> {
@ExperimentalSplittiesApi
class AppCtxInitializer : Initializer<AppCtxInitializer> {
override fun create(context: Context): AppCtxInitializer {
context.injectAsAppCtx()
return this
Expand Down

0 comments on commit b2560ec

Please sign in to comment.