阿里云atlas组件接入

在接入这个之前给各位广大猿友一个建议,一下步骤一步一步来,切勿贪快,亲身血淋淋的经历,接入过程中出现了一万个bug,后面会把相关出现的问题也搞上来并说明处理方法,直接进入正题:

1.目录结构阿里云atlas组件接入

        a.app 是主工程,打包使用

        b.librarybundle:中间bundle,其他module都依赖

        c.localbundle:本地bundle,打包的时候会打进去

        d.remotebundle:远程bundle,打包的时候不会打进去,需要的时候加载出来

2.将gradle配置修改成3.3,具体原因目前不清楚

3.project的build.gradle文件,这个里面有个位置需要注意的就是将classpath修改成阿里的,中间可能会出现gradle不兼容,需要修改

          buildscript {
repositories {
jcenter()
mavenLocal()//本地maven
}
//引入啊里的依赖,不需要再依赖classpath "com.android.tools.build:gradle"的版本,默认使用的是 2.1
dependencies {
classpath "com.taobao.android:atlasplugin:2.3.1.rc9"
}
}
allprojects {
repositories {
jcenter()
mavenLocal()//本地maven
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

4.app的build.gradle文件修改,需要注意不能一次性全部粘贴,这个里面有很多不同点造成的问题,后面我会贴出来我遇到的,如果你一次性粘贴出来会很难排查问题

// 需要放最上面初始化
group = "mmc.atlastest"
version = getEnvValue("versionName", "1.0.0");
def apVersion = getEnvValue("apVersion", "");
apply plugin: 'com.android.application'
apply plugin: 'com.taobao.atlas'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "mmc.atlastest"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
//atlas的依赖
compile('com.taobao.android:atlas_core: [email protected]') {
transitive = true
}
compile 'com.taobao.android:atlasupdate: [email protected]'
compile 'com.alibaba:fastjson: [email protected]'
//项目依赖
compile project(':librarybundle')
compile project(':localbundle')
compile project(':remotebundle')
}
//加入以下配置
atlas {
atlasEnabled true
tBuildConfig {
// autoStartBundles = ['com.android.homebundle'] //自启动bundle配置
outOfApkBundles = ['remotebundle'] //远程module,列表来的,可填多个
preLaunch = 'mmc.atlastest.AtlasLaunch' //AppApplication启动之前调用,这个类下面放出代码
}
patchConfigs {
debug {
createTPatch true
}
}
buildTypes {
debug {
if (apVersion) {
// 打差异补丁 gradlew assembleDebug -DapVersion=1.1.0 -DversionName=1.1.1
// 对应着本地maven仓库地址 .m2/repository/mmc/atlastest/AP-debug/1.1.4/AP-debug-1.1.4.ap
baseApDependency "mmc.atlastest:AP-debug:${apVersion}@ap"
patchConfig patchConfigs.debug
}
}
}
}
String getEnvValue(key, defValue) {
def val = System.getProperty(key);
if (null != val) {
return val;
}
val = System.getenv(key);
if (null != val) {
return val;
}
return defValue;
}
apply plugin: 'maven'
apply plugin: 'maven-publish'
publishing {
// 指定仓库位置
repositories {
mavenLocal()
}
publications {
// 默认本地仓库地址 用户目录/.m2/repository/
maven(MavenPublication) {
//读取ap目录上传maven
artifact "${project.buildDir}/outputs/apk/${project.name}-debug.ap"
//生成本地maven目录
groupId group
artifactId "AP-debug"
}
}
}

 

5.远程bundle和本地bundle的build.gradle,这个的修改量其实不大,也可以一点点添加上去

apply plugin: 'com.android.library'
apply plugin: 'com.taobao.atlas'
atlas {
bundleConfig{
awbBundle true
}
buildTypes {
debug {
baseApFile project.rootProject.file('app/build/outputs/apk/app-debug.ap')
}
}
}
//只添加上面的配置就行了,下面的是默认生成的
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
//依赖lib中间bundle
compile project(':librarybundle')
}

 

6.添加application,记得在manifest中配置进去,可以直接都复制使用这个

public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Atlas.getInstance().setClassNotFoundInterceptorCallback(new ClassNotFoundInterceptorCallback() {
@Override
public Intent returnIntent(Intent intent) {
final String className = intent.getComponent().getClassName();
final String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(className);
if (!TextUtils.isEmpty(bundleName) &;&; !AtlasBundleInfoManager.instance().isInternalBundle(bundleName)) {
//远程bundle
Activity activity = ActivityTaskMgr.getInstance().peekTopActivity();
File remoteBundleFile = new File(activity.getExternalCacheDir(),"lib" + bundleName.replace(".","_") + ".so");
String path = "";
if (remoteBundleFile.exists()){
path = remoteBundleFile.getAbsolutePath();
}else {
Toast.makeText(activity, " 远程bundle不存在,请确定 : " + remoteBundleFile.getAbsolutePath() , Toast.LENGTH_LONG).show();
return intent;
}

PackageInfo info = activity.getPackageManager().getPackageArchiveInfo(path, 0);
try {
Atlas.getInstance().installBundle(info.packageName, new File(path));
} catch (BundleException e) {
Toast.makeText(activity, " 远程bundle 安装失败," + e.getMessage() , Toast.LENGTH_LONG).show();
e.printStackTrace();
}
activity.startActivities(new Intent[]{intent});
}
return intent;
}
});
}
}

 

