Skip to content

Commit

Permalink
test(sample): sample中插件进程不该重复执行主进程才该执行的初始化流程
Browse files Browse the repository at this point in the history
  • Loading branch information
shifujun committed Apr 18, 2023
1 parent 18c718e commit b3b43a0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ public static void onApplicationCreate(Application application) {
DynamicRuntime.recoveryRuntime(application);
}

FixedPathPmUpdater fixedPathPmUpdater
= new FixedPathPmUpdater(new File("/data/local/tmp/sample-manager-debug.apk"));
boolean needWaitingUpdate
= fixedPathPmUpdater.wasUpdating()//之前正在更新中,暗示更新出错了,应该放弃之前的缓存
|| fixedPathPmUpdater.getLatest() == null;//没有本地缓存
Future<File> update = fixedPathPmUpdater.update();
if (needWaitingUpdate) {
try {
update.get();//这里是阻塞的,需要业务自行保证更新Manager足够快。
} catch (Exception e) {
throw new RuntimeException("Sample程序不容错", e);
if (isProcess(application, application.getPackageName())) {
FixedPathPmUpdater fixedPathPmUpdater
= new FixedPathPmUpdater(new File("/data/local/tmp/sample-manager-debug.apk"));
boolean needWaitingUpdate
= fixedPathPmUpdater.wasUpdating()//之前正在更新中,暗示更新出错了,应该放弃之前的缓存
|| fixedPathPmUpdater.getLatest() == null;//没有本地缓存
Future<File> update = fixedPathPmUpdater.update();
if (needWaitingUpdate) {
try {
update.get();//这里是阻塞的,需要业务自行保证更新Manager足够快。
} catch (Exception e) {
throw new RuntimeException("Sample程序不容错", e);
}
}
sPluginManager = new DynamicPluginManager(fixedPathPmUpdater);
}
sPluginManager = new DynamicPluginManager(fixedPathPmUpdater);
}

private static boolean isProcess(Context context, String processName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.tencent.shadow.sample.host;

import static android.os.Process.myPid;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
Expand All @@ -33,8 +35,6 @@

import java.io.File;

import static android.os.Process.myPid;

public class HostApplication extends Application {
private static HostApplication sApp;

Expand All @@ -56,7 +56,9 @@ public void onCreate() {
DynamicRuntime.recoveryRuntime(this);
}

PluginHelper.getInstance().init(this);
if (isProcess(this, getPackageName())) {
PluginHelper.getInstance().init(this);
}

HostUiLayerProvider.init(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package com.tencent.shadow.test.dynamic.host;

import static android.os.Process.myPid;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.webkit.WebView;
Expand Down Expand Up @@ -46,12 +50,17 @@ public void onCreate() {

LoggerFactory.setILoggerFactory(new AndroidLogLoggerFactory());

//在全动态架构中,Activity组件没有打包在宿主而是位于被动态加载的runtime,
//为了防止插件crash后,系统自动恢复crash前的Activity组件,此时由于没有加载runtime而发生classNotFound异常,导致二次crash
//因此这里恢复加载上一次的runtime
DynamicRuntime.recoveryRuntime(this);
if (isProcess(this, ":plugin")) {
//在全动态架构中,Activity组件没有打包在宿主而是位于被动态加载的runtime,
//为了防止插件crash后,系统自动恢复crash前的Activity组件,此时由于没有加载runtime而发生classNotFound异常,导致二次crash
//因此这里恢复加载上一次的runtime
DynamicRuntime.recoveryRuntime(this);
}

if (isProcess(this, getPackageName())) {
PluginHelper.getInstance().init(this);
}

PluginHelper.getInstance().init(this);

//Using WebView from more than one process at once with the same data directory is not supported.
//https://crbug.com/558377
Expand Down Expand Up @@ -92,4 +101,18 @@ public void loadPluginManager(File apk) {
public PluginManager getPluginManager() {
return mPluginManager;
}

private static boolean isProcess(Context context, String processName) {
String currentProcName = "";
ActivityManager manager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == myPid()) {
currentProcName = processInfo.processName;
break;
}
}

return currentProcName.endsWith(processName);
}
}

0 comments on commit b3b43a0

Please sign in to comment.