Use external application fragment/activity inside application

前端 未结 3 1048
旧时难觅i
旧时难觅i 2020-12-01 13:25

Is it possible to use a fragment/activity from an external application and use as it is embedded?

For example: embed a PDF reader fragment from a PDF reader applicat

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 14:11

    I use a quite similar approach to sandrstart. Maybe it's less secure. I use a normal Classloader derived from a packagecontext created with the plugin package name. The package names of all plugins are loaded and saved along with other configurations from a config website.

    Context ctx = createPackageContext(packetName, Context.CONTEXT_INCLUDE_CODE | 
                   Context.CONTEXT_IGNORE_SECURITY);
    ClassLoader cl = ctx.getClassLoader();
    Class c = cl.loadClass(className);
    Fragment fragObj = (Fragment)c.newInstance();
    

    But I wanted to stress that my approach and I think sandrstar's approach only work with the android internal android.app.Fragment class.

    I was trying (again) as part for adopting to android 9 to switch from android.app.Fragment (depricated) to android.support.v4.Fragment but could'nt get it to work.

    The reason is that: apk1:framework.class.A == apk2.framework.class.A but apk1:someJarOrAar.class.B != aps2.someJarOrAar.class.B even if the same exact jar/aar is used in both projects. So I will always get a ClassCastException on (Fragment)c.newInstance();.

    I have tried to cast via the classloader from the plugin and cast via classloader from main package but to no avail. I think there is no way around it as it can't be guaranteed that jars/aars are really the same even if they have same names and same classnames in them so it's a security issue to treat them as different even if they are the same.

    I surely hope for some workaround (other than keep on using the android.app.Fragment even under android 9 as I will do for now) but I will also be grateful for comments for why it is definetly not possible with shared classes (such as support.v4.Fragment).

提交回复
热议问题