首页 > 学院 > 开发设计 > 正文

动态加载

2019-11-09 18:52:24
字体:
来源:转载
供稿:网友

动态加载需要反射的知识

一、加载未安装apk 1、 首先得有宿主APK (ta)

下面是按钮触发 String fileName = "TestB.apk"; String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName; public void ClickIn(View v) { DexClassLoader classLoader = new DexClassLoader(apkPath, getDir("dex", Context.MODE_PRIVATE).getAbsolutePath(), null, getClassLoader()); try { Class TestBClass = classLoader.loadClass("com.hyhy.tb.TestBActivity"); Object TestBActivity = TestBClass.newInstance(); Method getEat = TestBClass.getDeclaredMethod("getEat", String.class); getEat.setaccessible(true); Object eat = getEat.invoke(TestBActivity, "吃饭吧!!!"); //调用getEat(String arg); Toast.makeText(this, eat.toString(), Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } }

2、插件APK(tb) 把tb命名成TestB.apk,复制到SD卡根目录就可以了 跑步

/** * 被调起的未安装APK */public class TestBActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public String getEat(String arg) { return arg; }}

:参考 http://www.cnblogs.com/over140/archive/2012/03/29/2423116.html

3、加载插件Activity并实现应用内跳转 http://blog.csdn.net/singwhatiwanna/article/details/22597587 的代码整理 https://github.com/BlogForMe/DynamicLoadMe dynamicloadclient和dynamicloadhost部分

二、动态加载dex文件

:问题 在Ubuntu git clone(https://github.com/BlogForMe/dynamic-load-apk) 这个项目,然后open之后,报出这样一个错误 Error:Failed to open zip file. Gradle’s dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network) Re-download dependencies and sync project (requires network)

我的解决方案: 到home 目录下 ctrl + H,然后出现.gradle隐藏文件,删除 ,然后让studio重新编译。 配置好后要是还跑不了,把Intant Run关了

参考:http://www.cnblogs.com/jycboy/p/5954220.html http://stackoverflow.com/questions/39675848/unsupportedmethodexception-android-studio-2-2

http://www.cnblogs.com/over140/archive/2011/11/23/2259367.html http://blog.csdn.net/singwhatiwanna/article/details/22597587


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表