android bugly热更新集成

244 阅读3分钟

背景

本篇主要介绍最新bugly的使用,详细操作步骤请查阅官方文档,本文不再赘述。

1.引入bugly最新依赖包

(1)项目外层build.gradle bugly引入

classpath "com.tencent.bugly:tinker-support:latest.release"

注意:此处最好指定最新版本,不要指定具体版本好,如需指定,请查阅相关文档,将tinker Sdk保持一致,否则会报异常。

(2)内层build.gradle 依赖引入

 compile "com.android.support:multidex:1.0.1" // 多dex配置
  //注释掉原有bugly的仓库
  //compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.3.4
  compile 'com.tencent.bugly:crashreport_upgrade:1.3.6'
  // 指定tinker依赖版本(注:应用升级1.3.5版本起,不再内置tinker)
  compile 'com.tencent.tinker:tinker-android-lib:1.9.9'
  compile 'com.tencent.bugly:nativecrashreport:latest.release' //其中latest.release指代最新版本号,也可以指定明确的版本号,例如2.2.0


(3)新建tinker-support.gradle脚本文件

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

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

/**
* 此处填写每次构建生成的基准包目录
*/
def baseApkDir = "app-0130-14-44-01"

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

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

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

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

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

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

  // 对应tinker插件applyResourceMapping
  baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
  //基础包base 补丁包patch
  // 构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性
  tinkerId = "patch-1.0.2"

  // 构建多渠道补丁时使用
  // 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的分配
  }
}

**讲解:**以上几个参数需要注意。

1.baseApkDir="xxxx" //表示现在打包的基础包所在目录;

  1. baseApk = "bakPath/{bakPath}/{baseApkDir}/wanAndroid-release.apk" //打包后的应用名称

  2. tinkerId = "patch-1.0.2" //构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性, 重要,基础包base 补丁包patch,版本号可以更改 // 是否开启反射Application模式,若为false时,请参考文档。 4、enableProxyApplication = true

(4)内部build.gradle 引入thinker.support.gradle脚本文件 及配置

// 依赖插件脚本
 apply from: 'tinker-support.gradle'

(5)配置应用打包签名文件

 signingConfigs {
       releaseConfig {
           keyAlias 'key'
           keyPassword '123456'
           storeFile file('../test.jks')
           storePassword '123456'
       }
   }

   buildTypes {
       release {
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
           signingConfig signingConfigs.releaseConfig
           android.applicationVariants.all { variant ->
               // 更新至Android Studio 3.3 gradle 4.10.1
               variant.outputs.all {
                   outputFileName = "wanAndroid-release.apk"
               }

           }
       }
   }

(6)新建Application

class App :Application(){
   //定义静态变量
   companion object{
       var context: Context by Delegates.notNull(

       )
       lateinit var instance: Application

   }


   override fun onCreate() {
       super.onCreate()
       instance=this
       context=this;
       Bugly.init(context, "your appId", true);

   }

   override fun attachBaseContext(base: Context?) {
       super.attachBaseContext(base)
       // you must install multiDex whatever tinker is installed!
       MultiDex.install(base);
       // 安装tinker
       Beta.installTinker();
   }
}

(7)打包基础正式包

选中右侧Gradle->app->tasks->other->assembleRelease 打包为正式包并将应用安装到手机上

(8)thinker-support 打包补丁包

执行完成后,将在项目文件下app\build\outputs\patch\xxx.patch_signed_7zip 最后将补丁文件上上传

(9)补丁包下发

出现已下发数量表示下发成功。

end:写博客不易,如对你有所帮助请手动点赞。