在android 插件化技术中(宿主app和插件app设置相同的shareduserid),动态加载apk有两种方式:
下面介绍几种常见的方法获取资源以及代码的方法。
try { context = createpackagecontext("com.test.resource", context.context_include_code | context.context_ignore_security); textview.settext(context.getresources().gettext(r.string.message)); } catch (namenotfoundexception e) { e.printstacktrace(); }
新建一个获取资源的接口,传入插件apk的路径返回resources对象
// 获取插件apk的resources对象 public resources getbundleresource(context context, string apkpath) { assetmanager assetmanager = createassetmanager(apkpath); return new resources(assetmanager, context.getresources().getdisplaymetrics(), context.getresources().getconfiguration()); } private assetmanager createassetmanager(string apkpath) { try { assetmanager assetmanager = assetmanager.class.newinstance(); assetmanager.class.getdeclaredmethod("addassetpath", string.class) .invoke(assetmanager, apkpath); return assetmanager; } catch (throwable th) { th.printstacktrace(); } return null; }
获得了resource 对象之后,就可以通过函数resources.getdrawable、resources.getstring、resources.getlayout 获取图片、字符串、布局文件了。
如对本文有疑问, 点击进行留言回复!!
android RecyclerView checkbox复用解决
Android Q Input (1) -- InputManagerService启动
android -- ndk (stack corruption detected)
网友评论