欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

VirtualAPK的简单使用

程序员文章站 2022-04-28 21:53:35
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时会报错:

VirtualAPK的简单使用

建议各模块资源命名以模块名开头。

4、生成插件apk

打开gradle窗口,双击assembleplugin,生成apk

VirtualAPK的简单使用

文件生成目录: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