VirtualAPK的简单使用
VirtualApk引入步骤:
一、宿主应用引入VirtualApk
1、在项目的build.gradle文件中加入依赖:
dependencies {
classpath 'com.didi.virtualapk:gradle:0.9.8.6'
}
完整的gradle文件如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.didi.virtualapk:gradle:0.9.8.6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、在app的build.gradle文件中加入依赖:
apply plugin: 'com.didi.virtualapk.host'
dependencies {
implementation 'com.didi.virtualapk:core:0.9.8'
}
完整的gradle文件如下:
apply plugin: 'com.android.application'
apply plugin: 'com.didi.virtualapk.host'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.wangyz.virtualapk.host"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.+'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.didi.virtualapk:core:0.9.8'
}
3、新建项目的Application,继承自Application,并在attachBaseContext方法中初始化
public class App extends Application{
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
PluginManager.getInstance(base).init();
}
}
4、在AndroidManifest.xml中引入自定义的Application
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
5、申明权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
二、Plugin中引入VirtualApk
1、在项目的build.gradle文件中加入依赖:
dependencies {
classpath 'com.didi.virtualapk:gradle:0.9.8.6'
}
完整的gradle文件如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.didi.virtualapk:gradle:0.9.8.6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、在app的build.gradle文件中加入依赖:
apply plugin: 'com.didi.virtualapk.plugin'
virtualApk{
packageId = 0x6f
targetHost = '../../VirtualAPKHost/app'//宿主应用的app模块路径
applyHostMapping = true
}
3、在app的build.gradle文件中加入签名配置
signingConfigs{
release{
storeFile file('../../android.keystore')
storePassword "android"
keyAlias "android"
keyPassword "android"
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
完整的gradle文件如下:
apply plugin: 'com.android.application'
apply plugin: 'com.didi.virtualapk.plugin'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.wangyz.virtualapk.plugin"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs{
release{
storeFile file('../../android.keystore')
storePassword "android"
keyAlias "android"
keyPassword "android"
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.+'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
virtualApk{
packageId = 0x6f
targetHost = '../../VirtualAPKHost/app'
applyHostMapping = true
}
注意:Plugin应用的资源文件不能和宿主的资源文件重名,否则在生成插件APK时会报错:

建议各模块资源命名以模块名开头。
4、生成插件APK
打开gradle窗口,双击assemblePlugin,生成APK

文件生成目录:app/build/outputs/plugin/release/
三、在宿主应用中加载插件APK
1、将生成的插件APK推送(通过网络或者adb等)到手机指定路径,如/sdcard/Plugin.apk。
2、在宿主应用中加载APK
private static final String PLUGIN_PACKAGE_NAME = "com.wangyz.virtualapk.plugin";
private static final String PLUGIN_NAME = "com.wangyz.virtualapk.plugin.MainActivity";
private void loadPlugin() {
try {
String pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/Plugin.apk");
File plugin = new File(pluginPath);
PluginManager.getInstance(this).loadPlugin(plugin);
} catch (Exception e) {
e.printStackTrace();
}
}
3、调用APK中的Activity
public void loadPlugin(View view) {
if (PluginManager.getInstance(this).getLoadedPlugin(PLUGIN_PACKAGE_NAME) == null) {
Toast.makeText(getApplicationContext(), "未加载插件", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent();
intent.setComponent(new ComponentName(PLUGIN_PACKAGE_NAME, PLUGIN_NAME));
startActivity(intent);
}
源码地址:https://github.com/milovetingting/Samples/tree/master/VirtualAPK
VirtualAPK的简单使用的更多相关文章
- 插件化 VirtualAPK 简介 体验 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [置顶]
滴滴插件化VirtualAPK框架原理解析(二)之Service 管理
在前一篇博客滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理 中VirtualAPK是如何对Activity进行管理的,本篇博客,我们继续来学习这个框架,这次我们学习的是如何 ...
- [置顶]
滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理
上周末,滴滴与360都开源了各自的插件化框架,VirtualAPK与RePlugin,作为一个插件化方面的狂热研究者,在周末就迫不及待的下载了Virtualapk框架来进行研究,本篇博客带来的是Vir ...
- 滴滴插件化方案 VirtualApk 源码解析
那么其中的难点很明显是对四大组件支持,因为大家都清楚,四大组件都是需要在AndroidManifest中注册的,而插件apk中的组件是不可能预先知晓名字,提前注册中宿主apk中的,所以现在基本都采用一 ...
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- Fabio 安装和简单使用
Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
- 在Openfire上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
随机推荐
- 【转】搭建自己的 sentry 服务
1. 安装 docker 首先要确认你的 Ubuntu 版本是否符合安装 Docker 的前提条件.如果没有问题,你可以通过下边的方式来安装 Docker : 使用具有 sudo 权限的用户来登录你的 ...
- IM进化论:腾讯也难逃被颠覆掉的命运
在一定程度上,腾讯代表了中国IM领域的过去和未来.但有句俗话,后来推前浪,前浪被拍死在沙滩上,"生死腾讯"也总会变为生和死,因为腾讯很可能会有被颠覆掉的一天.腾讯的IM接口是个庞然 ...
- 由于github仓库中提前建立readme文件,导致git push报错error: failed to push some refs to 'git@github.com:
$ git push -u origin master To git@github.com:xxx/xxx.git ! [rejected] master -> master (fetch fi ...
- Python基础之数据类型、变量、常量
数据类型 整数:任意大小的整数,十六进制用0x前缀 浮点数:浮点数也就是小数,科学计数法1.23x109就是1.23e9,0.000012可以写成1.2e-5 字符串:以单引号'或双引号"括 ...
- 第五章——支持向量机(Support Vector Machines)
svm可用于线性或非线性分类.回归.甚至异常检测. svm尤其适用于中小数据集的复杂分类问题. 5.1 Linear SVM Classification svm对feature scales敏感,如 ...
- Axios 中文说明
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http ...
- 基于.net core实现项目自动编译、并生成nuget包
近期想对自己的项目增加自动编译并生成nuget包,网上资料不少.但总还有迷糊的时候.首先:此解决方案包含多种版本的项目,如:有编译必须是x86平台,以及还有传统的.net foramework项目,以 ...
- web页面中http返回的状态码解释
状态码类别: 1xx: 信息类,表示客户发送的请求服务端正在处理 2xx:成功类,服务器 成功接收请求 3xx:重定向类,服务器中找到了多个请求内容,则需要用户再次操作选择 4xx:客 ...
- 你不知道的JavaScript--Item10 闭包(closure)
JavaScript 闭包究竟是什么? 用JavaScript一年多了,闭包总是让人二丈和尚摸不着头脑.陆陆续续接触了一些闭包的知识,也犯过几次因为不理解闭包导致的错误,一年多了资料也看了一些,但还是 ...
- java集合框架之ArrayList
参考http://how2j.cn/k/collection/collection-arraylist/363.html 使用数组的局限性 一个长度是10的数据:Hero[] heroArr=new ...