简体   繁体   English

Android应用程序插件架构

[英]Android application plug-in architecture

I am experimenting with implementing plug-in architecture in Android, more specifically, cross-apk class loading 我正在尝试在Android中实现插件架构,更具体地说是跨Apk类加载

assume I have the following: apk A and apk B, with the same sharedUserId defined in AndroidManifest.xml interface I is defined in apk A, and apk B contains a class IB that implements I 假设我具有以下内容:apk A和apk B,在AndroidManifest.xml 接口中定义了相同的sharedUserId在apk A中定义了,而apk B包含实现了I类IB

I tried the following approaches 我尝试了以下方法

  1. in apk A, use createPackageContext to obtain context of B, then call Context.getClassLoader and load the desired class name. 在apk A中,使用createPackageContext获取B的上下文,然后调用Context.getClassLoader并加载所需的类名称。 However, due to the fact that this creates two class loaders, and therefore I cannot cast the class IB loaded in B into interface I ; 但是,由于这会创建两个类加载器,因此我无法将B中加载的类IB强制转换为接口I which means I need to use reflection... 这意味着我需要使用反射...

  2. pass apk B to DexClassLoader (path to apk B obtained by ApplicationInfo.sourceDir ) and failed with " Class resolved by unexpected DEX ", probably because there is a duplicate interface I in apk B as well... 将apk B传递到DexClassLoader (由ApplicationInfo.sourceDir获得的apk B的路径),并由于“ 类由意外的DEX解决 ”而失败,可能是因为apk B中也存在重复的接口I ...

  3. modify build xml and follow the approach in Custom Class Loading in Dalvik to create a separate jar containing the class IB in B which implements I and put the jar into apk B's assets directory. 修改build xml并按照Dalvik自定义类加载中的方法创建一个单独的jar,其中包含B中的IB类 ,该jar实现I ,并将该jar放入apk B的assets目录中。 This approach looks promising, as I can load class IB and cast it to I without problems. 这种方法看起来很有希望,因为我可以加载IB类并将其强制转换为I。 However, it creates the need for copying the jar, and I am not sure what will happen when NDK shared library is involved. 但是,它需要复制jar,因此我不确定在涉及NDK共享库时会发生什么。

My question is, is there an approach that involves no modification of build.xml/ant.properties and works with NDK so library? 我的问题是,有没有一种方法无需修改build.xml / ant.properties并可以与NDK so库一起使用?

Not sure about other approaches, but for approach 2, you can exclude interface I when you were packing B to apk. 不确定其他方法,但是对于方法2,可以在将B包装为apk时排除接口I。 Just exclude I.class file generated is enough. 仅排除生成的I.class文件就足够了。

You may find this post by Fred Chung helpful and may give you some hint. 您可能会发现Fred Chung的这篇文章很有帮助,并可能给您一些提示。 http://android-developers.blogspot.kr/2011/07/custom-class-loading-in-dalvik.html http://android-developers.blogspot.kr/2011/07/custom-class-loading-in-dalvik.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM