Running multiple applications on a shared process

2019-06-11 16:19发布

问题:

I have a set of applications that run in the same process. These processes have a shared libary, the ActionBarSherlock so that they have the same UI accross different versions of Android. The first time i access elements of the ActionBarSherlock library everything works fine. But when i access the same elements from a different application on the same Android process, i get an error like this:

E/AndroidRuntime(  797): java.lang.RuntimeException: Unable to start activity \
    ComponentInfo{xper.tristram/xper.tristram.XperTristramActivity}: \
    java.lang.ClassCastException: xper.common.CommonView cannot be cast to xper.common.CommonView
E/AndroidRuntime(  797):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
E/AndroidRuntime(  797):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
E/AndroidRuntime(  797):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(  797):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
E/AndroidRuntime(  797):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  797):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  797):    at android.app.ActivityThread.main(ActivityThread.java:4340)
E/AndroidRuntime(  797):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  797):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  797):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(  797):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(  797):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  797): Caused by: java.lang.ClassCastException: \
    xper.common.CommonView cannot be cast to xper.common.CommonView
E/AndroidRuntime(  797):    at xper.tristram.XperTristramActivity.onCreate(XperTristramActivity.java:29)
E/AndroidRuntime(  797):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(  797):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(  797):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
E/AndroidRuntime(  797):    ... 11 more

The above block code is copy pasted from this site. A colleague of mine found out this site that describes the root of the problem which is that there are two different instances of the loaded class of the shared library by the two Class Loaders one for each application. The author of the site provides a workaround, that is to override of the creation of shared views by the LayoutInflater, using the method LayoutInflater.setFactory(). My question is, if there is any other solution that doesn't require to override core methods of the Android library which in my opinion can be error prone and not efficient.

Thanks in advance:)