MultiProcessComponent
源碼地址
- <a >github源碼下載地址https://github.com/onlynight/MultiProcessComponent</a>
概述
這個(gè)demo中將會(huì)向你展示如果將不同的組件分配到不同的進(jìn)程中敞峭。這里我們只演示android原生提供的多進(jìn)程方式畸冲,通過jni fork的進(jìn)程我們不做演示疾牲。
- android原生為我們提供的多進(jìn)程方式只有一種就是在
AndroidManifest.xml
中申明組件的時(shí)候加上android:process
屬性晤硕。 - 默認(rèn)情況下我們不申明這個(gè)屬性的時(shí)候所有的組件都在同一個(gè)進(jìn)程中励饵。
-
android:processs
值有以下兩種書寫方式:
這樣
android:process=":remote"
前面帶有引號(hào)切不包含包名的寫法表示該組件為應(yīng)用的私有組件壁熄,外部無法通過任何形式訪問組件媒咳。實(shí)際上該組件所在進(jìn)程的名稱為<package>:remote
凶杖,例如我的組件名稱就是com.github.onlynight:remote
這種
android:process="com.github.onlynight.remote"
不帶引號(hào)的寫法就表示該組件可被其他應(yīng)用訪問瓦糕,且該組件所在進(jìn)程的名稱為com.github.onlynight.remote
demo 如下
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:process=":remote"/>
<service
android:name=".ThirdService"
android:enabled="true"
android:exported="true"
android:process="org.github.lion.multiprocesscomponent.remote"/>
</application>
輸出日志截圖如下:
這里寫圖片描述
這里寫圖片描述
這里寫圖片描述