微信Tinker-熱更新

使用指南:
  • 在項(xiàng)目根目錄下的build.gradle文件中:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // TinkerPatch 插件
        classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.1.7"
    }
}
  • 在app目錄下的build.gradle文件中:
dependencies {
    // 若使用annotation需要單獨(dú)引用,對(duì)于tinker的其他庫(kù)都無(wú)需再引用
    provided("com.tinkerpatch.tinker:tinker-android-anno:1.7.11")
    compile("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.1.7")
}
apply from: 'tinkerpatch.gradle'
  • 在app目錄下創(chuàng)建tinkerpatch.gradle文件并添加如下配置:
apply plugin: 'tinkerpatch-support'
/**
 * TODO: 請(qǐng)按自己的需求修改為適應(yīng)自己工程的參數(shù)
 */
def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-1.0.0-0629-15-41-34"
def variantName = "release"
//def variantName = "debug"
/**
 * 對(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
    autoBackupApkPath = "${bakPath}"
    appKey = ""http:// TODO 申請(qǐng)的appKey
    /** 注意: 若發(fā)布新的全量包, appVersion一定要更新 **/
    appVersion = "1.0.0"
    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}"
    }
}
/**
 * 一般來(lái)說(shuō),我們無(wú)需對(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
    }
}
  • 創(chuàng)建FetchPatchHandler用于檢測(cè)是否更新(剛打開(kāi)時(shí)會(huì)檢測(cè)一次)
public class FetchPatchHandler extends Handler {
    public static final long HOUR_INTERVAL = 3600 * 1000;
    private long checkInterval;
    /**
     * 通過(guò)handler, 達(dá)到按照時(shí)間間隔輪訓(xùn)的效果
     * @param hour
     */
    public void fetchPatchWithInterval(int hour) {
        //設(shè)置TinkerPatch的時(shí)間間隔
        TinkerPatch.with().setFetchPatchIntervalByHours(hour);
        checkInterval = hour * HOUR_INTERVAL;
        //立刻嘗試去訪問(wèn),檢查是否有更新
        sendEmptyMessage(0);
    }
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        //這里使用false即可
        TinkerPatch.with().fetchPatchUpdate(false);
        //每隔一段時(shí)間都去訪問(wèn)后臺(tái), 增加10分鐘的buffer時(shí)間
        sendEmptyMessageDelayed(0, checkInterval + 10 * 60 * 1000);
    }
}
  • 初始化SDK(reflectApplication = true 的情況)
public class SampleApplication extends Application {
    private ApplicationLike tinkerApplicationLike;
    @Override
    public void onCreate() {
        super.onCreate();
        if (BuildConfig.TINKER_ENABLE) {
            // 我們可以從這里獲得Tinker加載過(guò)程的信息
            tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();
            // 初始化TinkerPatch SDK, 更多配置可參照API章節(jié)中的,初始化SDK
            TinkerPatch.init(tinkerApplicationLike)
                    .reflectPatchLibrary()
                    .setPatchRollbackOnScreenOff(true)
                    .setPatchRestartOnSrceenOff(true);
            // 每隔3個(gè)小時(shí)去訪問(wèn)后臺(tái)時(shí)候有更新,通過(guò)handler實(shí)現(xiàn)輪訓(xùn)的效果
            new FetchPatchHandler().fetchPatchWithInterval(3);
        }
    }
}
  • 在app目錄下創(chuàng)建keystore文件夾,添加release.keystore及debug.keystore:
  • 在app目錄下的build.gradle文件中添加簽名配置:
// 簽名配置
signingConfigs {
    release {
        try {
            storeFile file("./keystore/release.keystore")
            storePassword "testres"
            keyAlias "testres"
            keyPassword "testres"
        } catch (ex) {
            throw new InvalidUserDataException(ex.toString())
        }
    }
    debug {
        storeFile file("./keystore/debug.keystore")
    }
}
buildTypes {
    release {
        minifyEnabled false
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
}

官方文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容