自建服務端實現Tinker熱修復

前言:

      最近工做不是很忙尋思着學習學習一些新技術or熱門技術,熱修復,聽着好高大上啊,安卓高級程序員必會技能(高逼格技能~~)。熱修復這一兩年確實很火,技術文章滿天飛,BAT等大公司都有一套本身的熱修復框架,TinkerDexposedAndFixHotFixNuwa等等,熱修復有兩大流派:java

  • Native,表明有阿里的Dexposed、AndFix與騰訊的內部方案KKFix;mysql

  • Java,表明有Qzone的超級補丁、大衆點評的nuwa、百度金融的rocooFix, 餓了麼的amigo以及美團的robust。android

Native流派與Java流派都有着本身的優缺點,它們具體差別能夠參考這裏~~,這些框架GitHub上都有實現原理和踩坑方法~~你們能夠根據需求選擇最適合本身的熱修復框架。好了咱們直奔主題!!
git

1、爲何使用Tinker?

     出至騰訊是微信官方的Android熱補丁解決方案,咱們每使用一個開源框架須要考慮的是性能、兼容性成功率、後期維護等。首先性能和兼容性你們看這裏,做爲一個擁有9億用戶的超級app,Tinker可以做爲微信熱修復支撐,極致的性能和兼容性是必須的,也是Tinker開發的初衷,通過了一系列的版本迭代到最新的1.9.2已經愈加穩定成熟,而後再是維護,從16年9月第一次發佈版本到目前爲止更新了17個版本,2178次commit。。。可見微信團隊一直在致力維護這個項目,而AndFix已經兩年沒更新了,issue也是一大堆問題沒人解決。。看來阿里爸爸是放棄這個框架了~~~,dexposed也是一個樣。。。貼個圖各個熱修復框架優點對比:程序員

2、開始採坑之旅

一、安卓Tinker集成github

     新建項目配置project的gradle:ps由於Tinker支持gradle配置,配置屬性比較多最好直接拷貝過來再把你原有的gradle屬性配置上去(要看懂這些配置屬性是什麼意思,須要有必定的gradle知識~~~)web

buildscript {
    repositories {
        mavenLocal()
        google()
        jcenter()
    }

    dependencies {
        if (project.hasProperty('GRADLE_3') && GRADLE_3.equalsIgnoreCase('TRUE')) {
            classpath 'com.android.tools.build:gradle:3.0.0'
        } else {
            classpath 'com.android.tools.build:gradle:2.3.3'
        }
        classpath "com.tencent.tinker:tinker-patch-gradle-plugin:${TINKER_VERSION}"
    }
}

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
    }
}

def is_gradle_3() {
    return hasProperty('GRADLE_3') && GRADLE_3.equalsIgnoreCase('TRUE')
}複製代碼

這個會報錯:sql

別急,打開gradle.properties在最後面加入:數據庫

TINKER_VERSION=1.9.2
GRADLE_3=true複製代碼

而後配置app的gradle(我把整個放放出來):windows

apply plugin: 'com.android.application'

