Bugly是鵝廠的一個(gè)集成異常上報(bào),運(yùn)營統(tǒng)計(jì)笙纤,在線升級(jí)耗溜,熱更新的框架;
與傳統(tǒng)的熱更新相比省容,這個(gè)不需要搭建后臺(tái)抖拴,不需要知道補(bǔ)丁是如何生成的,我只簡述使用Bugly熱更新成功的一種例子和我遇到的問題
集成
①工程根目錄下“build.gradle”文件中添加:
classpath "com.tencent.bugly:tinker-support:1.1.2"
②在app module的“build.gradle”文件中添加(示例配置):
android {
defaultConfig {
ndk {
//設(shè)置支持的SO庫架構(gòu)
abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
}
dependencies {
compile "com.android.support:multidex:1.0.1" // 多dex配置
//注釋掉原有bugly的倉庫
//compile 'com.tencent.bugly:crashreport:latest.release'//其中l(wèi)atest.release指代最新版本號(hào),也可以指定明確的版本號(hào),例如1.3.4
compile 'com.tencent.bugly:crashreport_upgrade:1.3.5'
// 指定tinker依賴版本(注:應(yīng)用升級(jí)1.3.5版本起残吩,不再內(nèi)置tinker)
compile 'com.tencent.tinker:tinker-android-lib:1.9.6'
compile 'com.tencent.bugly:nativecrashreport:latest.release' //其中l(wèi)atest.release指代最新版本號(hào)驻龟,也可以指定明確的版本號(hào),例如2.2.0
}
③在APP目錄下添加tinker-support.gradle文件
內(nèi)容如下:
apply plugin: 'com.tencent.bugly.tinker-support'
def bakPath = file("${buildDir}/bakApk/")
/**
* 此處填寫每次構(gòu)建生成的基準(zhǔn)包目錄
*/
def baseApkDir = "app-0719-16-37-01"
/**
* 對(duì)于插件各參數(shù)的詳細(xì)解析請參考
*/
tinkerSupport {
// 開啟tinker-support插件净当,默認(rèn)值true
enable = true
// 指定歸檔目錄,默認(rèn)值當(dāng)前module的子目錄tinker
autoBackupApkDir = "${bakPath}"
// 是否啟用覆蓋tinkerPatch配置功能,默認(rèn)值false
// 開啟后tinkerPatch配置不生效往湿,即無需添加tinkerPatch
overrideTinkerPatchConfiguration = true
// 編譯補(bǔ)丁包時(shí),必需指定基線版本的apk惋戏,默認(rèn)值為空
// 如果為空领追,則表示不是進(jìn)行補(bǔ)丁包的編譯
// @{link tinkerPatch.oldApk }
baseApk = "${bakPath}/${baseApkDir}/app-release.apk"
// 對(duì)應(yīng)tinker插件applyMapping
baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"
// 對(duì)應(yīng)tinker插件applyResourceMapping
baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
// 構(gòu)建基準(zhǔn)包和補(bǔ)丁包都要指定不同的tinkerId,并且必須保證唯一性
tinkerId = "patch-1.0.1"
// 構(gòu)建多渠道補(bǔ)丁時(shí)使用
// buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
// 是否啟用加固模式响逢,默認(rèn)為false.(tinker-spport 1.0.7起支持)
// isProtectedApp = true
// 是否開啟反射Application模式
enableProxyApplication = true
// 是否支持新增非export的Activity(注意:設(shè)置為true才能修改AndroidManifest文件)
supportHotplugComponent = true
}
④在app module的“build.gradle”文件中添加:
// 依賴插件腳本
apply from: 'tinker-support.gradle'
⑤AndroidManifest.xml配置
加入權(quán)限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
加入Activity:
<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity"
android:configChanges="keyboardHidden|orientation|screenSize|locale"
android:theme="@android:style/Theme.Translucent" />
加入配置FileProvider:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
假如你已經(jīng)使用了FileProvider,就加入此配置
<provider
android:name=".util.BuglyFileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="name,authorities,exported,grantUriPermissions">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="name,resource"/>
</provider>
⑥在res文件夾下新增xml文件夾绒窑,新增provider_paths.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
<external-path name="beta_external_path" path="Download/"/>
<!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
<external-path name="beta_external_files_path" path="Android/data/"/>
</paths>
⑦為了避免混淆SDK龄句,在Proguard混淆文件中增加以下配置:
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}
# tinker混淆規(guī)則
-dontwarn com.tencent.tinker.**
-keep class com.tencent.tinker.** { *; }
如果你使用了support-v4包回论,你還需要配置以下混淆規(guī)則:
-keep class android.support.**{*;}
⑧Application配置
package com.wocus.wine.application;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
/**
* Created by Administrator on 2018/7/5.
*/
public class BaseApplication extends Application{
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(base);
Beta.installTinker();
}
@Override
public void onCreate() {
super.onCreate();
Bugly.init(this, "ce34683adf", true);
}
}
到此已經(jīng)集成完畢
使用
①打包基準(zhǔn)包
注意加入keystore配置
注意一定要開啟混淆,不然打包沒有mapper文件
buildTypes {
release {
minifyEnabled true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
打包成功之后,會(huì)在此目錄有3個(gè)文件
app-release.apk則為你的基準(zhǔn)包分歇,上線發(fā)布的包傀蓉,拿這個(gè)apk在手機(jī)上安裝上報(bào)聯(lián)網(wǎng)
打包補(bǔ)丁包
上線之后如果發(fā)現(xiàn)版本有Bug,可以打補(bǔ)丁包修復(fù)职抡,Bug修復(fù)好了葬燎,去更改tinker-support.gradle文件
/**
* 此處填寫每次構(gòu)建生成的基準(zhǔn)包目錄
*/
def baseApkDir = "app-0719-16-37-01"
// 構(gòu)建基準(zhǔn)包和補(bǔ)丁包都要指定不同的tinkerId,并且必須保證唯一性
tinkerId = "patch-1.0.1"
tinkerId 一定不能和打包基準(zhǔn)包的Id一樣
我是這樣定義的缚甩,基準(zhǔn)報(bào)的tinkerId 為:base-版本號(hào)谱净,補(bǔ)丁包為:patch-版本號(hào),版本號(hào)都一致對(duì)應(yīng)基準(zhǔn)包的版本
baseApkDir 則為基準(zhǔn)包發(fā)布的那個(gè)目錄擅威,上圖有自己可以去看看
然后編譯補(bǔ)丁包
編譯成功之后會(huì)在此目錄有3個(gè)文件壕探,這個(gè)文件就是你需要上傳的補(bǔ)丁包了
然后拿這個(gè)去Bugly平臺(tái)上傳,就OK了
遇到的問題
1.打包出來基準(zhǔn)包的是未簽名apk
A:需要在build.gradle配置簽名文件,舉個(gè)栗子:
signingConfigs {
release {
keyAlias 'wine'
keyPassword '123456'
storeFile file('C:/Project/Android Studio Project/wine/wine.jks')
storePassword '123456'
}
}
2.打包出來的基準(zhǔn)包沒有mapper文件
A:需要在build.gradle開啟混肴
buildTypes {
release {
minifyEnabled true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
3.開啟混肴之后郊丛,打包基準(zhǔn)報(bào)報(bào)錯(cuò)
A:假如你之前項(xiàng)目都沒有配置混肴文件李请,對(duì)此來說需要花費(fèi)不少時(shí)間瞧筛,你把下面文件拷貝去混肴文件就OK了
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
#webView js調(diào)用不混淆,否則取不到數(shù)據(jù)
-keepclassmembers class com.zyp.ui.WebActivity$* {
<methods>;
}
#shrink导盅,測試后發(fā)現(xiàn)會(huì)將一些無效代碼給移除较幌,即沒有被顯示調(diào)用的代碼,該選項(xiàng) 表示 不啟用 shrink白翻。
#-dontshrink
#指定重新打包,所有包重命名,這個(gè)選項(xiàng)會(huì)進(jìn)一步模糊包名乍炉,將包里的類混淆成n個(gè)再重新打包到一個(gè)個(gè)的package中
#-flattenpackagehierarchy
#優(yōu)化時(shí)允許訪問并修改有修飾符的類和類的成員
-allowaccessmodification
#不跳過(混淆) jars中的 非public classes 默認(rèn)選項(xiàng)
-dontskipnonpubliclibraryclassmembers
#忽略警告
-ignorewarnings
#指定代碼的壓縮級(jí)別
-optimizationpasses 5
#不使用大小寫混合類名
-dontusemixedcaseclassnames
#不去忽略非公共的庫類
-dontskipnonpubliclibraryclasses
#不啟用優(yōu)化 不優(yōu)化輸入的類文件
-dontoptimize
#不預(yù)校驗(yàn)
-dontpreverify
#混淆時(shí)是否記錄日志
-verbose
#混淆時(shí)所采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#保護(hù)注解
-renamesourcefileattribute SourceFile
#保持源文件和行號(hào)的信息,用于混淆后定位錯(cuò)誤位置
-keepattributes SourceFile,LineNumberTable
#保持含有Annotation字符串的 attributes
-keepattributes *Annotation*
#過濾泛型
-keepattributes Signature
-keepattributes Exceptions,InnerClasses
-dontwarn org.apache.**
-dontwarn android.support.**
#基礎(chǔ)配置
# 保持哪些類不被混淆
# 系統(tǒng)組件
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
#如果有引用v4包可以添加下面這行
-keep public class * extends android.support.v4.app.Fragment
#自定義View
-keep public class * extends android.view.View
# V4,V7
-keep class android.support.constraint.**{ *; }
-keep class android.support.v4.**{ *; }
-keep class android.support.v7.**{ *; }
-keep class android.webkit.**{*;}
-keep interface android.support.v4.app.** { *; }
#保持 本化方法及其類聲明
-keepclasseswithmembers class * {
native <methods>;
}
#保持view的子類成員: getter setter
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
#保持Activity的子類成員:參數(shù)為一個(gè)View類型的方法 如setContentView(View v)
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
#保持枚舉類的成員:values方法和valueOf (每個(gè)enum 類都默認(rèn)有這兩個(gè)方法)
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#保持Parcelable的實(shí)現(xiàn)類和它的成員:類型為android.os.Parcelable$Creator 名字任意的 屬性
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#保持 任意包名.R類的類成員屬性。 即保護(hù)R文件中的屬性名不變
-keepclassmembers class **.R$* {
public static <fields>;
}
# 記錄生成的日志數(shù)據(jù)滤馍,在 proguard 目錄下
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
-dontwarn java.util.**
-keep class java.util.** {*; }
-dontwarn org.apache.http.**
-keep class org.apache.http.** {*; }
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
-keep public class * implements java.io.Serializable {*;}
-keepclassmembers class **.R$* {
public static <fields>;
}
# ======================= 以上是混淆的基礎(chǔ)配置================================
# ======================= 以下是混淆的第三方包的配置================================
# ======================= Gson 混淆================================
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.idea.fifaalarmclock.entity.***
-keep class com.google.gson.stream.** { *; }
# ======================= Umeng 混淆================================
-keepclassmembers class * {
public <init> (org.json.JSONObject);
}
#這是由于SDK中的部分代碼使用反射來調(diào)用構(gòu)造函數(shù)岛琼, 如果被混淆掉, 在運(yùn)行時(shí)會(huì)提示"NoSuchMethod"錯(cuò)誤纪蜒。
#另外衷恭,由于SDK需要引用導(dǎo)入工程的資源文件,通過了反射機(jī)制得到資源引用文件R.java纯续,但是在開發(fā)者通過proguard等混淆/優(yōu)化工具處理apk時(shí)随珠,proguard可能會(huì)將R.java刪除,如果遇到這個(gè)問題猬错,請?jiān)趐roguard配置文件中添加keep命令如:
-keep public class com.zyp.R$*{
public static final int *;
}
-keep class com.umeng.**
-keep public class com.idea.fifaalarmclock.app.R$*{
public static final int *;
}
-keep public class com.umeng.fb.ui.ThreadView {
}
-dontwarn com.umeng.**
-dontwarn org.apache.commons.**
-keep public class * extends com.umeng.**
-keep class com.umeng.** {*; }
# ======================= pinying4j 混淆================================
-dontwarn com.hp.hpl.sparta.**
-keep class com.hp.hpl.sparta.**{*;}
-dontwarn net.sourceforge.pinyin4j.**
-keep class net.sourceforge.pinyin4j.**{*;}
-dontwarn demo.**
-keep class demo.**{*;}
# ======================= volley 混淆================================
-dontwarn com.android.volley.**
-keep class com.android.volley.**{*;}
# ======================= fastjson 混淆================================
-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *; }
# ======================= alipay 混淆================================
-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
-keep class com.alipay.mobilesecuritysdk.*
-keep class com.ut.*
# ======================= weixin 混淆================================
-keep class com.tencent.mm.** { *; }
# ======================= okio ================================
-dontwarn okio.**
-keep class okio.**{*;}
-keep interface okio.**{*;}
# ======================= activation.jar ================================
-dontwarn javax.activation.**
-keep class javax.activation.**{*;}
-dontwarn com.sun.activation.registries.**
-keep class com.sun.activation.registries.**{*;}
# ======================= additionnal.jar ================================
-dontwarn myjava.awt.datatransfer.**
-keep class myjava.awt.datatransfer.**{*;}
-dontwarn org.apache.harmony.**
-keep class org.apache.harmony.**{*;}
# ======================= RxAndroid,RxJava ================================
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
#======================= httpmime =======================
-keep class org.apache.http.entity.mime.** {*;}
#======================= zxing =======================
-keep class com.google.zxing.** { *; }
#======================= simplelatlng =======================
-keep class com.javadocmd.simplelatlng.** {*;}
# =======================universal-image-loader ================================
-keep class com.nostra13.universalimageloader.** {*;}
# ======================= 百度地圖 ================================
-keep class com.baidu.** {*;}
-keep class vi.com.** {*;}
-dontwarn com.baidu.**
# ======================= 高德地圖 ================================
# 3D 地圖
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.amap.mapcore.*{*;}
-keep class com.amap.api.trace.**{*;}
# 定位
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}
#搜索
-keep class com.amap.api.services.**{*;}
#2D地圖
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
#導(dǎo)航
-keep class com.amap.api.navi.**{*;}
-keep class com.autonavi.**{*;}
#訊飛
-keep class com.iflytek.**{*;}
# ======================= app javabean 混淆================================
-keep class com.zyp.bean.**{*;}
##---------------Begin:retrofit+rxjava----------------------
-dontwarn javax.annotation.**
-dontwarn javax.inject.**
# OkHttp3
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *;}
-keep interface okhttp3.**{*;}
-dontwarn okhttp3.logging.**
-keep class okhttp3.internal.**{*;}
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
-dontwarn okio.**
# Retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
# RxJava RxAndroid
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
-dontnote android.webkit.**
-dontnote java.util.**
##---------------Inmobi----------------------
-keepattributes SourceFile,LineNumberTable -keep class com.inmobi.** { *; }
-keep public class com.google.android.gms.**
-dontwarn com.google.android.gms.**
-dontwarn com.squareup.picasso.**
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient{
public *;
}
# skip the Picasso library classes
-keep class com.squareup.picasso.** {*;}
-dontwarn com.squareup.picasso.**
-dontwarn com.squareup.okhttp.**
# skip Moat classes
-keep class com.moat.** {*;}
-dontwarn com.moat.**
混肴規(guī)則有個(gè)不錯(cuò)的博客:https://blog.csdn.net/guolipeng_network/article/details/74551968
如果集成中有什么問題窗看,歡迎聯(lián)系我QQ752422962