{"id":208,"date":"2025-03-03T17:46:17","date_gmt":"2025-03-03T09:46:17","guid":{"rendered":"http:\/\/www.devblog.cn\/?p=208"},"modified":"2025-03-03T17:46:18","modified_gmt":"2025-03-03T09:46:18","slug":"jetpack-startup%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90-2","status":"publish","type":"post","link":"http:\/\/www.devblog.cn\/?p=208","title":{"rendered":"Jetpack-Startup\u6e90\u7801\u89e3\u6790"},"content":{"rendered":"<h1>Startup\u6e90\u7801\u89e3\u6790<\/h1>\n<blockquote><p>\n  \u6e90\u7801\u7248\u672c\uff1a<br \/>\n  * <strong>Startup<\/strong>\uff1a1.2.0<br \/>\n  * <strong>\u66f4\u65b0\u65f6\u95f4<\/strong>\uff1a2025-3\u6708<\/p>\n<p>  <strong>\u5bfc\u822a\uff1a<\/strong><br \/>\n  * <a href=\"https:\/\/juejin.cn\/post\/7041081752126750756\">Jetpack-Lifecycle\u6e90\u7801\u89e3\u6790<\/a><br \/>\n  * <a href=\"https:\/\/juejin.cn\/post\/7037010631236534286\">Jetpack-LiveData\u6e90\u7801\u89e3\u6790<\/a><br \/>\n  * <a href=\"https:\/\/juejin.cn\/post\/7047313364992655396\">Jetpack-ViewModel\u6e90\u7801\u8be6\u89e3<\/a><br \/>\n  * <a href=\"https:\/\/juejin.cn\/post\/7052236629515173919\/\">Jetpack-Startup\u6e90\u7801\u8be6\u89e3<\/a><br \/>\n  * \u66f4\u591a\u7684\u6587\u7ae0\u770b\u8fd9\u91cc\uff1a<a href=\"https:\/\/juejin.cn\/user\/4450479132652679\/posts\">\u4e3b\u9875<\/a>\n<\/p><\/blockquote>\n<h1>\u4f7f\u7528<\/h1>\n<h2>\u5b9e\u73b0Initializer<\/h2>\n<pre><code class=\"language-kotlin\">class WorkManagerInitializer : Initializer&lt;WorkManager&gt; {\n\n    override fun create(context: Context): WorkManager {\n        \/\/ \u521d\u59cb\u5316WorkManager\n        val configuration = Configuration.Builder().build()\n        WorkManager.initialize(context, configuration)\n        \/\/ \u8fd4\u56deWorkManager\u521d\u59cb\u5316\u540e\u7684\u5b9e\u4f8b\uff0c\u4ee5\u4fbf\u901a\u8fc7AppInitializer.getInstance(context).initializeComponent(WorkManagerInitializer::class.java)\u521d\u59cb\u5316\u5e76\u83b7\u53d6\u3002\n        return WorkManager.getInstance(context)\n    }\n\n    override fun dependencies(): List&lt;Class&lt;out Initializer&lt;*&gt;&gt;&gt; {\n        \/\/ \u65e0\u4f9d\u8d56\u5176\u5b83\u5e93\n        return emptyList()\n    }\n}\n<\/code><\/pre>\n<p>\u56e0\u4e3a<code>WorkManager<\/code>\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u5176\u5b83\u5e93\uff0c\u6240\u4ee5\u8be5<code>dependencies(<\/code>)\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<strong>\u7a7a\u5217\u8868<\/strong>\u3002<\/p>\n<pre><code class=\"language-kotlin\">class ExampleLoggerInitializer : Initializer&lt;ExampleLogger&gt; {\n    override fun create(context: Context): ExampleLogger {\n        \/\/ \u521d\u59cb\u5316ExampleLogger\uff0c\u5e76\u8fd4\u56de\u5176\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u901a\u8fc7AppInitializer.getInstance(context).initializeComponent(ExampleLoggerInitializer::class.java)\u521d\u59cb\u5316\u5e76\u83b7\u53d6\u3002\n        return ExampleLogger(WorkManager.getInstance(context))\n    }\n\n    override fun dependencies(): List&lt;Class&lt;out Initializer&lt;*&gt;&gt;&gt; {\n        \/\/ \u4f9d\u8d56WorkManagerInitializer\n        return listOf(WorkManagerInitializer::class.java)\n    }\n}\n<\/code><\/pre>\n<p>\u56e0\u4e3a<code>ExampleLogger<\/code>\u4f9d\u8d56\u4e8e<code>WorkManager<\/code>\uff0c\u6240\u4ee5\u8be5<code>dependencies(<\/code>)\u65b9\u6cd5\u8fd4\u56de<strong>\u5305\u542b<\/strong><code>WorkManagerInitializer<\/code>\u7684<strong>\u5217\u8868<\/strong>\uff0c\u4f7f\u5176\u5728\u5b83<strong>\u4e4b\u524d\u521d\u59cb\u5316<\/strong>\u3002<\/p>\n<h2>\u521d\u59cb\u5316Initializer<\/h2>\n<h3>\u81ea\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/h3>\n<p><strong>\u5728\u6e05\u5355\u6587\u4ef6\u4e2d\u6dfb\u52a0<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;provider\n    android:name=\"androidx.startup.InitializationProvider\"\n    android:authorities=\"${applicationId}.androidx-startup\"\n    android:exported=\"false\"\n    tools:node=\"merge\"&gt;\n    &lt;meta-data  \n        android:name=\"com.example.ExampleLoggerInitializer\" \/\/ \u8981\u521d\u59cb\u5316\u7684Initializer\u7c7b\u540d\u5168\u8def\u5f84\n        android:value=\"androidx.startup\" \/&gt;\n&lt;\/provider&gt;\n<\/code><\/pre>\n<p><strong>\u81ea\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/strong>\uff0c\u5176\u4e3a<strong>\u5728\u5e94\u7528\u542f\u52a8\u65f6\u521d\u59cb\u5316\u7ec4\u4ef6<\/strong>\u3002\u4e0d\u9700\u8981\u4e3a<code>WorkManagerInitializer<\/code>\u6dfb\u52a0<code>&lt;meta-data&gt;<\/code>\u6761\u76ee\uff0c\u56e0\u4e3a\u5b83\u662f<code>ExampleLoggerInitializer<\/code>\u7684<strong>\u4f9d\u8d56\u9879<\/strong>\uff0c\u5f53\u7136\u58f0\u660e\u4e86\u4e5f\u4e0d\u4f1a\u6709\u95ee\u9898\uff08\u56e0\u4e3a\u5df2\u7ecf\u88ab\u521d\u59cb\u4e86\uff0c\u5219\u4e0d\u4f1a\u518d\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff0c\u8be6\u7ec6\u770b\u6e90\u7801\u4ecb\u7ecd\uff09\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. \u8be5<code>provider<\/code>\u7684<strong>\u58f0\u660e\u683c\u5f0f\u56fa\u5b9a<\/strong>\uff0c\u53ea\u9700\u8981\u4fee\u6539<code>&lt;meta-data&gt;<\/code>\u7684<code>name<\/code>\u5373\u53ef\u3002<br \/>\n  2. \u8be5<code>&lt;meta-data&gt;<\/code>\u7684<strong>\u987a\u5e8f<\/strong>\uff0c\u51b3\u5b9a\u7740<strong>\u521d\u59cb\u5316\u7684\u987a\u5e8f<\/strong>\u3002<br \/>\n  3. \u8be5<code>tools:node=\"merge\"<\/code>\u5c5e\u6027\u786e\u4fdd\u6e05\u5355\u5408\u5e76\u5de5\u5177<strong>\u6b63\u786e\u89e3\u51b3\u4efb\u4f55\u51b2\u7a81\u6761\u76ee<\/strong>\u3002\n<\/p><\/blockquote>\n<h3>\u624b\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/h3>\n<p><strong>\u624b\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/strong>\uff0c\u5176\u610f\u4e3a\u5c31\u662f<strong>\u4e0d\u81ea\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/strong>\uff0c\u6240\u4ee5\u60a8<strong>\u5fc5\u987b\u505c\u7528\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\uff0c\u5176\u53c8\u79f0\u4e3a<strong>\u5ef6\u8fdf\u521d\u59cb\u5316<\/strong>\u3002<\/p>\n<h4>\u505c\u7528\u5355\u4e2a\u7ec4\u4ef6\u7684\u81ea\u52a8\u521d\u59cb\u5316<\/h4>\n<p>\u8981\u505c\u7528\u5355\u4e2a\u7ec4\u4ef6\u7684\u81ea\u52a8\u521d\u59cb\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;provider\n    android:name=\"androidx.startup.InitializationProvider\"\n    android:authorities=\"${applicationId}.androidx-startup\"\n    android:exported=\"false\"\n    tools:node=\"merge\"&gt;\n    &lt;meta-data android:name=\"com.example.ExampleLoggerInitializer\"\n              tools:node=\"remove\" \/&gt;\n&lt;\/provider&gt;\n<\/code><\/pre>\n<p>\u8bf7<strong>\u79fb\u9664<\/strong>\u6e05\u5355\u4e2d\u8be5\u7ec4\u4ef6\u7684\u521d\u59cb\u5316\u7a0b\u5e8f\u7684 <code>&lt;meta-data&gt;<\/code> \u6761\u76ee\uff0c<strong>\u6216\u8005<\/strong>\u60a8\u53ef\u4ee5\u5728\u6761\u76ee\u4e2d\u4f7f\u7528 <code>tools:node=\"remove\"<\/code>\uff0c\u4ee5\u786e\u4fdd\u5408\u5e76\u5de5\u5177\u8fd8\u4f1a\u5c06\u76f8\u5e94\u6761\u76ee\u4ece<strong>\u5176\u4ed6\u6240\u6709\u5217\u8868\u4e2d\u5220\u9664<\/strong>\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. <strong>\u505c\u7528\u7ec4\u4ef6\u7684\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\u4e5f\u4f1a\u505c\u7528<strong>\u8be5\u7ec4\u4ef6\u7684\u4f9d\u8d56\u9879<\/strong>\u7684<strong>\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\u3002<br \/>\n  2. <code>tools:node=\"remove\"<\/code>\uff0c\u4ece<strong>\u5408\u5e76\u540e\u7684\u6e05\u5355<\/strong>\u4e2d<strong>\u79fb\u9664<\/strong>\u6b64\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<h4>\u505c\u7528\u6240\u6709\u7ec4\u4ef6\u7684\u81ea\u52a8\u521d\u59cb\u5316<\/h4>\n<p>\u8981\u505c\u7528\u6240\u6709\u81ea\u52a8\u521d\u59cb\u5316\uff0c\u8bf7\u5220\u9664 <code>InitializationProvider<\/code>\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;provider\n    android:name=\"androidx.startup.InitializationProvider\"\n    android:authorities=\"${applicationId}.androidx-startup\"\n    tools:node=\"remove\" \/&gt;\n<\/code><\/pre>\n<p>\u505c\u7528<strong>\u6240\u6709\u7ec4\u4ef6<\/strong>\u7684\u81ea\u52a8\u521d\u59cb\u5316\uff0c\u5c06<code>tools:node=\"remove\"<\/code>\u58f0\u660e\u5230<code>InitializationProvider<\/code>\u7684<code>provider<\/code>\u4e0a\u5373\u53ef\uff0c\u5176\u5b83\u4fdd\u6301\u4e0d\u53d8\u6216\u5220\u9664\u6389\u5168\u90e8<code>&lt;meta-data&gt;<\/code>\u90fd\u53ef\u4ee5\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. <strong>\u4e0d\u63a8\u8350<\/strong>\u505c\u7528<strong>\u6240\u6709\u7ec4\u4ef6<\/strong>\u7684\u81ea\u52a8\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u4f1a<strong>\u505c\u7528\u6389\u6240\u6709<\/strong>\u4f7f\u7528<code>startup<\/code>\u5e93<strong>\u521d\u59cb\u5316<\/strong>\u7684<strong>\u4e09\u65b9\u5e93<\/strong>\uff08\u5982<code>lifecycle<\/code>\u5e93\u7684<code>ProcessLifecycleInitializer<\/code>\uff09\uff0c\u5bfc\u81f4\u5f97\u9700\u8981<strong>\u624b\u52a8<\/strong>\u521d\u59cb\u5316<strong>\u6240\u6709<\/strong>\u4f7f\u7528<code>startup<\/code>\u5e93\u7684\u4e09\u65b9\u5e93\uff0c\u4e0d\u65b9\u4fbf\u540e\u7eed\u7ef4\u62a4\u3002\n<\/p><\/blockquote>\n<h4>\u624b\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/h4>\n<pre><code class=\"language-kotlin\">val result = AppInitializer.getInstance(context)\n    .initializeComponent(ExampleLoggerInitializer::class.java)\n<\/code><\/pre>\n<p><strong>\u624b\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/strong>\uff0c\u8c03\u7528<code>AppInitializer.initializeComponent()<\/code>\u65b9\u6cd5\u5373\u53ef\uff0c\u4ee5\u4e0a\u4ee3\u7801\u624b\u52a8\u521d\u59cb\u5316<code>ExampleLogger<\/code>\uff0c\u53c8\u56e0\u4e3a<code>ExampleLogger<\/code>\u4f9d\u8d56<code>WorkManager<\/code>\uff0c\u6240\u4ee5<code>WorkManager<\/code>\u4e5f\u5b8c\u6210\u4e86\u521d\u59cb\u5316\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. \u5176\u8fd4\u56de\u503c<code>result<\/code>\u4e3a<code>ExampleLoggerInitializer<\/code>\u7684<code>create()<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684<code>ExampleLogger<\/code>\u5b9e\u4f8b\u3002<br \/>\n  2. \u5982\u679c<code>AppInitializer.initializeComponent()<\/code>\u65b9\u6cd5\u521d\u59cb\u5316\u7684<code>Initializer<\/code>\u5df2\u7ecf\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u518d\u6b21\u8c03\u7528\u5219\u4e0d\u4f1a\u518d\u6b21\u521d\u59cb\u5316\uff0c\u800c\u662f\u4f1a\u8fd4\u56de<strong>\u7b2c\u4e00\u6b21\u521d\u59cb\u5316<\/strong>\u7684<strong>\u7ed3\u679c\u503c<\/strong>\uff08\u5982\u521d\u59cb\u5316<code>ExampleLoggerInitializer<\/code>\uff0c\u59cb\u7ec8\u4f1a\u8fd4\u56de<strong>\u7b2c\u4e00\u6b21\u521d\u59cb\u5316<\/strong>\u7684<code>ExampleLogger<\/code>\u5b9e\u4f8b\uff09\u3002\n<\/p><\/blockquote>\n<h1>\u6e90\u7801<\/h1>\n<h2>\u5b9e\u73b0Initializer<\/h2>\n<p><strong>Initializer\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public interface Initializer&lt;T&gt; {\n\n    \/\/ \u521d\u59cb\u5316\u7ec4\u4ef6\n    @NonNull\n    T create(@NonNull Context context);\n\n    \/\/ \u8fd9\u4e2aInitializer\u4f9d\u8d56\u7684\u4f9d\u8d56\u9879\u5217\u8868\u3002\u8fd9\u662f\u7528\u6765\u786e\u5b9aInitializer\u7684\u521d\u59cb\u5316\u987a\u5e8f\u3002\n    \/\/ \u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u521d\u59cb\u5316\u5668 B \u5b9a\u4e49\u4e86\u53e6\u4e00\u4e2a\u521d\u59cb\u5316\u5668 A \u4f5c\u4e3a\u5b83\u7684\u4f9d\u8d56\uff0c\u90a3\u4e48 A \u5728 B \u4e4b\u524d\u88ab\u521d\u59cb\u5316\u3002\n    @NonNull\n    List&lt;Class&lt;? extends Initializer&lt;?&gt;&gt;&gt; dependencies();\n}\n<\/code><\/pre>\n<p><code>Initializer<\/code>\u7c7b\uff0c\u4e3a<strong>\u521d\u59cb\u5316\u5668\u63a5\u53e3<\/strong>\uff0c\u8fd9\u4e2a\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e24\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>create()<\/code>\u65b9\u6cd5\uff0c\u5b83\u5305\u542b<strong>\u521d\u59cb\u5316\u7ec4\u4ef6\u6240\u9700\u7684\u6240\u6709\u64cd\u4f5c<\/strong>\uff0c\u5e76\u8fd4\u56de<strong>T\u7684\u5b9e\u4f8b<\/strong>\u3002<\/li>\n<li><code>dependencies()<\/code>\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de<code>Initializer<\/code><strong>\u6240\u4f9d\u8d56\u7684\u5176\u5b83<\/strong><code>Initializer&lt;T&gt;<\/code>\u5bf9\u8c61\u7684<strong>\u5217\u8868<\/strong>\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5<strong>\u63a7\u5236<\/strong><code>app<\/code>\u5728<strong>\u542f\u52a8\u65f6<\/strong>\u8fd0\u884c<code>initializers<\/code>\u7684<strong>\u987a\u5e8f<\/strong>\u3002<\/li>\n<\/ul>\n<h2>\u521d\u59cb\u5316Initializer<\/h2>\n<h3>\u81ea\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/h3>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b<code>startup<\/code>\u5e93\u7684<strong>\u6e05\u5355\u6587\u4ef6<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    package=\"androidx.startup\" &gt;\n\n    &lt;uses-sdk android:minSdkVersion=\"21\" \/&gt;\n\n    &lt;application&gt;\n        &lt;provider\n            android:name=\"androidx.startup.InitializationProvider\"\n            android:authorities=\"${applicationId}.androidx-startup\"\n            android:exported=\"false\"\n            tools:node=\"merge\" \/&gt;\n    &lt;\/application&gt;\n\n&lt;\/manifest&gt;\n<\/code><\/pre>\n<p>\u91cc\u9762\u58f0\u660e\u4e86<strong>\u6700\u5c0fSDK\u7248\u672c<\/strong>\u4e3a<strong>21<\/strong>\uff0c\u4ee5\u53ca\u58f0\u660e\u4e86\u4e00\u4e2a<code>ContentProvider<\/code>\uff0c\u5b83\u662f<code>InitializationProvider<\/code>\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b<code>InitializationProvider<\/code>\u7c7b\u3002<\/p>\n<p><strong>InitializationProvider\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class InitializationProvider extends ContentProvider {\n\n    @Override\n    public final boolean onCreate() {\n        Context context = getContext();\n        if (context != null) {\n            Context applicationContext = context.getApplicationContext();\n            if (applicationContext != null) {                AppInitializer.getInstance(context).discoverAndInitialize(getClass());\n            } else {\n                StartupLogger.w(\"Deferring initialization because `applicationContext` is null.\");\n            }\n        } else {\n            throw new StartupException(\"Context cannot be null\");\n        }\n        return true;\n    }\n\n    @Nullable\n    @Override\n    public final Cursor query(\n            @NonNull Uri uri,\n            @Nullable String[] projection,\n            @Nullable String selection,\n            @Nullable String[] selectionArgs,\n            @Nullable String sortOrder) {\n        throw new IllegalStateException(\"Not allowed.\");\n    }\n\n    @Nullable\n    @Override\n    public final String getType(@NonNull Uri uri) {\n        throw new IllegalStateException(\"Not allowed.\");\n    }\n\n    @Nullable\n    @Override\n    public final Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {\n        throw new IllegalStateException(\"Not allowed.\");\n    }\n\n    @Override\n    public final int delete(\n            @NonNull Uri uri,\n            @Nullable String selection,\n            @Nullable String[] selectionArgs) {\n        throw new IllegalStateException(\"Not allowed.\");\n    }\n\n    @Override\n    public final int update(\n            @NonNull Uri uri,\n            @Nullable ContentValues values,\n            @Nullable String selection,\n            @Nullable String[] selectionArgs) {\n        throw new IllegalStateException(\"Not allowed.\");\n    }\n}\n<\/code><\/pre>\n<p><strong>InitializationProvider\u7c7b<\/strong>\uff0c\u5b83\u662f\u4e00\u4e2a<code>ContentProvider<\/code>\uff0c\u5728\u5176<code>onCreate()<\/code>\u65b9\u6cd5\u91cc\u9762\u8c03\u7528\u4e86<code>AppInitializer.getInstance(context).discoverAndInitialize()<\/code>\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b<code>AppInitializer.getInstance(context)<\/code>\u65b9\u6cd5\uff0c\u7136\u540e\u518d\u770b\u4e00\u4e0b\u5176<code>discoverAndInitialize()<\/code>\u65b9\u6cd5\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. <code>Application<\/code>\u3001<code>ContentProvider<\/code>\u3001<code>Activity<\/code>\u7684<code>onCreate()<\/code>\u6267\u884c\u987a\u5e8f\uff1a<code>Application.attachBaseContext()<\/code> -> <code>ContentProvider.onCreate()<\/code> -> <code>Application.onCreate()<\/code> -> <code>Activity.onCreate()<\/code>\u3002\n<\/p><\/blockquote>\n<p><strong>AppInitializer &#8211;> getInstance\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">public final class AppInitializer {\n\n    \/\/ \u5355\u4f8bAppInitializer\u5b9e\u4f8b\n    private static volatile AppInitializer sInstance;\n\n    \/\/ \u83b7\u53d6\u5355\u4f8bAppInitializer\u7684\u5b9e\u4f8b\n    @NonNull\n    @SuppressWarnings(\"UnusedReturnValue\")\n    public static AppInitializer getInstance(@NonNull Context context) {\n        if (sInstance == null) {\n            synchronized (sLock) {\n                if (sInstance == null) {\n                    sInstance = new AppInitializer(context);\n                }\n            }\n        }\n        return sInstance;\n    }\n\n}\n<\/code><\/pre>\n<p><code>AppInitializer.getInstance(context)<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u5355\u4f8b<\/strong>\u83b7\u53d6<code>AppInitializer<\/code>\u7684\u5b9e\u4f8b\u3002<\/p>\n<p><code>AppInitializer<\/code>\u7684\u521b\u5efa<\/p>\n<pre><code class=\"language-java\">\/\/ Tracing\nprivate static final String SECTION_NAME = \"Startup\";\n\n\/\/ \u5355\u4f8bAppInitializer\u5b9e\u4f8b\nprivate static volatile AppInitializer sInstance;\n\n\/\/ \u7ebf\u7a0b\u9501\nprivate static final Object sLock = new Object();\n\n\/\/ \u5df2\u7ecf\u521d\u59cb\u5316Initializer\u7684Map\u96c6\u5408\uff0ckey\u4e3aInitializer\u7684class\uff0cvalue\u4e3aInitializer.onCreate()\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n@NonNull\nfinal Map&lt;Class&lt;?&gt;, Object&gt; mInitialized;\n\n\/\/ \u5df2\u53d1\u73b0Initializer\u7684Set\u96c6\u5408\uff0cvalue\u4e3aInitializer\u7684class\u3002\n@NonNull\nfinal Set&lt;Class&lt;? extends Initializer&lt;?&gt;&gt;&gt; mDiscovered;\n\n@NonNull\nfinal Context mContext;\n\n\/\/ \u521b\u5efaAppInitializer\nAppInitializer(@NonNull Context context) {\n    mContext = context.getApplicationContext();\n    mDiscovered = new HashSet&lt;&gt;();\n    mInitialized = new HashMap&lt;&gt;();\n}\n<\/code><\/pre>\n<p><strong>AppInitializer &#8211;> discoverAndInitialize\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">void discoverAndInitialize(\n        @NonNull Class&lt;? extends InitializationProvider&gt; initializationProvider) {\n    try {\n        Trace.beginSection(SECTION_NAME);\n        \/\/ \u83b7\u53d6InitializationProvider\u7684ProviderInfo\n        ComponentName provider = new ComponentName(mContext, initializationProvider);\n        ProviderInfo providerInfo = mContext.getPackageManager()\n                .getProviderInfo(provider, GET_META_DATA);\n        \/\/ \u83b7\u53d6\u5176&lt;meta-data&gt;\u4fe1\u606f\n        Bundle metadata = providerInfo.metaData;\n        \/\/ \u53d1\u73b0\u5e76\u521d\u59cb\u5316\n        discoverAndInitialize(metadata);\n    } catch (PackageManager.NameNotFoundException exception) {\n        throw new StartupException(exception);\n    } finally {\n        Trace.endSection();\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u83b7\u53d6\u5230<strong>metaData<\/strong>\u4fe1\u606f\u540e\u8c03\u7528\u4e86\u4e0b\u9762\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">\/\/ \u5df2\u53d1\u73b0Initializer\u7684Set\u96c6\u5408\uff0cvalue\u4e3aInitializer\u7684class\u3002\n@NonNull\nfinal Set&lt;Class&lt;? extends Initializer&lt;?&gt;&gt;&gt; mDiscovered;   \n\n@SuppressWarnings(\"unchecked\")\nvoid discoverAndInitialize(@Nullable Bundle metadata) {\n    \/\/ \u83b7\u53d6\u5b57\u7b26\u4e32\uff0c\u503c\u4e3a\"androidx.startup\"\n    String startup = mContext.getString(R.string.androidx_startup);\n    try {\n        \/\/ \u5224\u65admetadata\u4e3a\u4e0d\u4e3anull\uff0c\u5373\u6709\u6ca1\u6709\u914d\u7f6e&lt;meta-data&gt;\uff0c\u6ca1\u914d\u7f6e\u5219\u4e0d\u5904\u7406\u3002\n        if (metadata != null) {\n            \/\/ \u521d\u59cb\u5316\u4e2dInitializer\u7684Class\u96c6\u5408\n            Set&lt;Class&lt;?&gt;&gt; initializing = new HashSet&lt;&gt;();\n            \/\/ \u83b7\u53d6metadata\u4e2d\u6240\u6709\u7684key\uff0c\u5373\u83b7\u53d6\u6240\u6709&lt;meta-data&gt;\u5185\u7684android:name\u3002\n            Set&lt;String&gt; keys = metadata.keySet();\n            \/\/ \u904d\u5386metadata\u4e2d\u6240\u6709\u7684key\n            for (String key : keys) {\n                \/\/ \u83b7\u53d6metadata\u4e2d\u7684\u503c\u5373\u83b7\u53d6&lt;meta-data&gt;\u5185\u7684android:value\u3002\n                String value = metadata.getString(key, null);\n                \/\/ \u5224\u65ad&lt;meta-data&gt;\u5185\u7684android:value\u4e3a\"androidx.startup\"\n                if (startup.equals(value)) {\n                    \/\/ \u83b7\u53d6&lt;meta-data&gt;\u5185\u7684android:name\u6307\u5b9a\u7684class\n                    Class&lt;?&gt; clazz = Class.forName(key);\n                    \/\/ \u5224\u65ad&lt;meta-data&gt;\u5185\u7684android:name\u6307\u5b9a\u7684class\u662f\u5426\u662fInitializer\u7684\u5b50\u7c7b\u3002\n                    if (Initializer.class.isAssignableFrom(clazz)) {\n                        \/\/ \u662fInitializer\u5b50\u7c7b\uff0c\u5219\u5f3a\u8f6c\u3002\n                        Class&lt;? extends Initializer&lt;?&gt;&gt; component =\n                                (Class&lt;? extends Initializer&lt;?&gt;&gt;) clazz;\n                        \/\/ \u6dfb\u52a0\u5230mDiscovered\uff08\u5df2\u53d1\u73b0\uff09\u96c6\u5408\n                        mDiscovered.add(component);\n                        \/\/ \u6253\u5370\u65e5\u5fd7\n                        if (StartupLogger.DEBUG) {\n                            StartupLogger.i(String.format(\"Discovered %s\", key));\n                        }\n                    }\n                }\n            }\n\n            \/\/ \u8bf7\u5728\u53d1\u73b0\u5b8c\u6210\u540e\u8fdb\u884c\u521d\u59cb\u5316\u3002\u8fd9\u6837\uff0cisEagerlyInitialized\u7684\u68c0\u67e5\u662f\u6b63\u786e\u7684\u3002\n            for (Class&lt;? extends Initializer&lt;?&gt;&gt; component : mDiscovered) {\n                \/\/ \u521d\u59cb\u5316component\uff0c\u5373\u521d\u59cb\u5316&lt;meta-data&gt;\u5185\u7684android:name\u6307\u5b9a\u7684class\u3002\n                doInitialize(component, initializing);\n            }\n        }\n    } catch (ClassNotFoundException exception) {\n        throw new StartupException(exception);\n    }\n}\n<\/code><\/pre>\n<p><code>AppInitializer.discoverAndInitialize()<\/code>\u65b9\u6cd5\uff0c\u4e3a<strong>\u53d1\u73b0\u5e76\u521d\u59cb\u5316<\/strong><code>Initializer<\/code>\uff0c\u627e\u5230<strong>\u6e05\u5355\u6587\u4ef6<\/strong>\u914d\u7f6e\u7684<strong>\u6240\u6709<\/strong><code>Initializer<\/code>\uff0c\u7136\u540e\u8c03\u7528<code>doInitialize()<\/code>\u65b9\u6cd5\u8fdb\u884c<strong>\u521d\u59cb\u5316<\/strong>\u64cd\u4f5c\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. <code>&lt;meta-data&gt;<\/code>\u5185\u7684<code>android:value<\/code>\uff0c\u5fc5\u987b\u4e3a<code>androidx.startup<\/code>\u3002<br \/>\n  2. <code>&lt;meta-data&gt;<\/code>\u5185\u7684<code>android:name<\/code>\uff0c\u5fc5\u987b\u4e3a<code>Initializer<\/code><strong>\u5b50\u7c7b<\/strong>\u7684<strong>\u7c7b\u540d\u5168\u8def\u5f84<\/strong>\u3002<br \/>\n  3. \u5728<strong>\u6e05\u5355\u6587\u4ef6<\/strong>\u914d\u7f6e\u7684<strong>\u6240\u6709<\/strong><code>Initializer<\/code>\uff0c\u90fd\u4f1a\u6dfb\u52a0\u5230<code>mDiscovered<\/code>\uff08\u5df2\u53d1\u73b0\uff09<strong>\u96c6\u5408<\/strong>\u3002\n<\/p><\/blockquote>\n<p>\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b<code>AppInitializer.doInitialize()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><strong>AppInitializer &#8211;> doInitialize\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u5df2\u7ecf\u521d\u59cb\u5316Initializer\u7684Map\u96c6\u5408\uff0ckey\u4e3aInitializer\u7684class\uff0cvalue\u4e3aInitializer.onCreate()\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n@NonNull\nfinal Map&lt;Class&lt;?&gt;, Object&gt; mInitialized;\n\n\/\/ \u505a\u521d\u59cb\u5316\nprivate &lt;T&gt; T doInitialize(\n        @NonNull Class&lt;? extends Initializer&lt;?&gt;&gt; component,\n        @NonNull Set&lt;Class&lt;?&gt;&gt; initializing) {\n    boolean isTracingEnabled = Trace.isEnabled();\n    try {\n        if (isTracingEnabled) {\n            Trace.beginSection(component.getSimpleName());\n        }\n        \/\/ \u5982\u679c\u8fd9\u4e2a\u7c7b\u6b63\u5728\u521d\u59cb\u5316\u4e2d\uff0c\u518d\u521d\u59cb\u5316\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002\n        if (initializing.contains(component)) {\n            String message = String.format(\n                    \"Cannot initialize %s. Cycle detected.\", component.getName()\n            );\n            throw new IllegalStateException(message);\n        }\n        \/\/ \u7ed3\u679c\n        Object result;\n        \/\/ \u5224\u65ad\u8fd9\u4e2a\u7c7b\u662f\u5426\u88ab\u521d\u59cb\u5316\u8fc7\uff0c\u9632\u6b62\u91cd\u590d\u521d\u59cb\u5316\u3002\n        if (!mInitialized.containsKey(component)) {\n            \/\/ \u6ca1\u521d\u59cb\u5316\u8fc7\uff0c\u5219\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5e76\u8bb0\u5f55\u5176create\u7684\u7ed3\u679c\u3002\n            \/\/ \u6dfb\u52a0\u5230\u6b63\u5728\u521d\u59cb\u5316\u4e2d\u7684\u96c6\u5408\uff0c\u6807\u8bb0\u6b63\u5728\u521d\u59cb\u5316\u4e2d\u3002\n            initializing.add(component);\n            try {\n                \/\/ \u53cd\u5c04\u521b\u5efa\u5bf9\u8c61\n                Object instance = component.getDeclaredConstructor().newInstance();\n                \/\/ \u5f3a\u8f6c\uff0c\u56e0\u4e3acomponent\u5b9e\u73b0\u4e86Initializer\uff0c\u6240\u4ee5\u6ca1\u95ee\u9898\u3002\n                Initializer&lt;?&gt; initializer = (Initializer&lt;?&gt;) instance;\n                \/\/ \u83b7\u53d6\u5176\u4f9d\u8d56\u7684Initializer\u5217\u8868\n                List&lt;Class&lt;? extends Initializer&lt;?&gt;&gt;&gt; dependencies =\n                        initializer.dependencies();\n\n                \/\/ \u5982\u679c\u5176\u4f9d\u8d56\u7684Initializer\u5217\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5c31\u5148\u521d\u59cb\u5316\u4f9d\u8d56\u5217\u8868\u3002\n                if (!dependencies.isEmpty()) {\n                    \/\/ \u904d\u5386\u5176\u4f9d\u8d56\u7684Initializer\u5217\u8868\n                    for (Class&lt;? extends Initializer&lt;?&gt;&gt; clazz : dependencies) {\n                        \/\/ \u5224\u65ad\u5176\u4f9d\u8d56\u7684Initializer\uff0c\u662f\u5426\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u8fc7\u3002\n                        if (!mInitialized.containsKey(clazz)) {\n                            \/\/ \u6ca1\u521d\u59cb\u5316\u8fc7\uff0c\u5219\u8fdb\u884c\u9012\u5f52\u521d\u59cb\u5316\u3002\n                            \/\/ -\u8bf4\u660e\uff1a\u5728\u6b64\u4f1a\u7b49\u5f85\u6240\u6709\u4f9d\u8d56\u5b8c\u6210\u624d\u4f1a\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002\n                            doInitialize(clazz, initializing);\n                        }\n                    }\n                }\n                \/\/ \u6253\u5370\u65e5\u5fd7\uff1a\u521d\u59cb\u5316\u4e2d\u7684\u7ec4\u4ef6\u540d\u3002\n                if (StartupLogger.DEBUG) {\n                    StartupLogger.i(String.format(\"Initializing %s\", component.getName()));\n                }\n                \/\/ \u8c03\u7528Initializer\u7684create()\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5e76\u8bb0\u5f55\u5176\u8fd4\u56de\u7ed3\u679c\u3002\n                result = initializer.create(mContext);\n                \/\/ \u6253\u5370\u65e5\u5fd7\uff1a\u521d\u59cb\u5316\u5b8c\u6210\u7684\u7ec4\u4ef6\u540d\u3002\n                if (StartupLogger.DEBUG) {\n                    StartupLogger.i(String.format(\"Initialized %s\", component.getName()));\n                }\n                \/\/ \u5728\u6b63\u5728\u521d\u59cb\u5316\u4e2d\u7684\u96c6\u5408\u4e2d\u79fb\u9664\uff0c\u6807\u8bb0\u5df2\u7ecf\u521d\u59cb\u5316\u8fc7\u3002\n                initializing.remove(component);\n                \/\/ \u5b58\u5165\u5df2\u7ecf\u521d\u59cb\u5316\u7684Initializer\uff0c\u4ee5\u53ca\u5176create()\u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u3002\n                mInitialized.put(component, result);\n            } catch (Throwable throwable) {\n                throw new StartupException(throwable);\n            }\n        } else {\n            \/\/ \u5df2\u7ecf\u521d\u59cb\u5316\u8fc7\uff0c\u5219\u83b7\u53d6\u5176create()\u7684\u7ed3\u679c\u3002\n            result = mInitialized.get(component);\n        }\n        \/\/ \u8fd4\u56deinitializer.create()\u7684\u7ed3\u679c\n        return (T) result;\n    } finally {\n        Trace.endSection();\n    }\n}\n<\/code><\/pre>\n<p><code>AppInitializer.doInitialize()<\/code>\u65b9\u6cd5\uff0c\u4e3a\u771f\u6b63\u7684<strong>\u521d\u59cb\u5316<\/strong><code>Initializer<\/code>\u7684\u65b9\u6cd5\uff0c\u5b83<strong>\u53cd\u5c04\u521b\u5efa<\/strong><code>Initializer<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176<code>create()<\/code>\u65b9\u6cd5<strong>\u901a\u77e5\u5185\u90e8\u7684\u521d\u59cb\u5316<\/strong>\uff0c\u5e76<code>mInitialized<\/code><strong>\u8bb0\u5f55<\/strong>\u5176<code>create()<\/code><strong>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c<\/strong>\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. \u5982\u679c\u8fd9\u4e2a<code>Initializer<\/code><strong>\u672a\u88ab\u521d\u59cb\u5316<\/strong>\uff0c\u5219<strong>\u53cd\u5c04\u521b\u5efa<\/strong>\u8fd9\u4e2a<code>Initializer<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176<code>create()<\/code>\u65b9\u6cd5<strong>\u901a\u77e5\u5185\u90e8\u7684\u521d\u59cb\u5316<\/strong>\uff0c\u5e76\u5c06\u5176<code>create()<\/code>\u65b9\u6cd5\u7684<strong>\u7ed3\u679c<\/strong>\u6dfb\u52a0\u5230\u5728<code>mInitialized<\/code>\u4e2d\u4ee5\u4fbf\u540e\u7eed\u83b7\u53d6\uff1b<strong>\u5426\u5219<\/strong>\uff0c\u5219\u4ece<code>mInitialized<\/code>\u4e2d\u83b7\u53d6<strong>\u7b2c\u4e00\u6b21\u521d\u59cb\u5316<\/strong>\u7684<strong>\u7ed3\u679c\u503c<\/strong>\uff0c\u4f7f\u5176<strong>\u4e0d\u4f1a\u9891\u7e41\u521b\u5efa<\/strong>\u8fd9\u4e2a<code>Initializer<\/code>\u5bf9\u8c61\u3002<br \/>\n  2. \u5982\u679c\u8981\u521b\u5efa\u7684<code>Initializer<\/code>\uff0c\u4f9d\u8d56\u5176\u5b83<code>Initializer<\/code>\uff0c\u5219\u4f1a\u8fdb\u884c<strong>\u5faa\u73af\u9012\u5f52\u521d\u59cb\u5316<\/strong>\u5176\u5b83<code>Initializer<\/code>\uff0c\u4f7f\u5176\u5b83<code>Initializer<\/code><strong>\u5168\u90e8\u521d\u59cb\u5316\u5b8c\u6210<\/strong>\uff0c\u624d\u4f1a\u6267\u884c\u6b64<code>Initializer<\/code>\u7684<code>create()<\/code>\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<h3>\u624b\u52a8\u521d\u59cb\u5316\u7ec4\u4ef6<\/h3>\n<p><strong>AppInitializer &#8211;> initializeComponent\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ \u521d\u59cb\u5316Initializer\u7c7b\n@NonNull\n@SuppressWarnings(\"unused\")\npublic &lt;T&gt; T initializeComponent(@NonNull Class&lt;? extends Initializer&lt;T&gt;&gt; component) {\n    return doInitialize(component);\n}\n<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/ \u7ebf\u7a0b\u9501\nprivate static final Object sLock = new Object();\n\n@NonNull\n@SuppressWarnings({\"unchecked\", \"TypeParameterUnusedInFormals\"})\n&lt;T&gt; T doInitialize(@NonNull Class&lt;? extends Initializer&lt;?&gt;&gt; component) {\n    Object result;\n    \/\/ \u540c\u6b65\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002\n    synchronized (sLock) {\n        result = mInitialized.get(component);\n        if (result == null) {\n            result = doInitialize(component, new HashSet&lt;Class&lt;?&gt;&gt;());\n        }\n    }\n    return (T) result;\n}\n<\/code><\/pre>\n<p><code>AppInitializer.initializeComponent()<\/code>\u65b9\u6cd5\uff0c\u76f4\u63a5\u8c03\u7528\u4e86<code>doInitialize()<\/code>\u65b9\u6cd5\u8fdb\u884c\u521b\u5efa\uff0c\u5e76\u8fd4\u56de\u4e86\u5176<code>doInitialize()<\/code>\u65b9\u6cd5\u7684\u7ed3\u679c\u503c\uff08\u5373<code>Initializer.create()<\/code>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff09\u3002<\/p>\n<blockquote><p>\n  \u8bf4\u660e\uff1a<br \/>\n  1. \u53ea\u6709<code>initializeComponent()<\/code>\u65b9\u6cd5\u624d\u80fd\u62ff\u5230<code>Initializer<\/code>\u7684<code>create()<\/code>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\uff0c\u4e0d\u7ba1\u662f<strong>\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\uff08<code>ContentProvider<\/code>\uff09<strong>\u5b58\u5165<\/strong>\uff0c\u8fd8\u662f<strong>\u624b\u52a8\u521d\u59cb\u5316<\/strong>\uff08\u8c03\u7528<code>initializeComponent()<\/code>\uff09<strong>\u5b58\u5165<\/strong>\uff0c\u90fd\u53ef\u4ee5<strong>\u518d\u6b21<\/strong>\u8c03\u7528<code>initializeComponent()<\/code>\u65b9\u6cd5<strong>\u83b7\u53d6\u5230<\/strong>\u5176\u5728<code>create()<\/code>\u65b9\u6cd5\u7684<strong>\u8fd4\u56de\u503c<\/strong>\u3002<br \/>\n  2. <code>doInitialize()<\/code>\u65b9\u6cd5\uff0c\u56e0\u4e3a\u4f7f\u7528\u4e86<code>synchronized<\/code>\u4ee3\u7801\u5757\uff0c\u4e14\u9501<code>sLock<\/code>\u4e3a<strong>\u9759\u6001<\/strong>\u7684\uff08\u552f\u4e00\uff09\uff0c\u6240\u4ee5\u5f53\u6709\u7ebf\u7a0b<strong>\u6b63\u5728<\/strong>\u6267\u884c<code>doInitialize()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5176\u5b83\u7ebf\u7a0b\u518d\u6267\u884c<code>doInitialize()<\/code>\u65b9\u6cd5\u65f6\u90fd\u5f97<strong>\u7b49\u5f85\u4e0a\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210<\/strong>\u3002<br \/>\n  3. \u7531\u4e8e<code>initializeComponent()<\/code>\u65b9\u6cd5\u8c03\u7528\u4e86<code>doInitialize()<\/code>\u65b9\u6cd5\uff08\u5185\u90e8\u4f7f\u7528\u4e86<code>synchronized<\/code>\uff09\uff0c\u5bfc\u81f4<strong>\u5176\u6267\u884c\u4e2d<\/strong>\uff0c\u5176\u5b83\u90fd\u5f97\u7b49\u5f85\u3002\u4f8b\u5982\uff1a<code>A<\/code>\u3001<code>B<\/code><strong>\u8017\u65f6<\/strong>\u6240\u4ee5<strong>\u5206\u522b\u5728\u4e00\u4e2a\u5b50\u7ebf\u7a0b<\/strong>\u6267\u884c<code>initializeComponent()<\/code>\u65b9\u6cd5\uff0c<code>C<\/code>\u5728<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u6267\u884c\uff0c\u4f7f\u5176\u4e09\u4e2a\u5206\u522b\u5728<strong>\u4e09\u4e2a\u7ebf\u7a0b<\/strong>\u6267\u884c\uff0c\u4ee5\u8fbe\u5230<strong>\u5e76\u53d1<\/strong>\u7684\u6548\u679c\uff0c\u4f46\u662f\u7ed3\u679c\u4e3a\uff1a<code>A<\/code>\u6267\u884c\u4e2d\uff0c<code>B<\/code>\u3001<code>C<\/code>\u7b49\u5f85<code>A<\/code>\u6267\u884c\u5b8c\u6210\uff0c\u672a\u8fbe\u5230<strong>\u5e76\u53d1<\/strong>\u6548\u679c\u3002\n<\/p><\/blockquote>\n<h2>\u5176\u5b83\u6e90\u7801<\/h2>\n<p><strong>AppInitializer &#8211;> isEagerlyInitialized\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-java\">\/\/ Initializer\u662f\u5426\u662f\u88ab\u6025\u5207\u5730\u521d\u59cb\u5316\npublic boolean isEagerlyInitialized(@NonNull Class&lt;? extends Initializer&lt;?&gt;&gt; component) {\n    \/\/ \u5982\u679c\u4ece\u672a\u8c03\u7528\u8fc7discoverAndInitialize()\uff0c\u5373\u4e0d\u662f\u901a\u8fc7\u6e05\u5355\u6587\u4ef6\u521d\u59cb\u5316\uff0c\u5219\u6ca1\u6709\u6025\u4e8e\u521d\u59cb\u5316\u4efb\u4f55\u5185\u5bb9\u3002\n    return mDiscovered.contains(component);\n}\n<\/code><\/pre>\n<p><code>AppInitializer.isEagerlyInitialized()<\/code>\u65b9\u6cd5\uff0c\u4e3a<code>Initializer<\/code>\u662f\u5426\u662f<strong>\u88ab\u6025\u5207\u5730\u521d\u59cb\u5316<\/strong>\uff0c\u5373\u662f\u5426\u662f\u901a\u8fc7<strong>\u6e05\u5355\u6587\u4ef6\u914d\u7f6e<\/strong>\u521d\u59cb\u5316\u3002<\/p>\n<h1>\u4f18\u7f3a\u70b9<\/h1>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<ul>\n<li>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u89c4\u5219\uff0c\u53ef\u4ee5\u8ba9\u6240\u6709\u7684\u4e09\u65b9\u5e93\uff0c\u4f7f\u7528<strong>\u540c\u4e00\u4e2a<\/strong><code>ContentProvider<\/code>\u5728\u5176<strong>\u5e93\u5185\u90e8<\/strong>\u8fdb\u884c<strong>\u521d\u59cb\u5316<\/strong>\uff0c<strong>\u51cf\u5c11\u4e86\u521d\u59cb\u5316\u4ee3\u7801<\/strong>\u4e14<strong>\u4f18\u5316<\/strong>\u4e86<strong>\u521b\u5efa\u591a\u4e2a<\/strong><code>ContentProvider<\/code>\u9020\u6210\u7684<strong>\u6027\u80fd\u3001\u65f6\u95f4\u635f\u8017<\/strong>\u3002<\/li>\n<li>\u652f\u6301<strong>\u521d\u59cb\u5316\u7684\u987a\u5e8f<\/strong>\u4ee5\u53ca<strong>\u4f9d\u8d56\u5173\u7cfb<\/strong>\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9<\/strong><\/p>\n<ul>\n<li>\u53ea\u80fd\u5728<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u521d\u59cb\u5316\uff0c\u4e0d\u652f\u6301<strong>\u5b50\u7ebf\u7a0b<\/strong>\u521d\u59cb\u5316\u3002<\/li>\n<li>\u53cd\u5c04\u521b\u5efa<code>Initializer<\/code>\u7c7b\uff0c\u5bf9\u6027\u80fd\u6709\u7a0d\u5fae\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<h1>\u603b\u7ed3<\/h1>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5168\u9762\u7684<code>Jetpack-Startup<\/code>\u6e90\u7801\u4e86\uff01\u6b22\u8fce\u5927\u5bb6<strong>\u70b9\u8d5e\u3001\u6536\u85cf<\/strong>\uff0c\u4ee5\u65b9\u4fbf\u60a8\u540e\u7eed\u67e5\u770b\uff0c\u4e4b\u540e\u4f1a\u51fa<code>Jetpack<\/code>\u5176\u5b83\u6e90\u7801\u7cfb\u5217\uff0c\u8bf7\u53ca\u65f6\u5173\u6ce8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Startup\u6e90\u7801\u89e3\u6790 \u6e90\u7801\u7248\u672c\uff1a * Startup\uff1a1.2.0 * \u66f4\u65b0\u65f6\u95f4\uff1a2025-3\u6708 \u5bfc\u822a\uff1a *  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18],"tags":[22],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-source-jetpack","tag-jetpack"],"modified_by":"admin","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":2,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":210,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/210"}],"wp:attachment":[{"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.devblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}