VirtualAPK is an excellent plug-in framework developed by Didi Chuxing
Let’s cut the crap and just masturbate
-
Reservoir engineering
- Start by creating a host project, which is a normal project, because I recently learned kotlin, so the demo is written based on Kotlin
- Add project dependencies to your project’s build.gradle
dependencies { classpath 'com. Android. Tools. Build: gradle: 2.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com. Didi. Virtualapk: gradle: 0.9.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } Copy the code
- Add dependencies in the app folder called build.gradle
apply plugin: 'com.didi.virtualapk.host' compile 'com. Didi. Virtualapk: core: 0.9.1' Copy the code
override fun onCreate() {
super.onCreate()
println("onCreate")
}
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
println("attachBaseContext")
PluginManager.getInstance(this)
.init()
}
}
Copy the code
- Write the activity test code
Let’s just test two buttons
Let’s wrap up and write the plug-in project
-
The plug-in project
- Creating a plug-in project is also a normal project
-
Engineering build. Add the classpath gradle ‘com. Didi. Virtualapk: gradle: 0.9.4’
dependencies { classpath 'com. Android. Tools. Build: gradle: 2.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com. Didi. Virtualapk: gradle: 0.9.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } Copy the code
-
Under the app build. Gradle add apply plugin: ‘com. Didi. Virtualapk. Plugin’
-
VirtualAPK builder
The position marked 1 in the figure is filled with the host project APP file. Since my host project and plug-in project are no longer in the same folder, I use the local absolute path. You can fill it by yourself according to the actual situation
PackageId = 0x6f // The path of the host project Application module, Plugin builds depend on this path targetHost = '.. /.. /VirtualAPK/app' // Defaults to true, if the plugin has a class that references the host, ApplyHostMapping = true} Note: Gradle is recommended for plugin building 2.14.1 version,3.x version may have adaptation problems, the gradle3.0.1 version I used before was not built, it is OK after 2.3.3Copy the code
Please refer to the official Wiki VirtualAPK README for more details
Overview of the VirtualAPK Builder API
- Once the plug-in project is configured, the next step is to package the plug-in
Find the plug-in APK in the Build folder
-
-
A moment of wonder
-
Change the name of the plugin apk to test. apk and push it to the sd card root of your phone (don’t forget to apply for SD card permission for phones above 6.0).
-
The host project activity adds three methods, as well as click events
/ / check whether there is a plug-in private fun existsApk () : a Boolean {val apk = File (Environment. External.getexternalstoragedirectory (),"Test.apk") returnApk.exists ()} private fun loadPlugin(base: Context) { val pluginManager = PluginManager.getInstance(base) val apk = File(Environment.getExternalStorageDirectory(),"Test.apk") println("Does it exist?" + apk) if(apk.exists()) { try { pluginManager.loadPlugin(apk) } catch (e: Exception) {e.printStackTrace()}}} // Jump to plugin method private funjump() {// The package name of the plug-in val PKG ="com.bidostar.virtualplugindemo" if (PluginManager.getInstance(this).getLoadedPlugin(pkg) == null) { Toast.makeText(this, "plugin [com.bidostar.virtualplugindemo] not loaded", Toast.LENGTH_SHORT).show() returnIntent.setclassname (this, intent.setClassName) intent.setClassName(this, intent.setClassName)"com.bidostar.virtualplugindemo.PluginActivity") startActivity(intent) } Copy the code
-
It’s a mule or a horse. Pull it out
-
Afterword.
The VirtualAPK constructor is not configured correctly, especially the host project directory targetHost is not configured correctly. The plugin project activity name and layout file name and the host project have the same name, also can not load the plugin APK * 6.0 Mobile phone does not apply for SD card permission, causing failure to load plug-in APK *... So when writing, we should be careful. We should read the official documents first and pay attention to some detailsCopy the code
The official gihub
Frequently asked Questions