XWalk browser kernel plug-in, just install a copy, can provide compatibility browser controls for other apps, android 4.4 version, or other versions of the custom machine, running a higher version of the WebView. Compared to x5 kernel, this kind of plug-in is what I want, not like X5, every APP that calls X5 kernel will copy up to more than 100 megabytes of files, with dynamic loading apK /dex way to achieve Google does not allow plug-in (also only the domestic market with many rules will allow).

I have looked at the source code for Xwalk. In fact, watching the source code is not what exalted things, Java layer is more transparent, IDEA can be directly used to click on the mouse, even if it is a compiled JAR file, you can also view the decomcompiled source code, find references, the full text search package and so on. Seeing that XWalk is a plugin implemented using the createPackageContext method, I wanted to learn something about it, but now THAT I have time, I decided to give it a try.

Write and install plug-in APK

Github address of project file

The link is the dark mode switch button written in the previous article, which realizes the switch animation of the moon into the sun and the sun into the moon. It is directly used as the plug-in project demonstrated in this article.

Brief description of the project construction process: Create a new project with Android Studio, use an empty Activity template, and name the package com.knziha.plugin101. Paste the code from the previous article into the directory tree, and then reference this button in the main interface XML.

Call the plug-in APK

See the github address in the project file

Call the plugin package in onCreate to dynamically create the DarkToggleButton and embed it in the main screen:

Reflection creates views in plug-ins

findViewById(android.R.id.content).postDelayed(() -> {
   TextView txvA = findViewById(R.id.textview_first);
   try {
      String pluginPkg = "com.knziha.plugin101"; Context context = createPackageContext(pluginPkg, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); Class<? > cls = context.getClassLoader().loadClass(pluginPkg+".R"); // Get the R class of the target APK
      txvA.setText(context.getResources().getText(getResourseIdByName(cls, "string"."message"))); Class<? > darkToggleClass = context.getClassLoader().loadClass(pluginPkg+".DarkToggleButton"); Constructor<? > cons = darkToggleClass.getConstructor(Context.class, AttributeSet.class); View darkToggleBtn = (View) cons.newInstance(this.null);
      ((ViewGroup)txvA.getParent()).addView(darkToggleBtn);
      
   } catch(Exception e) { e.printStackTrace(); }},200);
Copy the code

Brief description of the project construction process: Create a new project with Android Studio and select basic activities from the template. Paste the above code at the end of the onCreate for the main activity.

Reflection calls the methods of the plug-in class

Interface:

public interface DarkToggleInterface {
   void toggle(a);
}
Copy the code
public class DarkToggle implements DarkToggleInterface{
   final Object mDelegate;
   final Method mToggle;
   
   public DarkToggle(Object mDelegate) throws NoSuchMethodException {
      this.mDelegate = mDelegate;
      mToggle = mDelegate.getClass().getMethod("toggle");
   }
   
   public void toggle(a) {
      try {
         mToggle.invoke(mDelegate);
      } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Call:

try {
   DarkToggle darkToggle = new DarkToggle(darkToggleBtn);
   darkToggle.toggle();
} catch (Exception e) {
   throw new RuntimeException(e);
}
Copy the code