Skip to content

Commit

Permalink
Fix issue didi#294 by guessing the method startYourEngines
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed Jun 6, 2023
1 parent f3872c0 commit e276506
Showing 1 changed file with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
import android.util.Log;
import android.webkit.WebView;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static com.didiglobal.booster.instrument.Constants.TAG;
import static com.didiglobal.booster.instrument.Reflection.invokeMethod;
import static com.didiglobal.booster.instrument.Reflection.invokeStaticMethod;

/**
Expand Down Expand Up @@ -42,8 +47,37 @@ private static void startChromiumEngine(final Context context) {
try {
final long t0 = SystemClock.uptimeMillis();
final Object provider = invokeStaticMethod(Class.forName("android.webkit.WebViewFactory"), "getProvider");
invokeMethod(provider, "startYourEngines", new Class[]{boolean.class}, new Object[]{true});
Log.i(TAG, "Start chromium engine complete: " + (SystemClock.uptimeMillis() - t0) + " ms");

try {
final Method startYourEngines = provider.getClass().getDeclaredMethod("startYourEngines", boolean.class);
startYourEngines.setAccessible(true);
startYourEngines.invoke(provider, true);
Log.i(TAG, "Start chromium engine complete: " + (SystemClock.uptimeMillis() - t0) + " ms");
} catch (final Throwable t) {
final Set<Method> candidates = new HashSet<>();
final Method[] methods = provider.getClass().getSuperclass().getDeclaredMethods();

for (final Method m : methods) {
if (0 == (Modifier.STATIC & m.getModifiers())
&& 1 == m.getParameterCount()
&& void.class.equals(m.getReturnType())
&& boolean.class.equals(m.getParameterTypes()[0])) {
candidates.add(m);
}
}

if (candidates.size() == 1) {
final Method startYourEngines = candidates.iterator().next();
startYourEngines.setAccessible(true);
startYourEngines.invoke(provider, true);
Log.i(TAG, "Start chromium engine from " + provider.getClass().getName() + "." + startYourEngines.getName() + "(boolean) complete: " + (SystemClock.uptimeMillis() - t0) + " ms");
} else if (candidates.size() > 1) {
Log.w(TAG, "Method " + provider.getClass().getName() + ".startYourEngines(boolean) not found, multiple candidates found: " + Arrays.toString(candidates.toArray()));
} else {
Log.w(TAG, "Method " + provider.getClass().getName() + ".startYourEngines(boolean) not found", t);
}
}

if (Build.VERSION.SDK_INT >= 28) {
String processName = Application.getProcessName();
String packageName = context.getPackageName();
Expand Down

0 comments on commit e276506

Please sign in to comment.