The tutorial is easy to understand

The standard name

The Android plug-in,

The DexClassLoader loads the uninstalled APK and provides resources for the host APP to use

There are many add-on tutorials on the Internet. I read many tutorials, all of which are Android. I didn’t find autoJS, so I translated them and drew a simple and clear flow chart

The flow chart

Autojs version

9.0.4

Uninstalled app resources available in the tutorial

  • The picture
  • color
  • string
  • activity_main.xml

The code on

1. Information about the uninstalled APK is obtained
// resourcePath is the app file path on the SD card
function queryPackageInfo(resourcePath) {
  return context.getPackageManager().getPackageArchiveInfo(resourcePath, context.getPackageManager().GET_ACTIVITIES);
}
Copy the code
2. Create an AssetManager instance
assetManager = Class.forName("android.content.res.AssetManager").newInstance();
Copy the code
3. Add the APK path
let method = assetManager.getClass().getMethod("addAssetPath", Class.forName("java.lang.String"));
// Reflection sets the resource loading path
method.invoke(assetManager, resourcePath);
Copy the code
4. Construct the correct Resource
resources = new Resources(
  assetManager,
  context.getResources().getDisplayMetrics(),
  context.getResources().getConfiguration()
);
Copy the code
5. Instantiate DexClassLoader
// The constructor
// public DexClassLoader(String dexPath, String optimizedDirectory, String libraryPath, ClassLoader parent)
// dexPath - is the path of the apk file
// Optimizeddirectory-apk Specifies the decompressed directory for storing the dex. This directory is not allowed on the SD card after 4.1
// libraryPath - the local library
// parent - parent loader
new DexClassLoader(resourcePath, mDexDir, null, context.getClassLoader())
Copy the code
6. Class is loaded
cls = mResourceLoadBean.getClassLoader().loadClass(rClassName);
Copy the code
7. Obtain the resource ID
cls = mResourceLoadBean.getClassLoader().loadClass(rClassName);
resID = cls.getField(fieldName).get(null);
Copy the code
8. Obtain resource entities
drawable = mResourceLoadBean.getResources().getDrawable(resourceID);
Copy the code

Matters needing attention

  • The second parameter of the DexClassLoader must be a private directory and cannot be empty
  • The text of activity_main. XML is centered, which is normal in Androi Studio. Dynamic loading with activity_setContentView is not centered
  • Java class, converted to autoJS class, internal members are best attached to this
  • The instance of The assetManager is created by the host app, but be sure to set the resource load path to the uninstalled app, because plug-ins use the uninstalled app resource

The statement

This tutorial is for study only and is prohibited for other purposes