Skip to content

Commit f65623c

Browse files
committed
Support custom Application feature.
1 parent 4f7e7b6 commit f65623c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

android-pluginmgr/src/main/java/androidx/pluginmgr/PluginManager.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
import androidx.pluginmgr.utils.PluginManifestUtil;
4343
import androidx.pluginmgr.verify.PluginNotFoundException;
4444
import androidx.pluginmgr.verify.PluginOverdueVerifier;
45+
import android.content.pm.ApplicationInfo;
46+
import android.app.*;
47+
import java.lang.reflect.Field;
48+
import android.content.*;
49+
import androidx.pluginmgr.environment.PluginContext;
4550

4651
/**
4752
* 插件管理器
@@ -110,7 +115,8 @@ private PluginManager(Context context) {
110115
}
111116
DelegateActivityThread delegateActivityThread = DelegateActivityThread.getSingleton();
112117
Instrumentation originInstrumentation = delegateActivityThread.getInstrumentation();
113-
delegateActivityThread.setInstrumentation(new PluginInstrumentation(originInstrumentation));
118+
pluginInstrumentation = new PluginInstrumentation(originInstrumentation);
119+
delegateActivityThread.setInstrumentation(pluginInstrumentation);
114120
}
115121

116122

@@ -239,7 +245,7 @@ public Collection<PlugInfo> loadPlugin(final File pluginSrcDirFile)
239245
pluginPkgToInfoMap.clear();
240246
}
241247
File[] pluginApkFiles = pluginSrcDirFile.listFiles(this);
242-
if (pluginApkFiles == null || pluginApkFiles.length < 1) {
248+
if (pluginApkFiles == null || pluginApkFiles.length == 0) {
243249
throw new FileNotFoundException("could not find plugins in:"
244250
+ pluginSrcDirFile);
245251
}
@@ -290,11 +296,40 @@ private PlugInfo buildPlugInfo(File pluginApk, String pluginId,
290296
PluginClassLoader pluginClassLoader = new PluginClassLoader(info, dexPath, dexOutputPath
291297
, getPluginLibPath(info).getAbsolutePath(), ClassLoader.getSystemClassLoader().getParent());
292298
info.setClassLoader(pluginClassLoader);
293-
299+
ApplicationInfo appInfo = info.getPackageInfo().applicationInfo;
300+
String appClassName = null;
301+
if(appInfo != null){
302+
appClassName = appInfo.name;
303+
}
304+
Application app = makeApplication(pluginClassLoader,appClassName);
305+
attachBaseContext(info,app);
306+
info.setApplication(app);
294307
Log.i(TAG, "buildPlugInfo: " + info);
295308
return info;
296309
}
297310

311+
private void attachBaseContext(PlugInfo info, Application app)
312+
{
313+
try{
314+
Field mBase = ContextWrapper.class.getDeclaredField("mBase");
315+
mBase.setAccessible(true);
316+
mBase.set(app, new PluginContext(app.getBaseContext(), info));
317+
}catch(Throwable e){
318+
319+
}
320+
}
321+
322+
private Application makeApplication(PluginClassLoader pluginClassLoader, String appClassName)
323+
{
324+
if(appClassName != null) {
325+
try{
326+
return (Application) pluginClassLoader.loadClass(appClassName).newInstance();
327+
}catch(Throwable ignored){}
328+
}
329+
330+
return new Application();
331+
}
332+
298333

299334
/**
300335
* 将Apk复制到私有目录

android-pluginmgr/src/main/java/androidx/pluginmgr/environment/PluginInstrumentation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public void callActivityOnCreate(Activity activity, Bundle icicle) {
7575
field.setAccessible(true);
7676
field.set(activity, pluginContext);
7777

78+
Reflect.on(activity).set("mApplication", currentPlugin.getApplication());
79+
7880
} catch (Throwable e) {
7981
e.printStackTrace();
8082
}

android-pluginmgr/src/main/java/androidx/pluginmgr/utils/PluginManifestUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public static void setManifestInfo(Context context, String apkPath, PlugInfo inf
8181
private static boolean extractLibFile(ZipFile zip, File tardir)
8282
throws IOException {
8383

84+
if(!tardir.exists()){
85+
tardir.mkdirs();
86+
}
8487

8588
String defaultArch = "armeabi";
8689
Map<String,List<ZipEntry>> archLibEntries = new HashMap<String, List<ZipEntry>>();

0 commit comments

Comments
 (0)