如下是我这个系列的相关文章,有兴趣能够参考一下,能够给个喜欢或者关注个人文章。 [Android]如何作一个崩溃率少于千分之三噶应用app--章节列表java
国内的插件化出不了海,而Google终于也出了组件化和插件化的模型了,这一节就是带你感觉一下来自官方的威力。 1.只有上线google市场的应用才能使用。 2.先下载Android Studio 3.3吧,gradle会默认使用最新的4.9,骚年 不符合以上的条件的同窗,请自动略过吧android
1.初始下载的大小更加小 2.能够只下载地区资源 3.安装更加快 4.能够动态更新json
1.手机要有google store和google play 2.须要上传你的签名文件到google play 3.最低版本支持api 21 4.低于api21的第一次下载就会下载完整包,会优化地区和资源配置,可是没法作到动态更新 5.base app不能大于100M,动态更新的aab文件不要大于10M,并且最好要有下载提示api
1.Base App 首次安装到手机的资源和文件,基础的dex资源bash
2.Configuration APKs native libraries 和适配当前手机屏幕分辨率的资源网络
3.Dynamic feature APKs 不须要在首次安装就加载的模块,动态加载模块,打包后是.aab后缀的文件。架构
操做基础介绍 动态更新的module必须使用Dynamic Feature Module app
默认就是选择Android 5.0版本 ide
应用于app商店使用下载和安装动态功能的模块,若是下载的机型低于api21,会直接全量下载动态模块。 组件化
建立后生成的AndroidManifest.xml文件,其中dist中的内容是用于动态更新配置的。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.hotflyer.bussiness">
<dist:module
dist:onDemand="true"
dist:title="@string/title_bussiness">
<dist:fusing dist:include="true" />
</dist:module>
</manifest>
复制代码
1.最重要使用到的库是play:core,这个库是使用动态更新提供接口
api 'com.google.android.play:core:1.3.0'
复制代码
2.依赖关系如图
// Create request to install a feature module by name.
val request = SplitInstallRequest.newBuilder()
.addModule(name)
.build()
// Load and install the requested feature module.
manager.startInstall(request)
复制代码
3.添加加载监听
/** Listener used to handle changes in state for install requests. */
private val listener = SplitInstallStateUpdatedListener { state ->
val multiInstall = state.moduleNames().size > 1
state.moduleNames().forEach { name ->
// Handle changes in state.
when (state.status()) {
SplitInstallSessionStatus.DOWNLOADING -> { //网络拉取动态模块
// In order to see this, the application has to be uploaded to the Play Store.
displayLoadingState(state, "Downloading $name")
}
SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> { //须要用户确认选项(如更新play商店)
/*
This may occur when attempting to download a sufficiently large module.
In order to see this, the application has to be uploaded to the Play Store.
Then features can be requested until the confirmation path is triggered.
*/
startIntentSender(state.resolutionIntent().intentSender, null, 0, 0, 0)
}
SplitInstallSessionStatus.INSTALLED -> { //成功下载回调
onSuccessfulLoad(name, launch = !multiInstall)
}
SplitInstallSessionStatus.INSTALLING -> //安装中
displayLoadingState(state, "Installing $name")
SplitInstallSessionStatus.FAILED -> { //安装失败
Log.e(TAG, "Error: ${state.errorCode()} for module ${state.moduleNames()}")
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
manager = SplitInstallManagerFactory.create(this) //拆分app管理
initializeViews()
}
override fun onResume() {
// Listener can be registered even without directly triggering a download.
manager.registerListener(listener) //注册监听
super.onResume()
}
override fun onPause() {
// Make sure to dispose of the listener once it's no longer needed. manager.unregisterListener(listener) //销毁监听 super.onPause() } 复制代码
4.跳转到相应关系,由于依赖关系的问题,只能经过包名跳转。
/** Launch an activity by its class name. */
private fun launchActivity(className: String) {
Intent().setClassName(packageName, className)
.also {
startActivity(it)
}
}
复制代码
可是熟悉跳转逻辑,应该会明白这种状况,能够作一个适配的路由也是能够正常跳转的。
5.还能动态移除模块
/** Request uninstall of all features. */
private fun requestUninstall() {
toastAndLog("Requesting uninstall of all modules." +
"This will happen at some point in the future.")
val installedModules = manager.installedModules.toList()
manager.deferredUninstall(installedModules).addOnSuccessListener {
toastAndLog("Uninstalling $installedModules")
}
}
复制代码
6.须要注意的是动态添加native so须要使用SplitInstallHelper.loadLibrary加载
SplitInstallHelper.loadLibrary(this, "hello-jni")
复制代码
7.base module中须要配置dynamicFeature,才会编译AndroidManifest.xml中方向合并Dynamic library的AndroidManifest.xml
android{
dynamicFeatures = [':features:kotlin',
':features:java',
':features:native',
':features:assets', ":bussiness"]
}
复制代码
在build/intermediates/merged_manifests中能够找到合并的AndroidManifest
8.在build/intermediates/feature_set_metadata中能够看到feature-metadata.json 上面是有一些Dynamic library的信息的
9.play core的库是通过混淆的,除了个别几个对外使用的文件外,所有混淆,部分原理只能参照国内的插件化了。 提供了SplitCompat.install的方式安装,可是最低只兼容到api19,即4.4
10.须要配置Application,有两种方式,继承SplitCompatApplication,或者使用SplitCompat.install(this)
11.编译方式如图,会生成.aab格式的文件
1.由于会将四大组件信息都预先注册到合并的base AndroidManifest当中,那么没法新增四大组件,暂时不能像国内这样热更新。 2.比较适合使用的场景是热修复和地区适配场景。 3.动态library也是须要上传给google审核的
相比于普通的组件化架构,其启动入口是从base 的Application当中,其余的动态模块都须要下载后才能使用。
那么就须要一个启动加载画面下载App module和重要的module,而后再使用后台下载加载其余内容,而后模块跳转前,须要捕获异常和预断定模块加载是否完成,保证程序不会崩溃。
……2018.7.23更新…… google store 发布应用,须要选择是否使用App Bundle,若是使用了,那么将会从新去打包文件。会形成,你在google市场上下载的包和在其余市场中下载的包,包签名不一致,没法覆盖安装。若是在国内渠道发布,将没法用google市场中的app覆盖安装。 若是保持签名选退出计划,若是使用Android app bundle 选继续
这个只有一次机会,选择以后,不会存在更改了,因此请慎重。
群1已满,能够进群2学习组件化