




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android热更新框架Tinker探索之旅现在比较流行的热修复框架的优缺点和特色,在Tinker的wiki下都可以看到,这里就不在赘述了,Tinker只用了这句话来展现自己的优势: Tinker已运行在微信的数亿Android设备上,那么为什么你不使用Tinker呢?Tinker分为gradle接入和命令行接入,但是我这种菜鸟玩不转命令行,所以只能照搬gradle接入Tinker了。但是我感觉,命令行算不上一种接入,仅仅是多了一种编译差分包的方式而已,当然,这仅仅是个人见解哈,至于我为什么这么说,大家往下看就知道了。首先,在project的build.gradle文件里需要添加Tinker的插件依赖dependencies classpath com.android.tools.build:gradle:1.3.0 . /Tinker classpath com.tencent.tinker:tinker-patch-gradle-plugin:1.7.7 其次就是主module的build.gradle文件的配置,主要有下面几个地方1.引入Tinker依赖dependencies /tinker必须要引入多dex打包支持 compile com.android.support:multidex:1.0.1 /可选,用于注解生成application类 provided(com.tencent.tinker:tinker-android-anno:1.7.7 /tinker依赖版本 compile(com.tencent.tinker:tinker-android-lib:1.7.7) 2.tinker的一些配置 /-tinker config start- buildConfigField String,TINKER_VERSION,1.7.7 /* * buildConfig can change during patch! * we can use the newly value when patch */ buildConfigField String, MESSAGE, I am the base 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 /-tinker config end-3.编译差分包的开关和老版本文件位置确定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 def oldPrefixFormat=app_beta1_$appVersionName_$new Date().format(yyyy_MM_dd)_%s_build$appVersionCode_%s def oldApkPath=String.format(oldPrefixFormat,10_34_38,tinker.apk) def applyMappingPath=String.format(oldPrefixFormat,10_34_38,mapping.txt) def resourcePath=String.format(oldPrefixFormat,10_34_38,R.txt) /for some reason, you may want to ignore tinkerBuild, such as instant run debug build? tinkerEnabled = true /for normal build /old apk file to build patch apk tinkerOldApkPath = $bakPath/$oldApkPath /proguard mapping file to build patch apk tinkerApplyMappingPath = $bakPath/$applyMappingPath /resource R.txt to build patch apk, must input if there is resource changed tinkerApplyResourcePath = $bakPath/$resourcePath /only use for build all flavor, if not, just ignore this field tinkerBuildFlavorDirectory = $bakPath/app-patch-$releaseTime()4.tinker编译差分包的task配置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 wont crash, but these changes cant effect. you may ignore it * case 5: resources.arsc has changed, but we dont use applyResourceMapping to build */ ignoreWarning = false /* * optional,default true * whether sign the patch file * if not, you must do yourself. otherwise it cant 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, youd 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 dont 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 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 cant 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 com.simpletour.client.tinker.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 wont 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 dont 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 5.拷贝和自定义apk、mapping、R文件的别名 /* * bak apk and mapping */ android.applicationVariants.all variant - /* * task type, you want to bak */ def taskName = def date = new Date().format(yyyy_MM_dd_HH_mm_ss) def buildTypeName=variant.baseName+ def isRelease=buildTypeName=release tasks.all if (assemble$taskName.capitalize().equalsIgnoreCase() it.doLast copy /正常情况下生成的apk文件的文件名前缀 def fileNamePrefix = $-$buildTypeName /自定义apk文件的别名 def newFileNamePrefix=app_beta1_$isRelease?$buildTypeName_:$appVersionName_$date_build$appVersionCode /自定义apk、mapping、R文件存放的目录 def destPath = hasFlavors ? file($bakPath/$-$date/$variant.flavorName) : bakPath /复制apk文件和重命名 from variant.outputs.outputFile into destPath rename String fileName - fileName.replace($fileNamePrefix.apk, $newFileNamePrefix_tinker.apk) /复制和重命名mapping文件 from $buildDir/outputs/mapping/$variant.dirName/mapping.txt into destPath rename String fileName - fileName.replace(mapping.txt, $newFileNamePrefix_mapping.txt) /复制和重命名R文件 from $buildDir/intermediates/symbols/$variant.dirName/R.txt into destPath rename String fileName - fileName.replace(R.txt, $newFileNamePrefix_R.txt) 有了上面这些基本的配置,基本上tinker的引入就算完成了。这里要说一下引入multidex的目的不仅是解决65536的限制,还有就是因为tinker的修复原理是dex合并,所以必须支持multidex。还有就是编译差分包的时候,老版本apk、mapping、R文件的路径一定要配正确,不然tinkerPatch任务会执行失败。由于tinker不支持dex加固后的热修复,所以采用dex加固加密的apk,就不能使用tinker来做热更新了。最后要注意的就是,tinker补丁安装完成后,必须重启你的应用才能生效,在华为手机上甚至需要清除缓存的应用进程后重启才能生效。下面是完整的app-build.gradle文件apply plugin: com.android.applicationdef appVersionCodedef appVersionNameandroid lintOptions abortOnError false /tinker recommend dexOptions jumboMode true; signingConfigs release keyAlias 你的签名key的别名 keyPassword 你的签名key的密码 storeFile file(你的签名文件) storePassword 你的签名文件的密码 debug keyAlias 你的签名key的别名 keyPassword 你的签名key的密码 storeFile file(你的签名文件) storePassword 你的签名文件的密码 compileSdkVersion 23 buildToolsVersion 23.0.1 defaultConfig applicationId 你的工程主module包名 minSdkVersion 15 targetSdkVersion 23 multiDexEnabled = true versionCode 68 versionName 2.4.0 signingConfig signingConfigs.release /-tinker config start- appVersionCode=versionCode appVersionName=v.concat(versionName) buildConfigField String,TINKER_VERSION,1.7.7 /* * buildConfig can change during patch! * we can use the newly value when patch */ buildConfigField String, MESSAGE, I am the base 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 /-tinker config end- buildTypes debug / 不显示log buildConfigField boolean, ENABLE_DEBUG, true /不启用混淆编译 minifyEnabled false /移除无用的资源文件 shrinkResources true /Zipalign优化 zipAlignEnabled true proguardFiles getDefaultProguardFile(proguard-android.txt), signingConfig signingConfigs.release release / 不显示log buildConfigField boolean, ENABLE_DEBUG, false /不启用混淆编译 minifyEnabled false /移除无用的资源文件 shrinkResources true /Zipalign优化 zipAlignEnabled true proguardFiles getDefaultProguardFile(proguard-android.txt), signingConfig signingConfigs.release /解决编译时as内存溢出 dexOptions preDexLibraries = false javaMaxHeapSize 4g /这个改大 repositories mavenCentral() flatDir dirs libs def releaseTime() return new Date().format(yyyy_MM_dd)dependencies compile fileTree(include: *.jar, dir: libs) compile com.android.support:multidex:1.0.1 /tinker recommend options ,可选,用于生成application类 provided(com.tencent.tinker:tinker-android-anno:1.7.7/-tinker build start-/生成tinker_id,在app/build/intermediates/manifests/full/debug(或release)下的Manifest.xml/文件里可以看到def gitSha() try String gitRev = git rev-parse -short HEAD.execute(null, project.rootDir).text.trim() if (gitRev = null) throw new GradleException(cant get git rev, you should add git to system path or just input test value, such as testTinkerId) return gitRev catch (Exception e) throw new GradleException(cant get git rev, you should add git to system path or just input test value, such as testTinkerId) /自定义重命名apk包和R.txt以及mapping.txt的存放目录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 /这个是我自定义打包apk的名字的格式化字符串(关于自定义输出apk文件的名字那些,参考后面的bak apk and /mapping注释那里),这里找到上次编译的apk、mapping、R文件,用来和本次新编译的文件做diff生成补丁包 def oldPrefixFormat=app_beta1_$appVersionName_$new Date().format(yyyy_MM_dd)_%s_build$appVersionCode_%s def oldApkPath=String.format(oldPrefixFormat,10_34_38,tinker.apk) def applyMappingPath=String.format(oldPrefixFormat,10_34_38,mapping.txt) def resourcePath=String.format(oldPrefixFormat,10_34_38,R.txt) /for some reason, you may want to ignore tinkerBuild, such as instant run debug build? tinkerEnabled = true /for normal build /old apk file to build patch apk tinkerOldApkPath = $bakPath/$oldApkPath /proguard mapping file to build patch apk tinkerApplyMappingPath = $bakPath/$applyMappingPath /resource R.txt to build patch apk, must input if there is resource changed tinkerApplyResourcePath = $bakPath/$resourcePath /only use for build all flavor, if not, just ignore this field /这个是你自定义的差分包相关文件的输出路径 tinkerBuildFlavorDirectory = $bakPath/app-patch-$releaseTime()/下面这些是一些自
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江西省“三新”协同教研共同体2024-2025学年高一上学期12月月考历史试卷(含答案)
- 联合体投标协议书
- 河北石家庄市正中实验中学2024-2025学年高一上学期第一次月考思想政治试卷(含答案)
- 学校运动会部署会上校长讲话-:抓实筹备每一环保障安全每一步成就健康每一人
- 2025秋第二次教学工作推进会上,校长讲话:提质增效,要真抓实做-教学推进会上校长六项发言要点
- 应城交警安全培训中心课件
- 巡察谈话课件
- 岩石变化课件
- 尾矿库安全检查培训课件
- 输液港与PICC的区别
- 五年级上册数学课件-2.1 轴对称 ︳青岛版 (共17张PPT)
- GJB9001C-2017质量管理体系检查内容的内部审核检查表【含检查内容】
- 半导体数字集成电路测试技术概要
- 心包积液以及心包填塞
- 商业银行内部审计技术与方法
- 河道清淤整治工程施工组织设计方案
- 论信息技术对公共行政的影响分析研究行政管理专业
- 蒂斯——动态能力与战略管理中译版
- 技术部薪资等级晋升制度76799
- 生物化学:第2章 核酸的结构与功能
- 湖南省住院病案首页
评论
0/150
提交评论