美文网首页
Android APP/apk集成Bugly热更新应用升级详细步

Android APP/apk集成Bugly热更新应用升级详细步

作者: Lee坚武 | 来源:发表于2021-01-07 20:38 被阅读0次

工作需求:游戏需要进行热更,后期需要更新资源打包

了解到使用腾讯bugly很方便
开始接入步骤
1.在项目根目录Project的build.gradle中加入依赖
buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'//因为使用3.4版本的会编译错误,说有的方法没有了,所以我查询资料后,改为3.2的
        classpath "com.tencent.bugly:tinker-support:1.1.5"
        classpath 'com.tencent.bugly:symtabfileuploader:latest.release'//Bugly符号表插件
        classpath 'com.meituan.android.walle:plugin:1.1.6'//多渠道打包
    }
}

以下是接入进来的效果:


image.png
2.在app文件夹的build.gradle中加入依赖
dependencies {
    api fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:26.1.0'
    // 多dex配置
    compile "com.android.support:multidex:1.0.2"
    // 本地集成aar方式
    // compile(name: 'bugly_crashreport_upgrade-1.3.6', ext: 'aar')

    // 远程依赖集成方式(推荐)
    compile "com.tencent.bugly:crashreport_upgrade:1.4.2"
    // 指定tinker依赖版本(注:应用升级1.3.5版本起,不再内置tinker)
    compile 'com.tencent.tinker:tinker-android-lib:1.9.9'
    /*渠道信息统计分析*/
    compile 'com.meituan.android.walle:library:1.1.6'
}
app
3.在app的build.gradle中配置支持的so库架构
defaultConfig {
        multiDexEnabled true
        applicationId "com.tanwan.game"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 9
        versionName "1.9"
        ndk {
            // 设置支持的SO库架构,第三方给的so库哪几种架构,就配置这几种架构
            abiFilters 'armeabi','x86', 'armeabi-v7a', 'x86_64', 'mips','mips64'
        }
    }
image.png
4.在app的build.gradle同级目录创建tinker-support.gradle文件
apply plugin: 'com.tencent.bugly.tinker-support'

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

/**
 * 此处填写每次构建生成的基准包目录
 */
def baseApkDir = "app-1228-10-37-24"
def tanwanDir_1 = "app-1226-18-03-54"
//def tanwanDir_2 = "app-1228-14-29-03"
//def tanwanDir_3 = "app-1230-15-20-55"
//def tanwanDir_4 = "app-1230-20-11-41"

/**
 * 对于插件各参数的详细解析请参考
 */
tinkerSupport {

    // 开启tinker-support插件,默认值true
    enable = true

    // 自动生成tinkerId, 你无须关注tinkerId,默认为false
    autoGenerateTinkerId = true

    // 指定归档目录,默认值当前module的子目录tinker
    autoBackupApkDir = "${bakPath}"

    // 是否启用覆盖tinkerPatch配置功能,默认值false
    // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
    overrideTinkerPatchConfiguration = true

    // 编译补丁包时,必需指定基线版本的apk,默认值为空
    // 如果为空,则表示不是进行补丁包的编译
    // @{link tinkerPatch.oldApk }
    baseApk = "${bakPath}/${baseApkDir}/app-release.apk"
    baseApk = "${bakPath}/${tanwanDir_1}/app-release.apk"
//    baseApk = "${bakPath}/${tanwanDir_2}/app-release.apk"
//    baseApk = "${bakPath}/${tanwanDir_3}/app-release.apk"
//    baseApk = "${bakPath}/${tanwanDir_4}/app-release.apk"
//    baseApk =  "${bakPath}/${baseApkDir}/app-debug.apk"

    // 对应tinker插件applyMapping
    baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"
    baseApkProguardMapping = "${bakPath}/${tanwanDir_1}/app-release-mapping.txt"
//    baseApkProguardMapping = "${bakPath}/${tanwanDir_2}/app-release-mapping.txt"
//    baseApkProguardMapping = "${bakPath}/${tanwanDir_3}/app-release-mapping.txt"
//    baseApkProguardMapping = "${bakPath}/${tanwanDir_4}/app-release-mapping.txt"
//    baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-debug-mapping.txt"

    // 对应tinker插件applyResourceMapping
    baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
    baseApkResourceMapping = "${bakPath}/${tanwanDir_1}/app-release-R.txt"
//    baseApkResourceMapping = "${bakPath}/${tanwanDir_2}/app-release-R.txt"
//    baseApkResourceMapping = "${bakPath}/${tanwanDir_3}/app-release-R.txt"
//    baseApkResourceMapping = "${bakPath}/${tanwanDir_4}/app-release-R.txt"
//    baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-debug-R.txt"

    // 构建基准包跟补丁包都要修改tinkerId,主要用于区分
    tinkerId = "1.9-patch"

    // 打多渠道补丁时指定目录
    // buildAllFlavorsDir = "${bakPath}/${baseApkDir}"

    // 是否使用加固模式,默认为false
    // isProtectedApp = true

    // 是否采用反射Application的方式集成,无须改造Application
    enableProxyApplication = true

    // 支持新增Activity
    supportHotplugComponent = true

}

/**
 * 一般来说,我们无需对下面的参数做任何的修改
 * 对于各参数的详细介绍请参考:
 * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
 */
tinkerPatch {
    tinkerEnable = true
    ignoreWarning = false
    useSign = false
    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 = "base-2.0.1"
    }
}
image.png
5.在app的build.gradle配置依赖插件脚本
apply plugin: 'com.android.application'
apply from: 'tinker-support.gradle'
apply plugin: 'bugly'
apply plugin: 'walle'
image.png
6.自定义MyApplication
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.interfaces.BetaPatchListener;

import java.util.Locale;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Beta.canAutoDownloadPatch = true;
        Beta.canAutoPatch = true;
        Beta.canNotifyUserRestart = true;
        Beta.betaPatchListener = new BetaPatchListener() {
            @Override
            public void onPatchReceived(String patchFile) {
                Toast.makeText(getApplicationContext(), "补丁下载地址" + patchFile, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadReceived(long savedLength, long totalLength) {
                Toast.makeText(getApplicationContext(),
                        String.format(Locale.getDefault(), "%s %d%%",
                                Beta.strNotificationDownloading,
                                (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadSuccess(String msg) {
                Toast.makeText(getApplicationContext(), "补丁下载成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadFailure(String msg) {
                Toast.makeText(getApplicationContext(), "补丁下载失败", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onApplySuccess(String msg) {
                Toast.makeText(getApplicationContext(), "补丁应用成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onApplyFailure(String msg) {
                Toast.makeText(getApplicationContext(), "补丁应用失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPatchRollback() {
                Toast.makeText(getApplicationContext(), "补丁回滚", Toast.LENGTH_SHORT).show();
            }
        };
        //bugly初始化,第二个参数填写在bugly后台生成的项目id
        Bugly.init(this,"9f3976fc55",false);

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
        Beta.installTinker();


    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Beta.unInit();
    }
}
image.png
7.配置AndroidManifest权限
<!--Bugly权限开始-->
<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.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!--Bugly权限结束-->
image.png
8.配置AndroidManifest&provider兼容Android N
<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>
image.png

开始编译基准包

image.png

改好然后开始编译生成基准包之后

编译补丁包时候,修改tinkerId


image.png

APP的build.gradle修改版本号


image.png

然后点击


image.png
后面生成的包就可以上传到bugly后台然后下发了!

相关文章

网友评论

      本文标题:Android APP/apk集成Bugly热更新应用升级详细步

      本文链接:https://www.haomeiwen.com/subject/aevooktx.html