1. 程式人生 > >SDk初始化的設計參考

SDk初始化的設計參考

0.前言

我們在整合第三方SDK的時候大多都會在Application的onCreate方法裡進行SDK的初始化或配置工作,這好像也沒有什麼問題,不過我們能不能能做的更好一點呢?就是希望使用者在gradle檔案裡compile一下相應的庫就可以直接使用,不需要額外的初始化和配置.這個問題,我在閱讀Android Architecture Components(https://developer.android.com/topic/libraries/architecture/index.html) 原始碼時找到答案,現在把這個方案記錄下來。

1.註冊佔位的Provider

<provider

android:name="android.arch.lifecycle.LifecycleRuntimeTrojanProvider"
android:authorities="com.example.android.persistence.lifecycle-trojan" android:exported="false" android:multiprocess="true" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.在Provider的onCreate初始化

public class LifecycleRuntimeTrojanProvider extends ContentProvider {
    public
LifecycleRuntimeTrojanProvider() { } public boolean onCreate() { LifecycleDispatcher.init(this.getContext()); ProcessLifecycleOwner.init(this.getContext()); return true; } @Nullable public Cursor query(@NonNull Uri uri, String[] strings, String s, String[] strings1, String s1) { return
null; } @Nullable public String getType(@NonNull Uri uri) { return null; } @Nullable public Uri insert(@NonNull Uri uri, ContentValues contentValues) { return null; } public int delete(@NonNull Uri uri, String s, String[] strings) { return 0; } public int update(@NonNull Uri uri, ContentValues contentValues, String s, String[] strings) { return 0; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3.分析和總結

先說下結論:Provider的onCreate優先於Application的onCreate執行,並且此時的Application已經建立成功,而Provider裡的context正是Application的物件,完全符合需求。(也就是和在Application的onCreate裡是寫一樣一樣的)

LifecycleRuntimeTrojanProvider.onCreate的呼叫棧: 
這裡寫圖片描述

Application.onCreate的呼叫棧: 
這裡寫圖片描述

由ActivityThread的handleBindApplication方法可以看到,是先呼叫installContentProviders方法,然後呼叫mInstrumentation.callApplicationOnCreate方法的。

public final class ActivityThread {

private void handleBindApplication(AppBindData data) {
    // Allow disk access during application and provider setup. This could
   // block processing ordered broadcasts, but later processing would
   // probably end up doing the same disk access.
   final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
   try {
       // If the app is being launched for full backup or restore, bring it up in
       // a restricted environment with the base application class.
       Application app = data.info.makeApplication(data.restrictedBackupMode, null);
       mInitialApplication = app;

       // don't bring up providers in restricted mode; they may depend on the
       // app's custom Application class
       if (!data.restrictedBackupMode) {
           if (!ArrayUtils.isEmpty(data.providers)) {
               installContentProviders(app, data.providers);
               // For process that contains content providers, we want to
               // ensure that the JIT is enabled "at some point".
               mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
           }
       }

       // Do this after providers, since instrumentation tests generally start their
       // test thread at this point, and we don't want that racing.
       try {
           mInstrumentation.onCreate(data.instrumentationArgs);
       }
       catch (Exception e) {
           throw new RuntimeException(
               "Exception thrown in onCreate() of "
               + data.instrumentationName + ": " + e.toString(), e);
       }

       try {
           mInstrumentation.callApplicationOnCreate(app);
       } catch (Exception e) {
           if (!mInstrumentation.onException(app, e)) {
               throw new RuntimeException(
                   "Unable to create application " + app.getClass().getName()
                   + ": " + e.toString(), e);
           }
       }
   } finally {
       StrictMode.setThreadPolicy(savedPolicy);
   }
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

參考

* Android Architecture Components(https://developer.android.com/topic/libraries/architecture/index.html)

本文轉自:http://blog.csdn.net/ihrthk/article/details/74025388