android中DexClassLoader类的使用

        在Java中涉及到的类加载器就是ClassLoader这个类,通过ClassLoader.forName()的方法可以加载我们需要的类,从而实现在运行时动态加载类库的需求。但是在android中直接使用ClassLoader是行不通的,因为ClassLoader加载的java的字节码文件,而在android中使用的是dex格式的字节码,对此android专门提供了一个DexClassLoader类来完成动态加载apk的需求。下面来看看android官方文档的说明:

A class loader that loads classes from .jar and .apk files containing a classes.dex entry. This can be used to execute code not installed as part of an application.
This class loader requires an application-private, writable directory to cache optimized classes. Use Context.getCodeCacheDir() to create such a directory:

   File dexOutputDir = context.getCodeCacheDir();
   
 Do not cache optimized classes on external storage. External storage does not provide access controls necessary to protect your application from code injection attacks.

     android中DexClassLoader类的使用_第1张图片 

     下面用一个简单的例子来说明一下DexClassLoader这个类的使用,这个例子涉及到两个知识点:跨包取资源 & 反射调用方法。 首先建立一个Client工程,这个工程很简单,只有一个简单的layout和一个sayHello()的方法。

public class Main extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
	}
	
	@SuppressWarnings("unused")
	private void sayHello(String msg){
		Log.d("mmtag",msg);
	}
	
}

接着建立另外一个工程,在这个工程中调用client工程中的view和调用sayHello()方法。在这个类中主要涉及到了DexClassLoader这个类的使用。

package com.example.dexclassloaderserver;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

import dalvik.system.DexClassLoader;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        useDexClassLoader();
    }

    @SuppressLint("NewApi")
    public void useDexClassLoader() {

        Intent mIntent = new Intent();
        mIntent.setClassName("com.example.dexclassloaderclient",
                "com.example.dexclassloaderclient.MainActivity");
        PackageManager pm = this.getPackageManager();
        List<ResolveInfo> mList = pm.queryIntentActivities(mIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        ResolveInfo info = mList.get(0);
        
        String apkPath = info.activityInfo.applicationInfo.sourceDir;
        String optPath = this.getCodeCacheDir().getAbsolutePath();
        String libPath = info.activityInfo.applicationInfo.nativeLibraryDir;
        
        DexClassLoader clsLoader = new DexClassLoader(apkPath, optPath,
                libPath, this.getClass().getClassLoader());
        try {

            Class cls = clsLoader
                    .loadClass("com.example.dexclassloaderclient.MainActivity");
            Object obj = cls.newInstance();
            Method invokeMethod = cls.getDeclaredMethod("sayHello",
                    new Class[] { String.class });
            invokeMethod.setAccessible(true);
            invokeMethod.invoke(obj, "hello world,DexClassLoader");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


生成的结果为:

I/dex2oat (20250): dex2oat took 1.547s (threads: 4)

D/mmtag   (20229): hello world,DexClassLoader

D/OpenGLRenderer(20229): Render dirty regions requested: tru

这样就成功的调用了其他的apk中的方法。

你可能感兴趣的:(android中DexClassLoader类的使用)