一 . 準(zhǔn)備工作
1. 在工程根目錄中的build.gradle添加插件依賴
classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.8"
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// TinkerPatch 插件
classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.8"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
2. 在app/build.gradle添加TinkerPatch SDK 庫(kù):
在defaultConfig里添加
multiDexEnabled true
multiDexKeepProguard file("tinkerMultidexKeep.pro")
// 多dex 打包的類庫(kù)
defaultConfig {
applicationId "com.mingtao.mtedu"
minSdkVersion 19
targetSdkVersion 25
multiDexEnabled true
versionCode 28
versionName "3.1.3"
multiDexEnabled true
multiDexKeepProguard file("tinkerMultidexKeep.pro") //keep specific classes using proguard syntax
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
// ndk {
// abiFilters "arm64-v8a"
// abiFilters "armeabi-v7a"
// }
添加 TinkerPatch SDK 庫(kù)的 denpendencies 依賴
// 多dex 打包的類庫(kù)
api 'com.android.support:multidex:1.0.3'
compileOnly("com.tinkerpatch.tinker:tinker-android-anno:1.9.8")
api("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.8")
3. 在app目錄下新建tinkerpatch.gradle
// 多dex 打包的類庫(kù)
apply plugin: 'tinkerpatch-support'
/**
* TODO: 請(qǐng)按自己的需求修改為適應(yīng)自己工程的參數(shù)
*/
def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-3.1.3-0627-14-14-33" //正常打包后在app/build/bakApk目錄下的名字替換此處
def variantName = "release"
/**
* 對(duì)于插件各參數(shù)的詳細(xì)解析請(qǐng)參考
* http://tinkerpatch.com/Docs/SDK
*/
tinkerpatchSupport {
/** 可以在debug的時(shí)候關(guān)閉 tinkerPatch **/
/** 當(dāng)disable tinker的時(shí)候需要添加multiDexKeepProguard和proguardFiles,
這些配置文件本身由tinkerPatch的插件自動(dòng)添加消约,當(dāng)你disable后需要手動(dòng)添加
你可以copy本示例中的proguardRules.pro和tinkerMultidexKeep.pro,
需要你手動(dòng)修改'tinker.sample.android.app'本示例的包名為你自己的包名, com.xxx前綴的包名不用修改
**/
tinkerEnable = true
reflectApplication = true
/**
* 是否開啟加固模式域帐,只能在APK將要進(jìn)行加固時(shí)使用,否則會(huì)patch失敗翻擒。
* 如果只在某個(gè)渠道使用了加固,可使用多flavors配置
**/
protectedApp = true
/**
* 實(shí)驗(yàn)功能
* 補(bǔ)丁是否支持新增 Activity (新增Activity的exported屬性必須為false)
**/
supportComponent = true
autoBackupApkPath = "${bakPath}"
appKey = "替換自己的appKey"
/** 注意: 若發(fā)布新的全量包, appVersion一定要更新 **/
appVersion = "3.1.3"
def pathPrefix = "${bakPath}/${baseInfo}/${variantName}/"
def name = "${project.name}-${variantName}"
baseApkFile = "${pathPrefix}/${name}.apk"
baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"
baseResourceRFile = "${pathPrefix}/${name}-R.txt"
/**
* 若有編譯多flavors需求, 可以參照: https://github.com/TinkerPatch/tinkerpatch-flavors-sample
* 注意: 除非你不同的flavor代碼是不一樣的,不然建議采用zip comment或者文件方式生成渠道信息(相關(guān)工具:walle 或者 packer-ng)
**/
}
/**
* 用于用戶在代碼中判斷tinkerPatch是否被使能
*/
android {
defaultConfig {
buildConfigField "boolean", "TINKER_ENABLE", "${tinkerpatchSupport.tinkerEnable}"
}
}
/**
* 一般來說,我們無需對(duì)下面的參數(shù)做任何的修改
* 對(duì)于各參數(shù)的詳細(xì)介紹請(qǐng)參考:
* https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
*/
tinkerPatch {
ignoreWarning = false
useSign = true
dex {
dexMode = "jar"
pattern = ["classes*.dex"]
loader = []
}
lib {
pattern = ["lib/*/*.so"]
}
res {
pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
ignoreChange = []
largeModSize = 100
}
packageConfig {
}
sevenZip {
zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
// path = "/usr/local/bin/7za"
}
buildConfig {
keepDexApply = false
}
}
并在app/build.gradle 最后面添加代碼:
apply from: 'tinkerpatch.gradle'
4. 修改項(xiàng)目的application
-public class YourApplication extends Application {
+public class SampleApplicationLike extends DefaultApplicationLike {
@SuppressWarnings("unused")
@DefaultLifeCycle(application = "com.mingtao.mtedu.MTApplication",//把AndroidManifest.xml中application的name修改為這個(gè)
flags = ShareConstants.TINKER_ENABLE_ALL,
loadVerifyFlag = false)
public class SampleApplicationLike extends DefaultApplicationLike {
public SampleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
}
/**
* install multiDex before install tinker
* so we don't need to put the tinker lib classes in the main dex
*
* @param base
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
context = getApplication();
//you must install multiDex whatever tinker is installed!
MultiDex.install(base);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
getApplication().registerActivityLifecycleCallbacks(callback);
}
@Override
public void onCreate() {
super.onCreate();
initTinker();
......
//添加自己的application中初始化的代碼
}
private void initTinker() {
if (BuildConfig.TINKER_ENABLE) {
//開始檢查是否有補(bǔ)丁,這里配置的是每隔訪問3小時(shí)服務(wù)器是否有更新。
TinkerPatch.init(this)
.reflectPatchLibrary()
.setPatchRollbackOnScreenOff(true)
.setPatchRestartOnSrceenOff(true)
.setFetchPatchIntervalByHours(3);
//每隔3個(gè)小時(shí)去訪問后臺(tái)時(shí)候有更新,通過handler實(shí)現(xiàn)輪訓(xùn)的效果
TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
}
}
}
在清單文件AndroidManifest.xml中把a(bǔ)pplication的name改為SampleApplicationLike中定義的application全名
<application
android:name="***.MTApplication"http://SampleApplicationLike中定義的名字
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
5. 在MainActivity或其他Activity中添加補(bǔ)丁下載代碼
//加載補(bǔ)丁包
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip");
TinkerPatch.with().fetchPatchUpdate(true);
二 . 測(cè)試
1.打包
會(huì)在app/build/bakApk目錄下生成一個(gè)文件夾
把a(bǔ)pp-release.apk安裝到手機(jī)上
2.生成補(bǔ)丁
修改代碼完成后在Android Studio右上角打開Gradle 雙擊tinkerPatchRelease
會(huì)在生成 app\build\outputs\apk\tinkerPatch
把生成的patch_signed_7zip.apk上傳到Tinker后臺(tái)