10000 merge from experiment:support android 2.3.6- · CJavaScala/android-pluginmgr@696bd98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 696bd98

Browse files
committed
merge from experiment:support android 2.3.6-
1 parent 4bf7ff3 commit 696bd98

File tree

7 files changed

+84
-43
lines changed

7 files changed

+84
-43
lines changed

PlugLoadDemo/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
>
9191
<activity
9292
android:name=".MainActivity"
93+
android:windowSoftInputMode="adjustUnspecified|stateHidden"
9394
android:label="@string/app_name" >
9495
<intent-filter>
9596
<action android:name="android.intent.action.MAIN" />

android-pluginmgr/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.android</groupId>
55
<artifactId>pluginmgr</artifactId>
6-
<version>0.0.4</version>
6+
<version>0.0.6</version>
77
<name>androidx.pluginmgr</name>
88
<description>dynamic load uninstalled apk</description>
99
<dependencies>

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

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import static java.lang.reflect.Modifier.STATIC;
2323

2424
import java.io.File;
25+
import java.io.FileOutputStream;
2526
import java.io.IOException;
27+
import java.util.jar.JarEntry;
28+
import java.util.jar.JarOutputStream;
2629

2730
import android.app.Activity;
2831
import android.content.ComponentName;
@@ -42,6 +45,7 @@
4245
import com.google.dexmaker.Local;
4346
import com.google.dexmaker.MethodId;
4447
import com.google.dexmaker.TypeId;
48+
import com.google.dexmaker.dx.dex.DexFormat;
4549

