RePlugin外置插件化的实现和踩过的坑

        首先说明的是RePlugin的插件化分为,内置和外置两种方式,感觉区别不是非常大,内置的插件经过升级之后实际上和外置插件是一样的。主要考虑到后期的热更新热修复等技术的应用,本文探讨外置插件化的实现。

        第一,就是宿主插件的导入:直接上代码

appModule目录build.gradle

apply plugin: 'com.android.application'

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.replugin.hostapplication"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        applicationId "com.qihoo360.replugin.sample.host"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

// apply语句必须放置到android标签之后,以读取applicationId属性
apply plugin: 'replugin-host-gradle'

repluginHostConfig{
    useAppCompat = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.qihoo360.replugin:replugin-host-lib:2.2.4'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

注意:applicationId是一定需要的,否则会出错,此处的applicationId不是清单文件中的哪一个。

apply plugin: 'replugin-host-gradle'需要在android{}之后,防止编译的时候找不到Id

然后我们继续在根目录下的build.gradle中加入

classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'  个人使用的仓储
maven {
    url  "https://dl.bintray.com/qihoo360/replugin"
}具体配置如下:
buildscript {
    
    repositories {
        google()
        jcenter()
        maven {
            url  "https://dl.bintray.com/qihoo360/replugin"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url  "https://dl.bintray.com/qihoo360/replugin"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

接下来呢,不要忘记了读写权限,因为宿主插件需要对其他的插件进行读取和写入等操作


当然了,敏感的权限在Android6以上需要动态申请,这个自己去百度,这里就不多说了。(否则会出现无法install的情况)

接下来就是宿主插件的Application了,可以采用继承和非集成的方式,本文采用的是非集成方式

package com.replugin.hostapplication;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import com.qihoo360.replugin.RePlugin;

public class MainApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        RePlugin.App.attachBaseContext(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        RePlugin.App.onCreate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();

        /* Not need to be called if your application's minSdkVersion > = 14 */
        RePlugin.App.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        /* Not need to be called if your application's minSdkVersion > = 14 */
        RePlugin.App.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);

        /* Not need to be called if your application's minSdkVersion > = 14 */
        RePlugin.App.onConfigurationChanged(config);
    }
}

就宿主插件而言,最后就只剩下了最基本的对其他插件的调用了,简单的写一下 下载,调用,卸载三个方法的调用

public void install(View view){
    PluginInfo pluginInfo = RePlugin.install(Environment.getExternalStorageDirectory().getPath().toString()+"/plugin1.apk");
    Log.d(TAG, pluginInfo + "");

}
public void openActivity(View view){
    try{
        RePlugin.startActivity(this,RePlugin.createIntent("plugin1", "com.replugin.replugindex1.MainActivity"));
    }catch (Exception e){
        e.printStackTrace();
    }
}
public void unInstall(View view){
    RePlugin.uninstall("/sdcard/plugin1.apk");
}

这里说明一下sdcard这个路径和Environment.getExternalStorageDirectory().getPath().toString()一个意思,就是你把手机连接上电脑以后打开的第一个目录就是,也就是文件管理的第一个界面,只要你在电脑上把apk丢进去就可以找到了。说明一下,放在这里的插件apk在install之后就会消失,因为已经被放到调用的目录了data/data/包名/app_p_a(自己脑补)

现在说一下其他插件的接入:

同理先说明

根目录下的build.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.qihoo360.replugin:replugin-plugin-gradle:2.2.4'

        // 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
}

app目录下的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.replugin.replugindex1"
        minSdkVersion 19
        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'
        }
    }
}

apply plugin: 'replugin-plugin-gradle'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

将插件工程build一下在output中找到apk,改名和别名一样,后缀名apk,然后放到sdcard目录下即可。自定义和动态加载和管理,后期会更新。欢迎大家指正

代码zip包地址(包含宿主插件和其他插件的工程)

https://download.csdn.net/download/qq_20369621/11233030

你可能感兴趣的:(android知识,插件化)