dependencies {
    if (is_gradle_3()) {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        testImplementation 'junit:junit:4.12'
        implementation "com.android.support:appcompat-v7:23.1.1"
        implementation("com.tencent.tinker:tinker-android-lib:${TINKER_VERSION}") { changing = true }
        annotationProcessor("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true }
        compileOnly("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true }

        implementation "com.android.support:multidex:1.0.1"
        //use to test multiDex
//    implementation group: 'com.google.guava', name: 'guava', version: '19.0'
//    implementation "org.scala-lang:scala-library:2.11.7"

        //use for local maven test
//        implementation("com.tencent.tinker:tinker-android-loader:${TINKER_VERSION}") { changing = true }
//        implementation("com.tencent.tinker:aosp-dexutils:${TINKER_VERSION}") { changing = true }
//        implementation("com.tencent.tinker:bsdiff-util:${TINKER_VERSION}") { changing = true }
//        implementation("com.tencent.tinker:tinker-ziputils:${TINKER_VERSION}") { changing = true }
//        implementation("com.tencent.tinker:tinker-commons:${TINKER_VERSION}") { changing = true }
    } else {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile "com.android.support:appcompat-v7:23.1.1"
        compile("com.tencent.tinker:tinker-android-lib:${TINKER_VERSION}") { changing = true }
        provided("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true }

        compile "com.android.support:multidex:1.0.1"

        //use to test multiDex
//    compile group: 'com.google.guava', name: 'guava', version: '19.0'
//    compile "org.scala-lang:scala-library:2.11.7"

        //use for local maven test
//        compile("com.tencent.tinker:tinker-android-loader:${TINKER_VERSION}") { changing = true }
//        compile("com.tencent.tinker:aosp-dexutils:${TINKER_VERSION}") { changing = true }
//        compile("com.tencent.tinker:bsdiff-util:${TINKER_VERSION}") { changing = true }
//        compile("com.tencent.tinker:tinker-ziputils:${TINKER_VERSION}") { changing = true }
//        compile("com.tencent.tinker:tinker-commons:${TINKER_VERSION}") { changing = true }
    }

    compile 'com.dx168.patchsdk:patchsdk:1.2.7'
}


def javaVersion = JavaVersion.VERSION_1_7

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    compileOptions {
        sourceCompatibility javaVersion
        targetCompatibility javaVersion
    }
    //recommend
    dexOptions {
        jumboMode = true
    }

//    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")
//        }
//    }

    defaultConfig {
        applicationId "com.oking.mytinker"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0.0"
        /**
         * you can use multiDex and install it in your ApplicationLifeCycle implement
         */
        multiDexEnabled true
        /**
         * buildConfig can change during patch!
         * we can use the newly value when patch
         */
        buildConfigField "String", "MESSAGE", "\"I am the base apk\""
//        buildConfigField "String", "MESSAGE", "\"I am the patch apk\""
        /**
         * client version would update with patch
         * so we can get the newly git version easily!
         */
        buildConfigField "String", "TINKER_ID", "\"${getTinkerIdValue()}\""
        buildConfigField "String", "PLATFORM", "\"all\""
    }

//    aaptOptions{
//        cruncherEnabled false
//    }

//    //use to test flavors support
//    productFlavors {
//        flavor1 {
//            applicationId 'tinker.sample.android.flavor1'
//        }
//
//        flavor2 {
//            applicationId 'tinker.sample.android.flavor2'
//        }
//    }

    buildTypes {
        release {
            minifyEnabled true
//            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), project.file('proguard-rules.pro')
        }
        debug {
            debuggable true
            minifyEnabled false
//            signingConfig signingConfigs.debug
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

def bakPath = file("${buildDir}/bakApk/")

/**
 * you can use assembleRelease to build you base apk
 * use tinkerPatchRelease -POLD_APK=  -PAPPLY_MAPPING=  -PAPPLY_RESOURCE= to build patch
 * add apk from the build/bakApk
 */
ext {
    // 是否使用Tinker(當你的項目處於開發調試階段時,能夠改成false)
    tinkerEnabled = true
    // 基礎包文件路徑(名字這裏寫死爲old-app.apk。用於比較新舊app以生成補丁包,不論是debug仍是release編譯)
    tinkerOldApkPath = "${bakPath}/old-app.apk"
    // 基礎包的mapping.txt文件路徑(用於輔助混淆補丁包的生成,通常在生成release版app時會使用到混淆,因此這個mapping.txt文件通常只是用於release安裝包補丁的生成)
    tinkerApplyMappingPath = "${bakPath}/old-app-mapping.txt"
    // 基礎包的R.txt文件路徑(若是你的安裝包中資源文件有改動,則須要使用該R.txt文件來輔助生成補丁包)
    tinkerApplyResourcePath = "${bakPath}/old-app-R.txt"
    //only use for build all flavor, if not, just ignore this field
    tinkerBuildFlavorDirectory = "${bakPath}/flavor"
}


def getOldApkPath() {
    return hasProperty("OLD_APK") ? OLD_APK : ext.tinkerOldApkPath
}

def getApplyMappingPath() {
    return hasProperty("APPLY_MAPPING") ? APPLY_MAPPING : ext.tinkerApplyMappingPath
}

def getApplyResourceMappingPath() {
    return hasProperty("APPLY_RESOURCE") ? APPLY_RESOURCE : ext.tinkerApplyResourcePath
}

def getTinkerIdValue() {
    return hasProperty("TINKER_ID") ? TINKER_ID : android.defaultConfig.versionName
}

def buildWithTinker() {
    return hasProperty("TINKER_ENABLE") ? TINKER_ENABLE : ext.tinkerEnabled
}

def getTinkerBuildFlavorDirectory() {
    return ext.tinkerBuildFlavorDirectory
}

if (buildWithTinker()) {
    apply plugin: 'com.tencent.tinker.patch'

    tinkerPatch {
        /**
         * necessary,default 'null'
         * the old apk path, use to diff with the new apk to build
         * add apk from the build/bakApk
         */
        oldApk = getOldApkPath()
        /**
         * optional,default 'false'
         * there are some cases we may get some warnings
         * if ignoreWarning is true, we would just assert the patch process
         * case 1: minSdkVersion is below 14, but you are using dexMode with raw.
         *         it must be crash when load.
         * case 2: newly added Android Component in AndroidManifest.xml,
         *         it must be crash when load.
         * case 3: loader classes in dex.loader{} are not keep in the main dex,
         *         it must be let tinker not work.
         * case 4: loader classes in dex.loader{} changes,
         *         loader classes is ues to load patch dex. it is useless to change them.
         *         it won't crash, but these changes can't effect. you may ignore it
         * case 5: resources.arsc has changed, but we don't use applyResourceMapping to build */ ignoreWarning = true // 是否忽略有風險的補丁包。這裏選擇忽略,當補丁包風險時不會中斷編譯。 /** * optional,default 'true' * whether sign the patch file * if not, you must do yourself. otherwise it can't check success during the patch loading
         * we will use the sign config with your build type
         */
        useSign = true

        /**
         * optional,default 'true'
         * whether use tinker to build
         */
        tinkerEnable = buildWithTinker()

        /**
         * Warning, applyMapping will affect the normal android build!
         */
        buildConfig {
            /**
             * optional,default 'null'
             * if we use tinkerPatch to build the patch apk, you'd better to apply the old * apk mapping file if minifyEnabled is enable! * Warning: * you must be careful that it will affect the normal assemble build! */ applyMapping = getApplyMappingPath() /** * optional,default 'null' * It is nice to keep the resource id from R.txt file to reduce java changes */ applyResourceMapping = getApplyResourceMappingPath() /** * necessary,default 'null' * because we don't want to check the base apk with md5 in the runtime(it is slow)
             * tinkerId is use to identify the unique base apk when the patch is tried to apply.
             * we can use git rev, svn rev or simply versionCode.
             * we will gen the tinkerId in your manifest automatic
             */
            tinkerId = getTinkerIdValue()

            /**
             * if keepDexApply is true, class in which dex refer to the old apk.
             * open this can reduce the dex diff file size.
             */
            keepDexApply = false

            /**
             * optional, default 'false'
             * Whether tinker should treat the base apk as the one being protected by app
             * protection tools.
             * If this attribute is true, the generated patch package will contain a
             * dex including all changed classes instead of any dexdiff patch-info files.
             */
            isProtectedApp = false

            /**
             * optional, default 'false'
             * Whether tinker should support component hotplug (add new component dynamically).
             * If this attribute is true, the component added in new apk will be available after
             * patch is successfully loaded. Otherwise an error would be announced when generating patch
             * on compile-time.
             *
             * <b>Notice that currently this feature is incubating and only support NON-EXPORTED Activity</b>
             */
            supportHotplugComponent = false
        }

        dex {
            /**
             * optional,default 'jar'
             * only can be 'raw' or 'jar'. for raw, we would keep its original format
             * for jar, we would repack dexes with zip format.
             * if you want to support below 14, you must use jar
             * or you want to save rom or check quicker, you can use raw mode also
             */
            dexMode = "jar"

            /**
             * necessary,default '[]'
             * what dexes in apk are expected to deal with tinkerPatch
             * it support * or ? pattern.
             */
            pattern = ["classes*.dex",
                       "assets/secondary-dex-?.jar"]
            /**
             * necessary,default '[]'
             * Warning, it is very very important, loader classes can't change with patch. * thus, they will be removed from patch dexes. * you must put the following class into main dex. * Simply, you should add your own application {@code tinker.sample.android.SampleApplication} * own tinkerLoader, and the classes you use in them * */ loader = [ //use sample, let BaseBuildInfo unchangeable with tinker "tinker.sample.android.app.BaseBuildInfo" ] } lib { /** * optional,default '[]' * what library in apk are expected to deal with tinkerPatch * it support * or ? pattern. * for library in assets, we would just recover them in the patch directory * you can get them in TinkerLoadResult with Tinker */ pattern = ["lib/*/*.so"] } res { /** * optional,default '[]' * what resource in apk are expected to deal with tinkerPatch * it support * or ? pattern. * you must include all your resources in apk here, * otherwise, they won't repack in the new apk resources.
             */
            pattern = ["res/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]

            /**
             * optional,default '[]'
             * the resource file exclude patterns, ignore add, delete or modify resource change
             * it support * or ? pattern.
             * Warning, we can only use for files no relative with resources.arsc
             */
            ignoreChange = ["assets/sample_meta.txt"]

            /**
             * default 100kb
             * for modify resource, if it is larger than 'largeModSize'
             * we would like to use bsdiff algorithm to reduce patch file size
             */
            largeModSize = 100
        }

        packageConfig {
            /**
             * optional,default 'TINKER_ID, TINKER_ID_VALUE' 'NEW_TINKER_ID, NEW_TINKER_ID_VALUE'
             * package meta file gen. path is assets/package_meta.txt in patch file
             * you can use securityCheck.getPackageProperties() in your ownPackageCheck method
             * or TinkerLoadResult.getPackageConfigByName
             * we will get the TINKER_ID from the old apk manifest for you automatic,
             * other config files (such as patchMessage below)is not necessary
             */
            configField("patchMessage", "tinker is sample to use")
            /**
             * just a sample case, you can use such as sdkVersion, brand, channel...
             * you can parse it in the SamplePatchListener.
             * Then you can use patch conditional!
             */
            configField("platform", "all")
            /**
             * patch version via packageConfig
             */
            configField("patchVersion", "1.0")
        }
        //or you can add config filed outside, or get meta value from old apk
        //project.tinkerPatch.packageConfig.configField("test1", project.tinkerPatch.packageConfig.getMetaDataFromOldApk("Test"))
        //project.tinkerPatch.packageConfig.configField("test2", "sample")

        /**
         * if you don't use zipArtifact or path, we just use 7za to try */ sevenZip { /** * optional,default '7za' * the 7zip artifact path, it will use the right 7za with your platform */ zipArtifact = "com.tencent.mm:SevenZip:1.1.10" /** * optional,default '7za' * you can specify the 7za path yourself, it will overwrite the zipArtifact value */ // path = "/usr/local/bin/7za" } } List<String> flavors = new ArrayList<>(); project.android.productFlavors.each { flavor -> flavors.add(flavor.name) } boolean hasFlavors = flavors.size() > 0 def date = new Date().format("MMdd-HH-mm-ss") /** * bak apk and mapping */ android.applicationVariants.all { variant -> /** * task type, you want to bak */ def taskName = variant.name tasks.all { if ("assemble${taskName.capitalize()}".equalsIgnoreCase(it.name)) { it.doLast { copy { def fileNamePrefix = "${project.name}-${variant.baseName}" def newFileNamePrefix = hasFlavors ? "${fileNamePrefix}" : "${fileNamePrefix}-${date}" def destPath = hasFlavors ? file("${bakPath}/${project.name}-${date}/${variant.flavorName}") : bakPath from variant.outputs.first().outputFile into destPath rename { String fileName -> fileName.replace("${fileNamePrefix}.apk", "${newFileNamePrefix}.apk") } from "${buildDir}/outputs/mapping/${variant.dirName}/mapping.txt" into destPath rename { String fileName -> fileName.replace("mapping.txt", "${newFileNamePrefix}-mapping.txt") } from "${buildDir}/intermediates/symbols/${variant.dirName}/R.txt" into destPath rename { String fileName -> fileName.replace("R.txt", "${newFileNamePrefix}-R.txt") } } } } } } project.afterEvaluate { //sample use for build all flavor for one time if (hasFlavors) { task(tinkerPatchAllFlavorRelease) { group = 'tinker' def originOldPath = getTinkerBuildFlavorDirectory() for (String flavor : flavors) { def tinkerTask = tasks.getByName("tinkerPatch${flavor.capitalize()}Release") dependsOn tinkerTask def preAssembleTask = tasks.getByName("process${flavor.capitalize()}ReleaseManifest") preAssembleTask.doFirst { String flavorName = preAssembleTask.name.substring(7, 8).toLowerCase() + preAssembleTask.name.substring(8, preAssembleTask.name.length() - 15) project.tinkerPatch.oldApk = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-release.apk" project.tinkerPatch.buildConfig.applyMapping = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-release-mapping.txt" project.tinkerPatch.buildConfig.applyResourceMapping = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-release-R.txt" } } } task(tinkerPatchAllFlavorDebug) { group = 'tinker' def originOldPath = getTinkerBuildFlavorDirectory() for (String flavor : flavors) { def tinkerTask = tasks.getByName("tinkerPatch${flavor.capitalize()}Debug") dependsOn tinkerTask def preAssembleTask = tasks.getByName("process${flavor.capitalize()}DebugManifest") preAssembleTask.doFirst { String flavorName = preAssembleTask.name.substring(7, 8).toLowerCase() + preAssembleTask.name.substring(8, preAssembleTask.name.length() - 13) project.tinkerPatch.oldApk = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-debug.apk" project.tinkerPatch.buildConfig.applyMapping = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-debug-mapping.txt" project.tinkerPatch.buildConfig.applyResourceMapping = "${originOldPath}/${flavorName}/${project.name}-${flavorName}-debug-R.txt" } } } } } } 複製代碼

在這裏特別須要注意的幾點:

    1)、若是出現這個錯誤

請檢查這個是否配置正確

def getTinkerIdValue() {
    return hasProperty("TINKER_ID") ? TINKER_ID : android.defaultConfig.versionName
}複製代碼

ps:我拷貝官方demo例子的gradle配置就報這個錯,後來把它改爲上面同樣就好了。

     2)、把這個改成true:ignoreWarning = true   // 是否忽略有風險的補丁包。這裏選擇忽略,當補丁包風險時不會中斷編譯。否則在生成補丁包會出錯。

    3)、而後就是這個:

tinkerOldApkPath:基礎包所在路徑複製代碼

之因此改爲old-app.apk,主要是爲了區分新的apk和基礎包,由於每次build一次都會生成一個apk

雖然每次都不一樣,你不改下名字根據編號很容易搞錯。官方例子是用的apk名稱ps:old-app.apk和old-app-mapping.txt很重要!!!你後面製做補丁都須要根據這個基礎包來製做,最好保存好並備份!!,若是不當心弄丟了,那就只能推送更新app吧~~~

基礎包:指的是發佈出去的安裝包(用戶正在使用的安裝包)。

4)、注意這個BuildInfo引用的BuildConfig類:


一開始會報錯,找不到這個BuildConfig類,你build一下就出來了。ps:我能說我直接把這個BuildInfo給的PLATFORM改爲空了麼,在後面打補丁死活打不上~~~~~,主要仍是gradle配置不能出錯~~~~


這裏的字段都是gradle配置的時候生成的。


配置好gradle後build下,看下是否成功,而後切換project視圖app>build目錄下是否生成bakApk目錄,上圖所示。

打開gradle操做界面雙擊TinkerPatchDebug,生成補丁包


切換project視圖app>build>outputs>tinkerPatch目錄下看是否生成補丁:


解釋:


若是失敗或者沒有生成這些個文件夾,請仔細檢查你的gradle配置是否正確~~。

拷貝java文件(官方demo的這些文件~~):




  • SampleUncaughtExceptionHandler:Tinker的全局異常捕獲器。
  • MyLogImp:Tinker的日誌輸出實現類。
  • SampleLoadReporter:加載補丁時的一些回調。
  • SamplePatchListener:過濾Tinker收到的補丁包的修復、升級請求。
  • SamplePatchReporter:修復或者升級補丁時的一些回調。
  • SampleTinkerReport:修復結果(成功、衝突、失敗等)。
  • SampleResultService::patch補丁合成進程將合成結果返回給主進程的類。
  • TinkerManager:Tinker管理器(安裝、初始化Tinker)。
  • TinkerUtils:拓展補丁條件斷定、鎖屏或後臺時應用重啓功能的工具類。
  • 這些只是對Tinker功能的拓展和封裝,都是可選的,你也能夠本身封裝

    TinkerApplication,這個類並非繼承Application,若是本身有自定義的application能夠把初始化操做放在onCreat()方法裏面:

    @SuppressWarnings("unused")
    @DefaultLifeCycle(application = "com.oking.mytinker.OriginalApplication",// application類名。只能用字符串,這個MyApplication文件是不存在的,但能夠在AndroidManifest.xml的application標籤上使用(name)
            flags = ShareConstants.TINKER_ENABLE_ALL,// tinkerFlags
            loaderClass = "com.tencent.tinker.loader.TinkerLoader",//loaderClassName, 咱們這裏使用默認便可!(可不寫)
            loadVerifyFlag = false)//tinkerLoadVerifyFlag
    public class TinkerApplication extends DefaultApplicationLike {
    
        private Application mApplication;
        private Context mContext;
        private Tinker mTinker;
    
        // 固定寫法
        public TinkerApplication(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
            super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
        }
    
        // 固定寫法
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
            getApplication().registerActivityLifecycleCallbacks(callback);
        }
    
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onBaseContextAttached(Context base) {
            super.onBaseContextAttached(base);
            mApplication = getApplication();
            mContext = getApplication();
            initTinker(base);
            // 能夠將以前自定義的Application中onCreate()方法所執行的操做搬到這裏...
        }
    
        private void initTinker(Context base) {
            // tinker須要你開啓MultiDex
            MultiDex.install(base);
    
            TinkerManager.setTinkerApplicationLike(this);
            // 設置全局異常捕獲
            TinkerManager.initFastCrashProtect();
            //開啓升級重試功能(在安裝Tinker以前設置)
            TinkerManager.setUpgradeRetryEnable(true);
            //設置Tinker日誌輸出類
            TinkerInstaller.setLogIml(new MyLogImp());
            //安裝Tinker(在加載完multiDex以後,不然你須要將com.tencent.tinker.**手動放到main dex中)
            TinkerManager.installTinker(this);
            mTinker = Tinker.with(getApplication());
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            // 將以前自定義的Application中onCreate()方法所執行的操做搬到這裏...
    
            String appId = "20171213203556412-8689";
            String appSecret = "cd34d15329cb4caeac3bbd4dc335707d";
            PatchManager.getInstance().init(getApplication(), "http://192.168.0.105:8080/hotfix-apis", appId, appSecret, new IPatchManager() {
                @Override
                public void cleanPatch(Context context) {
    //                TinkerInstaller.cleanPatch(context);
                }
    
                @Override
                public void patch(Context context, String patchPath) {
    //                TinkerInstaller.onReceiveUpgradePatch(context, patchPath);
                    Contact.patchPath = patchPath;
                    System.out.println("patch:"+patchPath);
    
                }
            });
            PatchManager.getInstance().register(new Listener() {
                @Override
                public void onQuerySuccess(String response) {
                    Log.d("TinkerApplication","獲取補丁成功"+response);
                }
    
                @Override
                public void onQueryFailure(Throwable e) {
                    Log.d("TinkerApplication","獲取補丁失敗"+e.getMessage());
                }
    
                @Override
                public void onDownloadSuccess(String path) {
                    Log.d("TinkerApplication","下載補丁成功"+path);
                }
    
                @Override
                public void onDownloadFailure(Throwable e) {
    
                }
    
                @Override
                public void onPatchSuccess() {
    
                }
    
                @Override
                public void onPatchFailure(String error) {
    
                }
    
                @Override
                public void onLoadSuccess() {
    
                }
    
                @Override
                public void onLoadFailure(String error) {
    
                }
            });
            PatchManager.getInstance().setTag("");
            PatchManager.getInstance().setChannel("");
            PatchManager.getInstance().queryAndPatch();
        }
    }
    
    複製代碼

    清單文件:

    權限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>複製代碼

    application

    這個文件是動態生成,不存在咱們項目中的,可是咱們項目是能夠引用的

    android:name=".OriginalApplication"複製代碼

    Service

    <service
        android:name="com.oking.mytinker.tinker.SampleResultService"
        android:exported="false"/>複製代碼

    3、服務器搭建

    放個Github地址

    一、下載部署所須要的文件(war包、配置文件、建庫sql文件) war包下載.

    二、在mysql(須要5.x版本)裏面建數據庫,建表sql在patchserver-manager/import.sql中

    三、把hotfix-apis.properties和hotfix-console.properties兩個配置文件放到/opt/config(*若是是windows部署,放置在tomcat對應的盤符下,假如tomcat在d://tomcat 目錄配置文件就放在d://opt/config目錄下,而且修改裏面對應的配置(數據源配置、訪問路徑配置、補丁存放目錄)

    四、把hotfix-apis.war hotfix-console.war放到tomcat下面的webapps目錄下

    等服務啓動完畢就能夠在瀏覽器上訪問http://localhost:8080/hotfix-console

    會配置Tomcat或者會一點Javaee方面的知識的部署起來會很容易。

    下載Tomcat配置好環境變量

    安裝數據庫Mysql新建數據庫patch_manager,ps:注意名稱要和配置文件數據庫名稱一致

    修改hotfix-apis.properties配置文件


    修改hotfix-console.properties文件


    注意上面加粗說明,文件別放錯了!!給個部署參照

    拷貝war包到tomcat的webapp目錄下

    運行Tomcat服務器

    訪問地址,註冊登陸。


    到目前爲止咱們已經把Tinker集成好了,服務器也部署成功了,下面咱們來擼代碼~~~

    4、擼代碼、實操

    app的Gradle加入一行

    compile 'com.dx168.patchsdk:patchsdk:1.2.7'複製代碼

    上面集成Tinker的時候有加上就不用加了

    TinkerApplication的onCreat方法裏面加入:

    String appId = "20171214154046922-6495";
            String appSecret = "9f820d28ac854e9a82e755fefd69ea63";
            PatchManager.getInstance().init(getApplication(), "http://192.168.0.105:8080/hotfix-apis", appId, appSecret, new IPatchManager() {
                @Override
                public void cleanPatch(Context context) {
    //                TinkerInstaller.cleanPatch(context);
                }
    
                @Override
                public void patch(Context context, String patchPath) {
    //                TinkerInstaller.onReceiveUpgradePatch(context, patchPath);
                    Contact.patchPath = patchPath;
                    System.out.println("patch:"+patchPath);
    
                }
            });
            PatchManager.getInstance().register(new Listener() {
                @Override
                public void onQuerySuccess(String response) {
                    Log.d("TinkerApplication","獲取補丁成功"+response);
                }
    
                @Override
                public void onQueryFailure(Throwable e) {
                    Log.d("TinkerApplication","獲取補丁失敗"+e.getMessage());
                }
    
                @Override
                public void onDownloadSuccess(String path) {
                    Log.d("TinkerApplication","下載補丁成功"+path);
                }
    
                @Override
                public void onDownloadFailure(Throwable e) {
    
                }
    
                @Override
                public void onPatchSuccess() {
    
                }
    
                @Override
                public void onPatchFailure(String error) {
    
                }
    
                @Override
                public void onLoadSuccess() {
    
                }
    
                @Override
                public void onLoadFailure(String error) {
    
                }
            });
            PatchManager.getInstance().setTag("");
            PatchManager.getInstance().setChannel("");
            PatchManager.getInstance().queryAndPatch();複製代碼

    咱們登陸補丁管理後臺,建立應用後面會獲得appId和appSecret,保存好:


    用過第三方平臺的都明白是什麼意思,這個也是同樣的。

    建立一個版本:


    在AS中build project

    而後把安裝包併發送到手機上進行安裝(ps我這直接改了名字了~~):


    安卓界面咱們長這樣~~~ps注意中間文本和Toast~:


    假設咱們項目上線了忽然出現緊急bug(若是用檢測更新推送新安裝包,這個用戶體驗就很很差了~~安裝包不要流量?一個小小的bug就讓我下載安裝包更新?差評啊,卸載~~~)

    咱們修復好的代碼:




    而後,咱們找到上個版本安裝包和txt文件把它放在bakApk目錄下更名爲old-app,切換到gradle視圖雙擊tinkerPatchDebug:


    找到patch_signed_7zip.apk補丁包,拷貝到桌面



    進入補丁管理後臺上傳補丁:


    發佈補丁:


    從新打開app點擊「修復」按鈕:

    查看控制檯打印日誌

    app界面有修復成功提示



    而後點退出,從新打開應用咱們上面修改的代碼都同步到app上來了,用戶不用從新下載安裝包且無感知修復bug:


    整個熱修復過程就這樣~~~說實話有點複雜~~

    ps:修復成功後再點擊修復按鈕是沒用的,一個補丁成功完成一次修復使命也就結束了,同時補丁文件也被刪除了(不用擔憂補丁塞滿sd卡啥的~~),即便你再次用同一個補丁也是打不了補丁的,這就是補丁的做用,Tinker爲咱們作了處理,不過你能夠卸載某個版本的補丁或者所有補丁~~~,具體請看文檔。。。。


    5、總結

    Tinker的熱修復遠不止我講的這些像資源文件、so、library修復等,更深層次原理性的東西還須要進一步去學習~~~官方WIKI文檔。

    關於那個補丁管理平臺,開源的後臺,下發補丁,管理補丁、補丁統計、黑名單機制等等。還有源代碼,有興趣的大佬能夠去研究研究改造改造~~,這樣咱們就不用出錢給第三方補丁平臺了。。。若是單純的就下發補丁,能夠不用這個平臺,後臺寫個相似版本更新的接口就能夠,下載文件>修復。平臺的好處就是更加方便、智能化管理、統計數據。

    相關文章
    相關標籤/搜索