标签:catch ref pre com tostring 类加载 目录 -- ack
如果对Android的ClassLoader加载机制不熟悉,猛戳Android插件化开发动态加载基础之ClassLoader工作机制 http://blog.csdn.net/u011068702/article/details/53248960
PathClassLoader:只能加载已经安装搭配Android系统中的apk文件
package com.example.testclassloader; public interface ShowString { public String sayChenyu(); }
package com.example.testclassloader; import android.util.Log; public class ShowStringClass implements ShowString{ public static final String TAG = "ShowStringClass"; @Override public String sayChenyu() { String chenyu = "chenyu"; Log.i(TAG, chenyu); return chenyu; } }
dx --dex --output=showStringClass_impl.jar showStringClass.jar然后再把showStringClass_impl.jar文件放到手机目录里面去用这个命令
adb push showStringClass_impl.jar /sdcard/具体操作图片如下
package com.example.testclassloader; import java.io.File; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.widget.TextView; import dalvik.system.DexClassLoader; public class MainActivity extends ActionBarActivity { public static final String TAG = "MainActivityClassLoader"; public static final String SHOWSTRINGCLASS = "showStringClass_impl.jar"; public static final String SHOWSTRINGCLASS_PATH= "com.example.testclassloader.ShowStringClass"; public static final String DEX = "dex"; public ShowStringClass mShowStringClass = null; public TextView mTv = null; public int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv = (TextView)findViewById(R.id.hello); DexClassLoader(this); } /** * 使用DexClassLoader方式加载类 */ public void DexClassLoader(Context context) { // dex压缩文件的路径(可以是apk,jar,zip格式) String dexPath = Environment.getExternalStorageDirectory().toString() + File.separator + SHOWSTRINGCLASS; // dex解压释放后的目录 String dexOutputDirs = Environment.getExternalStorageDirectory().toString(); //指定dexoutputpath为APP自己的缓存目录 File dexOutputDir = context.getDir(DEX, 0); // 定义DexClassLoader // 第一个参数:是dex压缩文件的路径 // 第二个参数:是dex解压缩后存放的目录 // 第三个参数:是C/C++依赖的本地库文件目录,可以为null // 第四个参数:是上一级的类加载器 //DexClassLoader dexClassLoader = new DexClassLoader(dexPath,dexOutputDirs,null,getClassLoader()); DexClassLoader dexClassLoader = new DexClassLoader(dexPath,dexOutputDir.getAbsolutePath(),null,getClassLoader()); Class libProvierClazz = null; // 使用DexClassLoader加载类 try { libProvierClazz = dexClassLoader.loadClass(SHOWSTRINGCLASS_PATH); // 创建dynamic实例 mShowStringClass = (ShowStringClass) libProvierClazz.newInstance(); if (mShowStringClass != null) { final String chenyu = mShowStringClass.sayChenyu(); if (chenyu != null) { mTv.post(new Runnable() { @Override public void run() { mTv.setText(chenyu); } }); } } else { Log.d(TAG, "mShowStringClass is null"); } } catch (Exception e) { e.printStackTrace(); } } /** * 打印系统的classLoader */ public void showClassLoader() { ClassLoader classLoader = getClassLoader(); if (classLoader != null){ Log.i(TAG, "[onCreate] classLoader " + i + " : " + classLoader.toString()); while (classLoader.getParent()!=null){ classLoader = classLoader.getParent(); Log.i(TAG,"[onCreate] classLoader " + i + " : " + classLoader.toString()); i++; } } } }
DexClassLoader dexClassLoader = new DexClassLoader(dexPath,dexOutputDirs,null,getClassLoader());会报下面的错误
//指定dexoutputpath为APP自己的缓存目录 File dexOutputDir = context.getDir(DEX, 0);
Android插件化开发之DexClassLoader动态加载dex、jar小Demo
标签:catch ref pre com tostring 类加载 目录 -- ack
原文地址:http://blog.csdn.net/u011068702/article/details/53263442