private final Application tryLoadApolo(Context mainAppContext, AppBindData data) {
if
(mainAppContext
=
=
null) {
throw new RuntimeException(
"mainAppContext is null"
);
}
final String APOLO_TAG
=
"ApoloModule"
;
/
/
1.
这里是通过Settings获取apolo包名
-
-
-
-
-
-
-
final ContentResolver resolver
=
mainAppContext.getContentResolver();
String apoloPkg
=
Settings.Global.getString(resolver,
"pkg.apolo"
);
if
(apoloPkg
=
=
null) {
/
/
1.1
如果Settings没找到,默认使用该包名
-
-
-
-
-
-
-
apoloPkg
=
"com.example.apolo"
;
}
/
/
2.
-
-
-
-
-
-
-
-
/
*
*
*
*
*
*
*
过滤apolo自身以及系统app
*
*
*
*
*
*
*
/
if
(Objects.equals(data.appInfo.packageName, apoloPkg)
|| data.appInfo.isSystemApp()
|| data.appInfo.isPrivilegedApp()) {
Log.d(APOLO_TAG,
"No need to load apolo for "
+
data.appInfo.packageName);
return
null;
}
Application apoloApp
=
null;
ApplicationInfo apoloInfo
=
null;
try
{
/
/
3.
获取apolo插件的ApplicationInfo
-
-
-
-
-
-
-
-
apoloInfo
=
mainAppContext.getPackageManager()
.getApplicationInfo(apoloPkg,
0
);
} catch (Exception e) {
Log.e(APOLO_TAG,
"load failed for apolo pkg not found"
, e);
}
if
(apoloInfo !
=
null) {
/
/
4
-
-
-
-
-
-
-
-
/
*
*
*
*
*
*
*
*
*
*
*
*
*
fix Apolo abi
*
*
*
*
*
*
*
*
*
*
*
*
*
*
/
final String runtimeInstruction
=
VMRuntime.getCurrentInstructionSet();
if
(Objects.equals(runtimeInstruction,
"arm"
)) {
apoloInfo.nativeLibraryRootDir
=
apoloInfo.sourceDir
+
"!/lib/"
;
apoloInfo.nativeLibraryDir
=
apoloInfo.sourceDir
+
"!/lib/armeabi-v7a"
;
apoloInfo.primaryCpuAbi
=
"armeabi-v7a"
;
}
final LoadedApk apoloApk
=
getPackageInfoNoCheck(apoloInfo, data.compatInfo);
try
{
/
/
5.
执行Apolo的入口Application.attachBaseContext
apoloApp
=
apoloApk.makeApplication(data.restrictedBackupMode, null);
} catch (Exception e) {
Log.e(APOLO_TAG,
"make apolo application fail"
, e);
}
}
if
(apoloApp !
=
null) {
Log.d(APOLO_TAG,
"\033[1;32mload apolo success\033[0m"
);
}
return
apoloApp;
}