滴滴出品的安卓插件框架 – VirtualAPK

VirtualAPK is a powerful yet lightweight plugin framework for Android. It can dynamically load and run an APK file (we call itLoadedPlugin) seamlessly as an installed application. Developers can use any Class, Resources, Activity, Service, Receiver and Provider inLoadedPluginas if they are registered in app’s manifest file.

Supported Features

Feature Detail
Supported components Activity, Service, Receiver and Provider
Manually register components in AndroidManifest.xml No need
Access host app classes and resources Supported
PendingIntent Supported
Supported Android features Almost all features
Compatibility Almost all devices
Building system Gradle plugin
Supported Android versions API Level 15+

Getting started

Host Project

Add a dependency inbuild.gradlein root of host project as following.

dependencies {
    classpath 'com.didi.virtualapk:gradle:0.9.0'
}

Apply plugin in application module ofbuild.gradle.

apply plugin: 'com.didi.virtualapk.host'

Compile VirtualAPK in application module ofbuild.gradle.

compile 'com.didi.virtualapk:core:0.9.0'

InitializePluginManagerinYourApplication::attachBaseContext().

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    PluginManager.getInstance(base).init();
}

Modify proguard rules to keep VirtualAPK related files.

-keep class com.didi.virtualapk.internal.VAInstrumentation { *; }
-keep class com.didi.virtualapk.internal.PluginContentResolver { *; }

-dontwarn com.didi.virtualapk.**
-dontwarn android.content.pm.**
-keep class android.** { *; }

Finally, load an APK and have fun!

String pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Test.apk");
File plugin = new File(pluginPath);
PluginManager.getInstance(base).loadPlugin(plugin);

// Given "com.didi.virtualapk.demo" is the package name of plugin APK, 
// and there is an activity called `MainActivity`.
Intent intent = new Intent();
intent.setClassName("com.didi.virtualapk.demo", "com.didi.virtualapk.demo.MainActivity");
startActivity(intent);

Plugin Project

Add a dependency inbuild.gradlein root of plugin project as following.

dependencies {
    classpath 'com.didi.virtualapk:gradle:0.9.0'
}

Apply plugin in application module ofbuild.gradle.

apply plugin: 'com.didi.virtualapk.plugin'

Config VirtualAPK. Remember to put following lines at the end ofbuild.gradle.

virtualApk {
    packageId = 0x6f             // The package id of Resources.
    targetHost='source/host/app' // The path of application module in host project.
    applyHostMapping = true      // [Optional] Default value is true. 
}

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:滴滴出品的安卓插件框架 – VirtualAPK