4650
/**
4751
* 动态生成 插件Activity子类的工具类
@@ -58,7 +62,16 @@ public static void createActivityDex(String superClassName,
5862
throws IOException {
5963
byte[] dex = createActivityDex(superClassName, targetClassName,
6064
pluginId, pkgName);
61-
FileUtil.writeToFile(dex, saveTo);
65+
if (saveTo.getName().endsWith(".dex")) {
66+
FileUtil.writeToFile(dex, saveTo);
67+
} else {
68+
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(
69+
saveTo));
70+
jarOut.putNextEntry(new JarEntry(DexFormat.DEX_IN_JAR_NAME));
71+
jarOut.write(dex);
72+
jarOut.closeEntry();
73+
jarOut.close();
74+
}
6275
}
6376

6477
/**
@@ -296,9 +309,19 @@ private static <S, D extends S> void declareMethod_startActivityForResult(
296309
TypeId<Intent> intent = TypeId.get(Intent.class);
297310
TypeId<Integer> requestCode = TypeId.INT;
298311
TypeId<Bundle> bundle = TypeId.get(Bundle.class);
299-
312+
313+
TypeId<?>[] params;
314+
String methodName = "startActivityForResult";
315+
final boolean isNewSdk = android.os.Build.VERSION.SDK_INT > 10;
316+
if (isNewSdk) {
317+
params = new TypeId[] { intent, requestCode, bundle };
318+
} else {
319+
params = new TypeId[] { intent, requestCode };
320+
}
300321
MethodId<D, Void> method = generatedType.getMethod(TypeId.VOID,
301-
"startActivityForResult", intent, requestCode, bundle);
322+
methodName, params);
323+
MethodId<S, Void> superMethod = superType.getMethod(TypeId.VOID,
324+
methodName, params);
302325
Code methodCode = dexMaker.declare(method, PUBLIC);
303326
TypeId<ActivityOverider> ActivityOverider = TypeId
304327
.get(ActivityOverider.class);
@@ -309,25 +332,40 @@ private static <S, D extends S> void declareMethod_startActivityForResult(
309332
// locals
310333
Local<D> localThis = methodCode.getThis(generatedType);
311334
Local<Intent> newIntent = methodCode.newLocal(intent);
335+
Local<Bundle> nullParamBundle = methodCode.newLocal(bundle);
312336
Local<String> pluginId = get_pluginId(generatedType, methodCode);
313337

314-
methodCode.invokeStatic(methodOveride,
315-
newIntent//
316-
,localThis
317-
, pluginId
318-
, methodCode.getParameter(0, intent)//
319-
, methodCode.getParameter(1, requestCode)//
320-
, methodCode.getParameter(2, bundle)//
321-
);
322-
// super.startActivityForResult(...)
323-
MethodId<S, Void> superMethod = superType.getMethod(TypeId.VOID,
324-
"startActivityForResult", intent, requestCode, bundle);
325-
methodCode.invokeSuper(superMethod, null,
326-
methodCode.getThis(generatedType)//
327-
, newIntent//
328-
, methodCode.getParameter(1, requestCode)//
329-
, methodCode.getParameter(2, bundle) //
330-
);
338+
methodCode.loadConstant(nullParamBundle, null);
339+
Local<?> args[];
340+
if (isNewSdk) {
341+
args = new Local[] {localThis
342+
, pluginId
343+
, methodCode.getParameter(0, intent)//
344+
, methodCode.getParameter(1, requestCode)//
345+
, methodCode.getParameter(2, bundle)//
346+
};
347+
methodCode.invokeStatic(methodOveride, newIntent, args);
348+
// super.startActivityForResult(...)
349+
methodCode.invokeSuper(superMethod, null,
350+
localThis//
351+
, newIntent//
352+
, methodCode.getParameter(1, requestCode)//
353+
, methodCode.getParameter(2, bundle) //
354+
);
355+
} else {
356+
args = new Local[] {localThis
357+
, pluginId
358+
, methodCode.getParameter(0, intent)//
359+
, methodCode.getParameter(1, requestCode)//
360+
,nullParamBundle
361+
};
362+
methodCode.invokeStatic(methodOveride, newIntent, args);
363+
methodCode.invokeSuper(superMethod, null,
364+
localThis//
365+
, newIntent//
366+
, methodCode.getParameter(1, requestCode)//
367+
);
368+
}
331369
methodCode.returnVoid();
332370
}
333371

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,27 @@ static File getPluginLibDir(String pluginId) {
173173
}
174174

175175
static File getPorxyActivityDexPath(String pluginId, String activity) {
176-
File folder = new File(getPluginBaseDir(pluginId)+"/acts/");
176+
File folder = new File(getPluginBaseDir(pluginId)+"/activities/");
177177
folder.mkdirs();
178-
File saveDir = new File(folder, activity + ".dex");
179-
return saveDir;
178+
String suffix = ".dex";
179+
if (android.os.Build.VERSION.SDK_INT < 11) {
180+
suffix = ".jar";
181+
}
182+
File savePath = new File(folder, activity + suffix);
183+
return savePath;
180184
}
181185

182-
static void createProxyDex(PlugInfo plugin, String activity) {
183-
createProxyDex(plugin, activity, true);
186+
static File createProxyDex(PlugInfo plugin, String activity) {
187+
return createProxyDex(plugin, activity, true);
184188
}
185189

186-
static void createProxyDex(PlugInfo plugin, String activity, boolean lazy) {
187-
File saveDir = getPorxyActivityDexPath(plugin.getId(), activity);
188-
createProxyDex(plugin, activity, saveDir, lazy);
190+
static File createProxyDex(PlugInfo plugin, String activity, boolean lazy) {
191+
File savePath = getPorxyActivityDexPath(plugin.getId(), activity);
192+
createProxyDex(plugin, activity, savePath, lazy);
193+
return savePath;
189194
}
190195

191-
static void createProxyDex(PlugInfo plugin, String activity, File saveDir,
196+
private static void createProxyDex(PlugInfo plugin, String activity, File saveDir,
192197
boolean lazy) {
193198
// Log.d(tag + ":createProxyDex", "plugin=" + plugin + "\n, activity="
194199
// + activity);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import android.app.Application;
22+
import android.content.Intent;
2223
import android.content.pm.ActivityInfo;
2324
import android.content.pm.PackageInfo;
2425
import android.content.pm.ResolveInfo;
@@ -177,9 +178,8 @@ public ServiceInfo findServiceByAction(String action) {
177178
public void addActivity(ResolveInfo activity) {
178179
activities.add(activity);
179180
if (mainActivity == null && activity.filter != null
180-
&& activity.filter.hasAction("android.intent.action.MAIN")
181-
&& activity.filter.hasCategory("android.intent.category.LAUNCHER")
182-
) {
181+
&& activity.filter.hasAction(Intent.ACTION_MAIN)
182+
&& activity.filter.hasCategory(Intent.CATEGORY_LAUNCHER)) {
183183
mainActivity = activity;
184184
}
185185
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ public PluginClassLoader(String dexPath, String optimizedDir, ClassLoader parent
5151
Class<?> loadActivityClass(final String actClassName) throws ClassNotFoundException {
5252
Log.d(tag, "loadActivityClass: " + actClassName);
5353

54-
File dexSaveDir = ActivityOverider.getPorxyActivityDexPath(thisPlugin.getId(), actClassName);
55-
// 在类加载之前检查创建代理的Activity dex文件,以免调用者忘记生成此文件
56-
ActivityOverider.createProxyDex(thisPlugin, actClassName, dexSaveDir, true);
54+
// 在类加载之前检查创建代理的Activity dex文件
55+
File dexSavePath = ActivityOverider.createProxyDex(thisPlugin, actClassName, true);
5756
ClassLoader actLoader = proxyActivityLoaderMap.get(actClassName);
5857
if (actLoader == null) {
59-
actLoader = new DexClassLoader(dexSaveDir.getAbsolutePath(), optimizedDirectory,libraryPath, this){
58+
actLoader = new DexClassLoader(dexSavePath.getAbsolutePath(), optimizedDirectory,libraryPath, this){
6059
@Override
6160
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
6261
Log.d("PlugActClassLoader("+ actClassName+")", "loadClass: " + name);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,10 @@ private void setApplicationBase(PlugInfo info,Application application)throws Exc
416416
//
417417
PluginContextWrapper ctxWrapper = new PluginContextWrapper(context,
418418
info);
419-
// set field: mBase
420-
ReflectionUtils.setFieldValue(application, "mBase", ctxWrapper);
421-
// set field: mLoadedApk, get from context(framework application)
422-
Object mLoadedApk = ReflectionUtils
423-
.getFieldValue(context, "mLoadedApk");
424-
ReflectionUtils.setFieldValue(application, "mLoadedApk", mLoadedApk);
419+
// attach
420+
java.lang.reflect.Method attachMethod = android.app.Application.class.getDeclaredMethod("attach", Context.class);
421+
attachMethod.setAccessible(true);
422+
attachMethod.invoke(application, ctxWrapper);
425423
if (context instanceof Application) {
426424
if (android.os.Build.VERSION.SDK_INT >= 14) {
427425
Application.class.getMethod("registerComponentCallbacks",

0 commit comments

Comments
 (0)
0