Android EMAS统一接入
官方接入教程:https://help.aliyun.com/knowledge_detail/68655.html
以下是博主的简单接入过程:
所需权限:
<!-- 网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--外部存储读权限,调试工具加载本地补丁需要-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1、新建应用,获取相关的appid和appkey…
2、 控制台下载 aliyun-emas-services.json文件,并将json文件放到应用根目录下(/app/),其内容如下:
{
"config": {
"emas.appKey":"281190",
"emas.appSecret":"8c00670f9b76fb3c8418761c41084e",
"emas.packageName":"com.vdsp.emas",
"hotfix.idSecret":"28119490-1",
"hotfix.rsaSecret":"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEWa7XS6NaGWj/nGHP3eTL1Uexgg7RM+dA40Jiqz5evyEl53gd6q8/gMVqRXOMibd0LRsqznmMrHGKha+pirMoGhI5lrrCJlMA1SlTGpv0kpAWtYoZPM+QBwH5slOaVn7PsZBeam8UNMr2xuWRSqiTiE3uv5hUyeKWUuTUCxwFVbtG5FFVKdXKoW8R+yghMY8AuVWKyaf89vuv4KR8DwBPlh+9OkRFQDqpLfm2ZD1E+xDQ/hIrHlZJVPgQf0V3hmieYmd97u5amv2jTjBeGjUhH9U63IkOUxAtKt4hTRt8fgb3t/mniM2f6ldDuN+dKMZk0bpIg40l/kUpfgdGuUSvAgMBAAECggEAOoncTnezEbfpfB10GdfASfPU2Qt4eEmnwEiEZVXuqUOKatR9iWXzC3huWPnNCN0k2mrU+FMI6hKfybAlqSyi31rgsr4bbqI61kRyGcLMNhnZR2IWaeE+wSqIUiPw3HPjdQPJoikbZmmPWCVTBrdp5o9EQRRXUTFWWjzh33PmeejyvO09SQYLpdfqUx2sOg7rsXjzYEOzxTXRoaDNOZwBhmz7fs5AWdXuRJCwJmWmhl+xwieuRxjoCBux/1ZEpZ95RrykKx6WBxQ6kdd3dkBphKUHWpoMbkz6fLAe1PMnv7SWsdwknfhM1YKseg6bPhh7b1Ou8mGGshN/RAaCmR42qQKBgQDekQVmbMlNyCcTEORebQaar6OGjuj0bqSgwhjHd28RAPTUVtpGiYUh8+b/BV5vTZROZtN8yBWYCop7k7d9VjE0eogdFwgdlYyYkAK0CT0sjOtZA0Iqjj/+l7GZuke4BorOskuBO3Zewsg1RL0kRcJEylw3vcR8D7/c6enf4Rzo6wKBgQCYO1MPM/sjo+DZFcyMAkyWSupC8vLHfVDWapfWtW4kL8bopb5YkuZulyvBDlswSayUmRWfWc22Qu0xfVojZXeV9NWTTvhl1yHTy7uHX5hILucBjyNB/3B9mv8zqoDEktpNb7puWmshR8UOqEZEdCrM4pgS/XtK9GZL2a6tqEgiTQKBgGcKEsrxkFzfTh/IkUGaDhWtPPK91OHdG4CFe71U8OmZXL+1mgZQAn/OkGH+lVLIQuHtPvolE8I5FxXRNIDlV5BpLfzxvcMcimPTQD0jjdvDPe4O0SIsizoN9p4qHKv482hwKP6ijdGfMdtfgIV7ek4otXrk80NQc04OSUVpzd2TAoGBAJahBMOjTJlsW1ySLE5RLBT+VaQfHsjzn5w55rol1bDqsWecMKcV2/qLjFi/as1x6Nk0TGfbhSgWOvrgLOOfT9/KXdYfqruiTi9yhMEjftvVxPAPAPUZ2uX+CwzFZhqbsHQ03IS65QwOJtck/DpZs8v9XjpUJL367ImbADOGsrF1AoGAWAMX0Qlz0n3ic/OnC3bAwHRApTKSjcZKt9WEXO0WpSKrIFeYTgmy86B4QHkAgZuFluhHG2lF4fgpyaxN2jklJWraOaLpGLowd9xe/fTanoajocEw6B/vpsERHgPM9SSADhGWA6KE2iCQSuxwqhA8/uwbTnMHW1DwDk2H/wtSPIY=",
"httpdns.accountId":"111402",
"httpdns.secretKey":"90aa21cfd4714f223d57af140acf0a0e",
"appmonitor.tlog.rsaSecret":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgAwCbvBmy3CKh6YZekd3ZZ5R0mspZ5YkyRqawfSlck/t3LX4tNUfrq5fiM+uFZqewo7j7WI9vHHAxebeobt9snEXdLJjH2sIjDS0oe2hRl+TjQNKb6nhSCRjlrJpdNf9VEu+gMdOSLjketdsxqaRhtEeN9+kPMNQPJt6104JaGwIDAQAB"
},
"services": {
"httpdns_service": {
"status": 1,
"version":"1.2.5"
},
"cps_service": {
"status": 1,
"version":"3.1.6"
},
"hotfix_service": {
"status": 1,
"version":"3.2.11"
},
"man_service": {
"status": 1,
"version":"1.2.4"
},
"feedback_service": {
"status": 1,
"version":"3.3.1"
},
"crashreporter_service": {
"status": 0,
"version":"1.1.3.2-open"
},
"tlog_service": {
"status": 0,
"version":"1.1.2.2-open"
},
"apm_service": {
"status": 0,
"version":"1.0.7.7-open"
}
},
"use_maven":true,
"proguard_keeplist":"\n#httpdns\n-keep class com.taobao.** {*;}\n-keep class com.alibaba.** {*;}\n-keep class com.ta.**{*;}\n-keep class com.ut.**{*;}\n-dontwarn com.taobao.**\n-dontwarn com.alibaba.**\n-dontwarn com.ta.**\n-dontwarn com.ut.**\n\n#cps\n-keep class com.taobao.** {*;}\n-keep class com.alibaba.** {*;}\n-keep class com.ta.**{*;}\n-keep class com.ut.**{*;}\n-dontwarn com.taobao.**\n-dontwarn com.alibaba.**\n-dontwarn com.ta.**\n-dontwarn com.ut.**\n-keepclasseswithmembernames class ** {\nnative <methods>;\n}\n-keepattributes Signature\n-keep class sun.misc.Unsafe { *; }\n-keep class com.alipay.** {*;}\n-dontwarn com.alipay.**\n-keep class anet.**{*;}\n-keep class org.android.spdy.**{*;}\n-keep class org.android.agoo.**{*;}\n-dontwarn anet.**\n-dontwarn org.android.spdy.**\n-dontwarn org.android.agoo.**\n\n#hotfix\n#基线包使用,生成mapping.txt\n-printmapping mapping.txt\n#生成的mapping.txt在app/buidl/outputs/mapping/release路径下,移动到/app路径下\n#修复后的项目使用,保证混淆结果一致\n#-applymapping mapping.txt\n#hotfix\n-keep class com.taobao.sophix.**{*;}\n-keep class com.ta.utdid2.device.**{*;}\n#防止inline\n-dontoptimize\n\n#man\n-keep class com.taobao.** {*;}\n-keep class com.alibaba.** {*;}\n-keep class com.ta.**{*;}\n-keep class com.ut.**{*;}\n-dontwarn com.taobao.**\n-dontwarn com.alibaba.**\n-dontwarn com.ta.**\n-dontwarn com.ut.**\n\n#feedback\n-keep class com.taobao.** {*;}\n-keep class com.alibaba.** {*;}\n-keep class com.ta.**{*;}\n-keep class com.ut.**{*;}\n-dontwarn com.taobao.**\n-dontwarn com.alibaba.**\n-dontwarn com.ta.**\n-dontwarn com.ut.**\n"
}
3、项目级目录下build.gradle(/build.gradle)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
//添加下面这个
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
//添加下面这个
classpath 'com.aliyun.ams:emas-services:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
//添加下面这个
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
}
}
4、应用级build.gradle集成(//build.gradle)
apply plugin: 'com.android.application'
//添加下面这个
apply plugin: 'com.aliyun.ams.emas-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.vdsp.emas"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//添加下面这个
implementation 'com.aliyun.ams:alicloud-android-hotfix:3.2.10'
}
5、新建MyRealApplication(类名自己定义),这里是请求服务器的补丁(后期正式应用这里可以做个开关)
public class MyRealApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// queryAndLoadNewPatch不可放在attachBaseContext 中,否则无网络权限,建议放在后面任意时刻,如onCreate中
SophixManager.getInstance().queryAndLoadNewPatch();
}
}
6、新建SophixStubApplication
/**
* Sophix入口类,专门用于初始化Sophix,不应包含任何业务逻辑。
* 此类必须继承自SophixApplication,onCreate方法不需要实现。
* 此类不应与项目中的其他类有任何互相调用的逻辑,必须完全做到隔离。
* AndroidManifest中设置application为此类,而SophixEntry中设为原先Application类。
* 注意原先Application里不需要再重复初始化Sophix,并且需要避免混淆原先Application类。
* 如有其它自定义改造,请咨询官方后妥善处理。
*/
public class SophixStubApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
// 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
@Keep
@SophixEntry(MyRealApplication.class)
static class RealApplicationStub {}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此处调用。
// MultiDex.install(this);
initSophix();
}
private void initSophix() {
String appVersion = new String();
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
} catch (Exception e) {
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
//这个的补丁版本我直接用的是应用的版本
//注意,要分清楚补丁版本和应用版本
.setAppVersion(appVersion)
//第一个参数为:AppKey,第二个参数为:AppSecret,第二个参数为:RSA
.setSecretMetaData("28119490","8c00670f9b76fb3c8418761c4175084e", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEWa7XS6NaGWj/nGHP3eTL1Uexgg7RM+dA40Jiqz5evyEl53gd6q8/gMVqRXOMibd0LRsqznmMrHGKha+pirMoGhI5lrrCJlMA1SlTGpv0kpA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MdtfgIV7ek4otXrk80NQc04OSUVpzd2TAoGBAJahBMOjTJlsW1ySLE5RLBT+VaQfHsjzn5w55rol1bDqsWecMKcV2/qLjFi/as1x6Nk0TGfbhSgWOvrgLOOfT9/KXdYfqruiTi9yhMEjftvVxPAPAPUZ2uX+CwzFZhqbsHQ03IS65QwOJtck/DpZs8v9XjpUJL367ImbADOGsrF1AoGAWAMX0Qlz0n3ic/OnC3bAwHRApTKSjcZKt9WEXO0WpSKrIFeYTgmy86B4QHkAgZuFluhHG2lF4fgpyaxN2jklJWraOaLpGLowd9xe/fTanoajocEw6B/vpsERHgPM9SSADhGWA6KE2iCQSuxwqhA8/uwbTnMHW1DwDk2H/wtSPIY=")
.setEnableDebug(true)
.setEnableFullLog()
.setPatchLoadStatusStub(new PatchLoadStatusListener() {
@Override
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Log.i(TAG, "sophix load patch success!");
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
// 如果需要在后台重启,建议此处用SharePreference保存状态。
Log.i(TAG, "sophix preload patch success. restart app to make effect.");
}
}
}).initialize();
}
}
查看AppKey和AppSecret,RSA
在Manifest.xml中添加
<application
...
android:name=".SophixStubApplication"
...
</application>
7、第二生成补丁
patch补丁包生成需要使用到打补丁工具SophixPatchTool, 如还未下载打包工具,请前往下载Android打包工具。
Mac版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_macos.zip
Windows版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_windows.zip
Linux版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_linux.zip
调试工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/hotfix_debug_tool-release.apk
8、上传补丁,测试,打完收工!!!!
已测试成功Android版本:4.4,6.0,7.1.1,8.1.0,P,Q(国内的测试版)