zoukankan      html  css  js  c++  java
  • Android动态加载jar/dex

    http://www.cnblogs.com/over140/archive/2011/11/23/2259367.html

    加载jar

    boolean bool = new File("/sdcard/test.jar").exists();
    		
    		DexClassLoader cl = new DexClassLoader(
    				"/sdcard/test.jar", this.getCacheDir()
    						.getAbsolutePath(), null, this.getClassLoader());
    		try {
    			Class<?> c  =  cl.loadClass("HelloWorld");
    			
    			Object obj = c.newInstance();
    			
    			Method method = c.getMethod("getHelloWord", null);
    			
    			String s = (String) method.invoke(obj, null);
    			
    			TextView tv = (TextView)findViewById(R.id.tv);
    			
    			tv.setText(s);
    			
    		} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    

      

    不要忘记:

    将jar优化时应该重新成jar(jar->dex->jar),如果如下命令:

          dx --dex --output=test.jar dynamic.jar

    加载未安装APK中的类

    public class MainActivity extends ActionBarActivity {
    
    	@SuppressLint("NewApi")
    	@SuppressWarnings("unused")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		boolean bool = new File("/sdcard/test.apk").exists();
    		
    		DexClassLoader cl = new DexClassLoader(
    				"/sdcard/test.apk", this.getCacheDir()
    						.getAbsolutePath(), null, this.getClassLoader());
    		try {
    			Class<?> c  =  cl.loadClass("com.example.testdexclassloaderloadapk.MainActivity");
    			
    			Object obj = c.newInstance();
    			
    			Method method = c.getMethod("getHelloWord", null);
    			
    			String s = (String) method.invoke(obj, null);
    			
    			TextView tv = (TextView)findViewById(R.id.tv);
    			
    			tv.setText(s);
    			
    		} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    }
    

      注意:

    1.由于是使用反射,无法取得Context,也就是TestBActivity与普通的类毫无区别,没有生命周期。

    2.做实验是出现了: Class ref in pre-verified class resolved to unexpected implementation 错误

    查看:http://blog.csdn.net/berber78/article/details/41721877

    原因是 两个工程引用了同样的 appcompat_v7 工程

     大神之作  dl 动态加载架构:

    https://github.com/singwhatiwanna/dynamic-load-apk

  • 相关阅读:
    遗传算法(Genetic Algorithm, GA)及MATLAB实现
    CCF CSP 201809-2 买菜
    PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
    PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
    PAT (Basic Level) Practice (中文)1004 成绩排名 (20 分)
    PAT (Basic Level) Practice (中文)1002 写出这个数 (20 分)
    PAT (Advanced Level) Practice 1001 A+B Format (20 分)
    BP神经网络(原理及MATLAB实现)
    问题 1676: 算法2-8~2-11:链表的基本操作
    问题 1744: 畅通工程 (并查集)
  • 原文地址:https://www.cnblogs.com/wjw334/p/4372676.html
Copyright © 2011-2022 走看看