Search code examples
androidpluginsviewapkadd-on

Create Plugins/Add-ons/Extentions in separate Apk for an App


I should create a "Container" app with basic features.

In the "Container" app I have a NavigationView (the items are dynamically added based on the installed plugins) and a FrameLayout.

In a separate apk I should create a plugins that add features to my app, with my own view.

the problem is: how do I create a Fragment with a View in the Plugins app and move them to the Context app's FrameLayout.

Here are two images that explain the situation better

enter image description here

enter image description here

Does anyone have any example code? Link to tutorial? or tell me how can I do it?

I tried to create an AIDL for communication between the processes but only pass primitive data, it is not allowed to pass: objects, resources, images etc.


Solution

  • It is possible, there are two things needed when you trying to load an apk: dex codes & resources.

    with dex codes, what you can do is modify the default ClassLoader of the current context. add separated ClassLoader for each of your plugins, and put them into context's ClassLoader, and modify context's ClassLoader's findClass method, make it find class from plugin's ClassLoader first, if not found then fallback to traditional ClassLoader way.I think PathClassLoader will do the trick, what you have to do is pass through plugin's file path to it.

    with resources, what you need to do is get the old AssetManager from Resources class, and call addAssetPath with reflection, and pass plugin's apk path to it.

    with these two steps done, you'll have a plugin and all its classes and resources run no much different from a regular app.

    ps: notices that there's still much work to do to make it work properly on different system verion and different custom rom, and these two steps won't let you add new activity/service/broadcast/provider from plugin, cause you'll need to register them to SystemServer, and that's restricted by the system, I don't think there's a way to do it without root.

    --------------------------- I am a separator ----------------------------

    play-store will not allow your app to publish if your app has the ability to run codes that can be download from the internet. cause there's no guarantee that those codes will collect private data from somebody's phone or not, or do anything illegal.