插件化之VirtualAPK框架基本使用

2021-07-07  本文已影响0人  戎码虫

一、插件化框架选择

DynamicAPK、VirtualApp、DroidPlugin等插件化框架,经过比较选择VirtualAPK支持四大组件,支持插件宿主之间的交互,兼容性强,对Android适配比较好,所以选择它作为插件化框架;优缺点对比可参考博客插件化篇 - 插件化框架对比

二、VirtualAPK集成

VirtualAPK github 地址

2.1 Host APP宿主集成
在项目的build.gradle文件中加入依赖
dependencies {
    classpath 'com.didi.virtualapk:gradle:0.9.8.6'
}
在app的build.gradle文件中加入依赖
apply plugin: 'com.didi.virtualapk.host'

dependencies {
    implementation 'com.didi.virtualapk:core:0.9.8'
}
新建项目的Application,继承自Application,并在attachBaseContext方法中初始化
public class AppAplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        PluginManager.getInstance(base).init();
    }
}
在AndroidManifest.xml中
<application
        android:name=".AppAplication"
...
 </application>
混淆文件proguard-rules.pro
-keep class com.didi.virtualapk.internal.VAInstrumentation { *; }
-keep class com.didi.virtualapk.internal.PluginContentResolver { *; }

-dontwarn com.didi.virtualapk.**
-dontwarn android.**
-keep class android.** { *; }
2.2 Plugin APP插件集成
在项目的build.gradle文件中加入依赖
dependencies {
    classpath 'com.didi.virtualapk:gradle:0.9.8.6'
}
在app的build.gradle文件中加入依赖
apply plugin: 'com.didi.virtualapk.plugin'
virtualApk{
    packageId = 0x6f  //插件资源id,避免资源id冲突
    targetHost = '../APP_Plug/app_host' //宿主应用的app模块路径
    applyHostMapping = true // 【选填】插件编译时是否启用应用宿主的apply mapping
}
插件流程图

https://blog.csdn.net/yyh352091626/article/details/74852390

三、生成插件APK

插件apk签名配置

 signingConfigs {
    release {
      //keystore文件的存放位置
      storeFile file('../debug.keystore')
      //keystore密码
      storePassword 'android'
      //Key别名
      keyAlias 'androiddebugkey'
      //Key密码
      keyPassword 'android'
    }
    
  }

  buildTypes {
    release {
      minifyEnabled false
      signingConfig signingConfigs.release
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }

打开gradle窗口,双击assemblePlugin,生成APK


插件文件

插件apk生成目录:app/build/outputs/plugin/release/

四、在宿主应用中加载插件APK

将插件生成的app_plug-release.apk拷贝到相应的主app目录下,加载代码如下

public class MainActivity extends AppCompatActivity {
  private final String TAG = "MainActivity";
  boolean isSuccess = false;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        == PackageManager.PERMISSION_GRANTED) {
    } else {
      ActivityCompat.requestPermissions(this, new String[] {
          Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
      }, 100);
    }

    //String pluginPath = Environment.getExternalStorageDirectory().getAbsolutePath().concat("/app_plug-release.apk");
    File plugin = new File(this.getFilesDir(), "app_plug-release.apk");
    if (plugin.exists()) {
      Log.e(TAG, "'exists'");
      try {
        PluginManager.getInstance(this).loadPlugin(plugin);
        isSuccess = true;
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    Button viewById = findViewById(R.id.btn_skip);
    viewById.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        if (isSuccess) {
          if (PluginManager.getInstance(MainActivity.this).getLoadedPlugin("com.zj.plugapp")
              == null) {
            Toast.makeText(getApplicationContext(), "加载插件失败", Toast.LENGTH_SHORT).show();
            return;
          }
          // Given "com.didi.virtualapk.demo" is the package name of plugin APK,
          // and there is an activity called `MainActivity`.
          Intent intent = new Intent();
          intent.setClassName("com.zj.plugapp", "com.zj.plugapp.HomeActivity");
          startActivity(intent);
        } else {
          Toast.makeText(MainActivity.this, "初始化插件失败!", Toast.LENGTH_LONG).show();
        }
      }
    });
  }
}

需要注意插件的包名和页面的路径

五、常见的问题

问题一:

由于virtualApk几年没有更新了,对最新的gradle不兼容。
解决:
强制使用以下版本:
https://github.com/didi/VirtualAPK/issues/340

classpath 'com.android.tools.build:gradle:3.1.4'

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
问题二:

目前VirtualAPK不支持Gradle插件3.2.1以上版(不支持AndroidX包)
解决:
gradle.properties 文件中:

android.useDexArchive=false
问题三:

编译报错

> Required entry 'plug_activity_home' but got 'notification_template_part_chronometer', This is seems to unsupport the buildToolsRevision: 28.0.0.

解决:
编译版本小等于28+:

 compileSdkVersion 28
 implementation 'com.android.support:appcompat-v7:28+'
问题四:

运行插件报错

Caused by: android.content.pm.PackageParser$PackageParserException: Package /data/user/0/com.zj.hostapp/files/com.zj.plugapp_20210701095025.apk has no certificates at entry AndroidManifest.xml

解决:
插件签名之后再加载调用

本文Demo github 地址

上一篇下一篇

猜你喜欢

热点阅读