7.在app新建一个类AtlasLaunch,继承AtlasPreLauncher


public class AtlasLaunch implements AtlasPreLauncher {
@Override
public void initBeforeAtlas(Context context) {
}
}

 

8.修改各个module的内容点

app里面添加三个按钮阿里云atlas组件接入

librarybundle的activity命名为baseactivity,然后localbundle和remotebundle里面都添加一个textview

然后mainactivity里面添加一下代码

//打开远程bundle
public void remote(View view){
Intent intent = new Intent();
intent.setClassName(view.getContext(), "mmc.remotebundle.RemoteActivity");
startActivity(intent);
}
//打开本地bundle
public void local(View view){
Intent intent = new Intent();
intent.setClassName(view.getContext(), "mmc.localbundle.LocalActivity");
startActivity(intent);
}
//更新补丁
public void update(View view){
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
update();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MainActivity.this, "更新完成,请重启", Toast.LENGTH_LONG).show();
}
}.execute();
}
private void update(){
File updateInfo = new File(getExternalCacheDir(), "update.json");
if (!updateInfo.exists()) {
showToast("更新信息不存在,请先 执行 buildTpatch.sh");
return;
}
String jsonStr = new String(FileUtils.readFile(updateInfo));
UpdateInfo info = JSON.parseObject(jsonStr, UpdateInfo.class);
File patchFile = new File(getExternalCacheDir(), "patch-" + info.updateVersion + "@" + info.baseVersion + ".tpatch");
try {
AtlasUpdater.update(info, patchFile);
} catch (Throwable e) {
e.printStackTrace();
showToast("更新失败, " + e.getMessage());
}
}

如有问题,请看下面bug处理,若没有相关的,就要自己去搞搞了,我贴出我自己碰到的

此时运行出现结果为

1、这个时候点击远程bundle会弹出说没有so文件,因为还没打so包呢
2、点击本地bundle,是可以跳转到那个本地bundle页面
3、点击更新补丁,会提示更新信息不存在

到目前为止前期工作就做好了,接下来就是更新了

1.打开AS的Terminal,输入:gradlew clean assembleDebug publish,生成so文件,如果失败就试试干掉java.exe进程,重启as再打,如果成功则会出现这样的文件,把这个文件放到手机内存卡Android/data/com.weidu.atlastest/cache 文件夹里面,点击remotebundle就可以进去了

阿里云atlas组件接入

2.修改版本号,对本地Bundle进行文字修改,对app主项目也可以修改(修改已有的Activity)

阿里云atlas组件接入

3.在Terminal里面输入:gradlew clean assembleDebug -DapVersion=1.0.0 -DversionName=1.0.1生成补丁差异包

阿里云atlas组件接入

 4.将箭头两个文件,放到手机内存卡Android/data/com.weidu.atlastest/cache 文件夹里面,然后点击“更新补丁”,过一会,提示更新成功后,就退出杀死app,再打开就是后面修改的内容了

 

接下来就是bug了,我遇到的

1.Caused by: java.io.IOException: CreateProcess error=2, 系统找不到指定的文件

这个是因为ndk版本原因,我目前是17,修改成16后就解决了

参考链接:https://blog.****.net/lovelixue/article/details/81125653

  1. 解决方案:升级ndk
    1. Build
    2. 解压下载的低版本ndk到这个目录
    3. 清空原来目录

2.运行时 出现了多个APP,无法打开的情况

原因:application里面出现了多个launcher的配置,包括各个module的都需要删除掉

参考链接:https://blog.****.net/lovelixue/article/details/81126434

解决方案:阿里云atlas组件接入

3.找不到AtlasLaunch这个报错

代码里面的包名是需要切换成你自己的

阿里云atlas组件接入

好了,目前就贴这么多吧,最后献上我的githup代码 https://github.com/sdgSnow/TestAtlas

如果遇到了其他问题,可以在下方留言,我知道的话会尽可能回答,满意记得帮我star下,谢谢

相信没有比我这更详细的了

参考文档:阿里云官方文档 https://www.aliyun.com/jiaocheng/14173.html?spm=5176.100033.2.24.7VP1aT

推荐一篇我集成的滴滴virtualapk插件化框架 https://mp.****.net/postedit/81197352