Android 插件开发--类加载器

1). Android中类加载器

Android 中常用的类加载器有DexClassLoader和PathClassLoader,其中PathClassLoader是Android应用中的默认加载器。

  • DexClassLoader可以加载任何路径下的apk/dex/jar/zip
  • PathClassLoader只能加载/data/app中的apk,也就是已经安装在手机中的apk。
2). DexClassLoader

构造方法:

  public DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent) ;

其中:

  • 继承自BaseDexClassLoader
  • dexPath: 加载apk/dex/jar/zip的路径
  • optimizedDirectory: 是dex的输出路径
  • librarySearchPath: 加载时候需要用到的lib库,一般不用
  • parent: DexClassLoader指定的父加载器
3). PathClassLoader

构造方法:

public PathClassLoader(String dexPath, ClassLoader parent);
public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent);

其中

  • 继承自BaseDexClassLoader
  • dexPath: 加载dex路径
  • librarySearchPath: 加载时候需要用到的lib库
  • parent: PathClassLoader指定的父加载器
  • dex释放路径: /data/dalvik-cache
4). 查看各种类加载器
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    printMoreClassLoader();
  }
  
  private static final String TAG = "MainActivity";
  /**
   * 打印各种类加载器
   */
  private void printMoreClassLoader() {
    Log.d(TAG, "Context类的类加载器:" + Context.class.getClassLoader());
    Log.d(TAG, "ListView类的类加载器:" + ListView.class.getClassLoader());
    Log.d(TAG, "应用程序默认的类加载器:" + getClassLoader());
    Log.d(TAG, "系统类加载器:" + ClassLoader.getSystemClassLoader());
    Log.d(TAG, "系统类加载器和Context类的类加载器是否相等:" + (Context.class.getClassLoader() ==ClassLoader.getSystemClassLoader()));
    Log.d(TAG, "系统类加载器和应用程序默认加载器是否相等:" + (getClassLoader() == ClassLoader.getSystemClassLoader()));
  
    Log.d(TAG, "================================================");
    
    Log.d(TAG, "打印应用程序默认加载器的委派机制:");
    ClassLoader classLoader = getClassLoader();
    while (null != classLoader) {
      Log.d(TAG, "类加载器: " + classLoader);
      classLoader = classLoader.getParent();
    }
  
    Log.d(TAG, "================================================");
    
    Log.d(TAG, "打印系统加载器的委派机制:");
    classLoader = ClassLoader.getSystemClassLoader();
    while (null != classLoader) {
      Log.d(TAG, "类加载器:" + classLoader);
      classLoader = classLoader.getParent();
    }
    
  }

打印结果:

Context类的类加载器:java.lang.BootClassLoader@8b6e11e
ListView类的类加载器:java.lang.BootClassLoader@8b6e11e
应用程序默认的类加载器:dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mazaiting.dynamicjar-2/base.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.mazaiting.dynamicjar-2/lib/arm64, /system/lib64, /vendor/lib64]]]
系统类加载器:dalvik.system.PathClassLoader[DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]]
系统类加载器和Context类的类加载器是否相等:false
系统类加载器和应用程序默认加载器是否相等:false
================================================
打印应用程序默认加载器的委派机制:
类加载器: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mazaiting.dynamicjar-2/base.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.mazaiting.dynamicjar-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.mazaiting.dynamicjar-2/lib/arm64, /system/lib64, /vendor/lib64]]]
类加载器: java.lang.BootClassLoader@8b6e11e
================================================
打印系统加载器的委派机制:
类加载器:dalvik.system.PathClassLoader[DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]]
类加载器:java.lang.BootClassLoader@8b6e11e

你可能感兴趣的:(Android 插件开发--类加载器)