3.dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')

compile 'com.android.support:appcompat-v7:24.1.1'

// 多dex配置

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

// 集成Bugly热更新aar(灰度时使用方式)

10.// compile(name: 'bugly_crashreport_upgrade-1.2.0', ext: 'aar')

compile "com.tencent.bugly:crashreport_upgrade:1.2.0"

12.}

15.android {

compileSdkVersion 23

buildToolsVersion "23.0.2"

// 编译选项

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

// recommend

dexOptions {

jumboMode = true

}

// 签名配置

signingConfigs {

// 签名配置

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.henry.testappbugly"

minSdkVersion 14

targetSdkVersion 23

versionCode 2

versionName "2.0"

// 开启multidex

multiDexEnabled true

// 以Proguard的方式手动加入要放到Main.dex中的类

multiDexKeepProguard file("keep_in_main_dex.txt")

}

buildTypes {

release {

minifyEnabled true

signingConfig signingConfigs.release

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

debug {

debuggable true

minifyEnabled false

signingConfig signingConfigs.debug

}

}

sourceSets {

main {

jniLibs.srcDirs = ['libs']

}

}

repositories {

flatDir {

dirs 'libs'

}

}

lintOptions {

checkReleaseBuilds false

abortOnError false

}

91.}

94.def gitSha() {

try {

String gitRev = 'git rev-parse --short HEAD'.execute(null, project.rootDir).text.trim()

if (gitRev == null) {

throw new GradleException("can't 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("can't get git rev, you should add git to system path or just input test value, such as 'testTinkerId'")

}

104.}

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

109./**

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

*/

114.ext {

// 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}/app-release-1201-09-46-25.apk"

// proguard mapping file to build patch apk

tinkerApplyMappingPath = "${bakPath}/app-release-1201-09-46-25-mapping.txt"

// resource R.txt to build patch apk, must input if there is resource changed

tinkerApplyResourcePath = "${bakPath}/app-release-1201-09-46-25-R.txt"

// only use for build all flavor, if not, just ignore this field

tinkerBuildFlavorDirectory = "${bakPath}/app-release-1201-09-46-25"

128.}

130.def getOldApkPath() {

return hasProperty("OLD_APK") ? OLD_APK : ext.tinkerOldApkPath

132.}

134.def getApplyMappingPath() {

return hasProperty("APPLY_MAPPING") ? APPLY_MAPPING : ext.tinkerApplyMappingPath

136.}

138.def getApplyResourceMappingPath() {

return hasProperty("APPLY_RESOURCE") ? APPLY_RESOURCE : ext.tinkerApplyResourcePath

140.}

142.def getTinkerIdValue() {

return hasProperty("TINKER_ID") ? TINKER_ID : gitSha()

144.}

146.def buildWithTinker() {

return hasProperty("TINKER_ENABLE") ? TINKER_ENABLE : ext.tinkerEnabled

148.}

150.def getTinkerBuildFlavorDirectory() {

return ext.tinkerBuildFlavorDirectory

152.}

154./**

*/

157.if (buildWithTinker()) {

// 依赖tinker插件

apply plugin: 'com.tencent.tinker.patch'

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

tinkerSupport {

}

// 全局信息相关配置项

tinkerPatch {

oldApk = getOldApkPath() //必选, 基准包路径

ignoreWarning = false // 可选,默认false

useSign = true // 可选,默认true, 验证基准apk和patch签名是否一致

// 编译相关配置项

buildConfig {

applyMapping = getApplyMappingPath() // 可选,设置mapping文件,建议保持旧apk的proguard混淆方式

applyResourceMapping = getApplyResourceMappingPath() // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配

tinkerId = "可以是签名版本号字符串等等比如:assdhfkdshfksdhfuksfhuk" // 必选,默认为null

}

// dex相关配置项

dex {

dexMode = "jar" // 可选,默认为jar

usePreGeneratedPatchDex = true // 可选,默认为false

pattern = ["classes*.dex",

"assets/secondary-dex-?.jar"]

// 必选

loader = ["com.tencent.tinker.loader.*",

"SampleApplication所在的全路径",

]

}

// lib相关的配置项

lib {

pattern = ["lib/armeabi/*.so"]

}

// res相关的配置项

res {

pattern = ["res/", "assets/", "resources.arsc", "AndroidManifest.xml"]

ignoreChange = ["assets/sample_meta.txt"]

largeModSize = 100

}

// 用于生成补丁包中的'package_meta.txt'文件

packageConfig {

configField("patchMessage", "tinker is sample to use")

configField("platform", "all")

configField("patchVersion", "1.0")

}

// 7zip路径配置项,执行前提是useSign为true

sevenZip {

zipArtifact = "com.tencent.mm:SevenZip:1.1.10" // optional

// path = "/usr/local/bin/7za" // optional

}

}

List flavors = new ArrayList<>();

project.android.productFlavors.each { flavor ->

flavors.add(flavor.name)

}

boolean hasFlavors = flavors.size() > 0

/**

bak apk and mapping

*/

android.applicationVariants.all { variant ->

/**

task type, you want to bak

*/

def taskName = variant.name

def date = new Date().format("MMdd-HH-mm-ss")

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.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"

}

}

}

}

}

306.}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