天天看點

Unity3D Bugly熱更新操作指南Unity3D Bugly熱更新操作指南

Unity3D Bugly熱更新操作指南

bugly的熱更新使用的是騰訊開源的熱更方案Tinker,使用的方法比較簡單。需要用到的工具Android Studio

  1. Unity導出安卓工程到AndroidStudio中打開
  2. 在打開的工程中找到自己項目的build.gradle
buildscript {
	repositories {
		jcenter {
			url "http://jcenter.bintray.com/"
		}
		google()
		maven { url "https://maven.google.com" }

	}

	dependencies {
		classpath 'com.android.tools.build:gradle:3.0.1'
		//引入的tinker的support依賴,可以指定版本,此處指定的是最新版本
		classpath "com.tencent.bugly:tinker-support:latest.release"
	}
	
}

android{
	defaultConfig {
	    /*
        ...其他的一些配置
        */
		ndk {
            abiFilters 'armeabi'
        }
	}
}	

dependencies {

	implementation "com.android.support:multidex:1.0.2" // 多dex配置
	//注釋掉原有bugly的倉庫,如果本身的項目使用了Bugly的話,須删除之前的jar包和依賴否則會報錯
	//compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.3.4
	implementation 'com.tencent.bugly:crashreport_upgrade:latest.release'
	// 指定tinker依賴版本(注:應用更新1.3.5版本起,不再内置tinker)
	implementation 'com.tencent.tinker:tinker-android-lib:latest.release'
	implementation 'com.tencent.bugly:nativecrashreport:latest.release'
    }
 //添加tinker-support.gradle的引用
 apply from: 'tinker-support.gradle'   

           

3.添加tinker-support.gradle檔案到需要打包的工程中(Unity導出的工程的話,就加在上一步修改build.gradle一個層級)

具體的内容如下:

apply plugin: 'com.tencent.bugly.tinker-support'

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

/**
 * 此處填寫每次建構生成的基準包目錄
 */
def baseApkDir = "AndroidWeeder-0613-14-41-00"

/**
 * 對于插件各參數的詳細解析請參考
 */
tinkerSupport {

    // 開啟tinker-support插件,預設值true
    enable = true

    // 指定歸檔目錄,預設值目前module的子目錄tinker
    autoBackupApkDir = "${bakPath}"

    // 是否啟用覆寫tinkerPatch配置功能,預設值false
    // 開啟後tinkerPatch配置不生效,即無需添加tinkerPatch
    overrideTinkerPatchConfiguration = true

    // 編譯更新檔包時,必需指定基線版本的apk,預設值為空
    // 如果為空,則表示不是進行更新檔包的編譯
    // @{link tinkerPatch.oldApk }
    baseApk = "${bakPath}/${baseApkDir}/app-release.apk"

    // 對應tinker插件applyMapping
    baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"

    // 對應tinker插件applyResourceMapping
    baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"

    // 建構基準包和更新檔包都要指定不同的tinkerId,并且必須保證唯一性
    //tinkerId = "base-1.0.1"
    autoGenerateTinkerId = true
    // 建構多管道更新檔時使用
    buildAllFlavorsDir = "${bakPath}/${baseApkDir}"

    // 是否啟用加強模式,預設為false.(tinker-spport 1.0.7起支援)
    isProtectedApp = true

    // 是否開啟反射Application模式
    enableProxyApplication = true


    // 是否支援新增非export的Activity(注意:設定為true才能修改AndroidManifest檔案)
    supportHotplugComponent = true

}

/**
 * 一般來說,我們無需對下面的參數做任何的修改
 * 對于各參數的詳細介紹請參考:
 * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
 */
tinkerPatch {
    //oldApk ="${bakPath}/${appName}/app-release.apk"
    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
        //tinkerId = "1.0.1-base"
        //applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" //  可選,設定mapping檔案,建議保持舊apk的proguard混淆方式
        //applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可選,設定R.txt檔案,通過舊apk檔案保持ResId的配置設定
    }
}
           

注意:此配置在已經設定tinkerID為自動建立,無需自行指定tinkerID。

4.初始化Bugly,

Unity中已經初始化的代碼注釋掉後,在Android工程中建立UnityApplication,并将其在AndroidManifest-application節點中設定為啟動類。
           
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.we.game.sdk.core.WeGameApplication;

/**
 * Created by Administrator on 2019/5/5.
 */

public class UnityApplication extends MultiDexApplication {
    @Override
    public void onCreate() {

        super.onCreate();
        WeGameApplication.onCreate(this);
        Bugly.init(this,"此處填寫項目的BuglyID",false);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        WeGameApplication.onTerminate();
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        WeGameApplication.attachBaseContext(base);
        
        ///此處以下的為熱更新所需要加入的
        MultiDex.install(base);
        // 安裝tinker
        Beta.installTinker();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        WeGameApplication.onConfigurationChanged(newConfig);
    }
}

           

5.打基準包

基準包就是正常的打包流程,使用Gradle的build下邊的各個配置打包
打出來的包會被放置在build-bakAPk下邊,每次打包都會根據時間生成檔案夾存儲
           

6.打更新檔

更新檔可以在Gradle下tinker-support中找到對應平台的buildTinkerPatchsomeplatformRelease操作
生成的檔案會被移動到Output-patch檔案夾下。
将生成的三個檔案中名稱中包含7zip的檔案通過bugly背景上傳即可。