VirtualApk引入步驟: 一、宿主應用引入VirtualApk 1、在項目的build.gradle文件中加入依賴: dependencies { classpath 'com.didi.virtualapk:gradle:0.9.8.6' } 完整的gradle文件如下: // Top le ...
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