From b2560ec7c4243c402f0f8f3fb19c35a2fbb74233 Mon Sep 17 00:00:00 2001 From: Louis CAD Date: Fri, 6 Aug 2021 11:39:16 +0200 Subject: [PATCH] Expose AppCtxInitializer and update its doc --- modules/appctx/build.gradle.kts | 1 + .../splitties/init/AppCtxInitializer.kt | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/modules/appctx/build.gradle.kts b/modules/appctx/build.gradle.kts index 527043f2..6010ac5d 100644 --- a/modules/appctx/build.gradle.kts +++ b/modules/appctx/build.gradle.kts @@ -20,6 +20,7 @@ kotlin { androidMain.dependencies { api(AndroidX.annotation) implementation(AndroidX.startup.runtime) + implementation(splitties("experimental")) } } } diff --git a/modules/appctx/src/androidMain/kotlin/splitties/init/AppCtxInitializer.kt b/modules/appctx/src/androidMain/kotlin/splitties/init/AppCtxInitializer.kt index fae5d90a..9f3c8242 100644 --- a/modules/appctx/src/androidMain/kotlin/splitties/init/AppCtxInitializer.kt +++ b/modules/appctx/src/androidMain/kotlin/splitties/init/AppCtxInitializer.kt @@ -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 { +@ExperimentalSplittiesApi +class AppCtxInitializer : Initializer { override fun create(context: Context): AppCtxInitializer { context.injectAsAppCtx() return this