Android插件化开发

Android插件化开发和组件化开发其实是类似的,都是为了便捷开发,提高效率。但插件化的一个好处就是可以实现在线下载更新安装插件,类似热更新的功能,而热更新的classloder技术在插件化中也是关键点。

主要插件化框架:

2019年  爱奇艺qisaw 腾讯shadow

2017年 滴滴virtualapk 360 Replugin

之前还有ali altas Driodplugin等等

主要的技术我觉得是类似的,shadow号称0HOOK,但aiqiyi说看了企鹅的源码,是有hook的,有兴趣可以看看

主要尝试了shadow和replugin,因为对比下来这两种用户广泛,而且兼容性好些

shadow我down了源码和demo,能够很好的运行,但对已有项目的更改可能稍多,同时调研了replugin,感觉replugin的集成更加快捷,先行记录

集成replugin

1.由于兼容或资源问题,宿主的工程添加

maven{url "https://dl.bintray.com/soli/maven"}

2.添加RePlugin Host Gradle依赖

classpath 'com.qihoo360.replugin:replugin-host-gradle:2.3.1'

3.宿主的app gradle

implementation 'com.qihoo360.replugin:replugin-host-lib:2.2.1' // 集成 RePlugin 添加的配置

4.

一是继承RePluginApplication

public class App extends RePluginApplication 

二是非继承的方式

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);
        ....
    }
}

5.plugin app

buildscript {
    dependencies {
        classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.2.4'
        ...
    }
}
apply plugin: 'replugin-plugin-gradle'

dependencies {
    compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.4'
    ...
}

6.plugin apk存放路径 

只需两步即可:

  1. 将APK改名为:[插件名].jar
  2. 放入主程序的assets/plugins目录

好了,准备打开插件吧 

Intent intent = RePlugin.createIntent("demo2",
    "com.qihoo360.replugin.sample.demo2.databinding.DataBindingActivity");
context.startActivity(intent);

 

到此replugin就集成完了,能正常启动app中的apk了,但是可能运行中遇到各种问题,

1.比如replugin不支持java8,因此你的插件项目如果使用了java8的新特性,如lambad表达式等,就需要进行改造了

2.项目必须包含v4或者v7包,否则会报错,replugin用到了包里的loadmanager

3.gradle版本问题,如果遇到莫名其妙的问题,考虑gradle版本和replugin版本的问题

还有很多设置,比如插件别名,多进程,外置插件等等,这些都简单些了,看需要研究,最主要的是能集成,跑起来。

我demo的源码:

宿主:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven{url "https://dl.bintray.com/soli/maven"}
        jcenter() {url 'https://maven.aliyun.com/repository/jcenter'}
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'

        // 1、添加RePlugin Host Gradle依赖
        classpath 'com.qihoo360.replugin:replugin-host-gradle:2.3.1'
        
        // 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
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.ccccit.plugintest"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
}

apply plugin: 'replugin-host-gradle'// 集成 RePlugin 添加的配置

// 集成 RePlugin 添加的配置
repluginHostConfig {
    useAppCompat = true // 如果项目需要支持 AppComat,则需要将此配置置为 true
    // 如果应用需要个性化配置坑位数量,则需要添加以下代码进行配置
    //    countNotTranslucentStandard = 6
    //    countNotTranslucentSingleTop = 2
    //    countNotTranslucentSingleTask = 3
    //    countNotTranslucentSingleInstance = 2
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:support-v4:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.qihoo360.replugin:replugin-host-lib:2.2.1' // 集成 RePlugin 添加的配置
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript { 
    ext.kotlin_version = '1.3.50'

    repositories {
        maven {url "https://dl.bintray.com/soli/maven"}

        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'

        // 1、添加RePlugin Host Gradle依赖(主工程用)
        classpath 'com.qihoo360.replugin:replugin-host-gradle:2.2.1'
        // 2、添加RePlugin Plugin Gradle依赖(插件工程用)
        classpath 'com.qihoo360.replugin:replugin-plugin-gradle:2.3.1'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.ccccit.pluginone"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
}

apply plugin: 'replugin-plugin-gradle'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

/*repluginPluginConfig {
    //插件名
    pluginName = "pluginone"
    //宿主app的包名
    hostApplicationId = "com.ccccit.plugintest"
    //宿主app的启动activity
    hostAppLauncherActivity = "com.ccccit.plugintest.TestActivity"
}*/

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:support-v4:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.qihoo360.replugin:replugin-plugin-lib:2.2.1' // 集成 RePlugin 添加的配置

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

 

 


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值