简体   繁体   中英

Android application plug-in architecture

I am experimenting with implementing plug-in architecture in Android, more specifically, cross-apk class loading

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

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. 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 ; 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...

  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. This approach looks promising, as I can load class IB and cast it to I without problems. However, it creates the need for copying the jar, and I am not sure what will happen when NDK shared library is involved.

My question is, is there an approach that involves no modification of build.xml/ant.properties and works with NDK so library?

Not sure about other approaches, but for approach 2, you can exclude interface I when you were packing B to apk. Just exclude I.class file generated is enough.

You may find this post by Fred Chung helpful and may give you some hint. http://android-developers.blogspot.kr/2011/07/custom-class-loading-in-dalvik.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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