Skip to content

Commit

Permalink
Merge pull request #15 from bytedance/bugfix/agp/352
Browse files Browse the repository at this point in the history
Bugfix/agp/352
  • Loading branch information
JingYeoh committed Jan 9, 2020
2 parents cfc6ed6 + 88e8700 commit 0814597
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@
<p align="center" style="font-size: 0.3em">The tool of obfuscated aab resources</p>
</h1>

[ ![Download](https://api.bintray.com/packages/yeoh/maven/aabresguard-plugin/images/download.svg) ](https://bintray.com/yeoh/maven/aabresguard-plugin/_latestVersion)
[ ![Download](https://api.bintray.com/packages/yeoh/maven/aabresguard-plugin/images/download.svg?version=0.1.3) ](https://bintray.com/yeoh/maven/aabresguard-plugin/0.1.3/link)
[![License](https://img.shields.io/badge/license-Apache2.0-brightgreen)](LICENSE)
[![Bundletool](https://img.shields.io/badge/Dependency-Bundletool/0.10.0-blue)](https://github.com/google/bundletool)

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -21,7 +21,7 @@ buildscript {
classpath deps.plugin.digital
classpath deps.plugin.shadow
classpath deps.plugin['bintray-plugin']
classpath deps.gradle.tools
classpath deps.gradle.agp
classpath deps.kotlin.plugin

if ("true".equalsIgnoreCase(System.getProperty("enableAabResGuardPlugin", "false"))) {
Expand Down
4 changes: 2 additions & 2 deletions core/build.gradle
Expand Up @@ -30,7 +30,7 @@ dependencies {

compileOnly "com.android.tools.build:aapt2-proto:0.4.0"
compileOnly gradleApi()
// compile deps.gradle.tools
// compile deps.gradle.agp

implementation "com.android.support:support-annotations:24.2.0"
shadow 'commons-io:commons-io:2.6'
Expand All @@ -42,7 +42,7 @@ dependencies {
shadow group: 'org.dom4j', name: 'dom4j', version: '2.1.0'
shadow deps.bundletool

testImplementation deps.gradle.tools
testImplementation deps.gradle.agp
testImplementation "com.android.tools.build:aapt2-proto:0.4.0"
testImplementation group: 'org.dom4j', name: 'dom4j', version: '2.1.0'
testImplementation "junit:junit:4.12"
Expand Down
2 changes: 1 addition & 1 deletion gradle/aabresguard.gradle
Expand Up @@ -4,7 +4,7 @@ if (!"true".equalsIgnoreCase(System.getProperty("enableAabResGuardPlugin", "fals

apply plugin: "com.bytedance.android.aabResGuard"
aabResGuard {
mappingFile = file("../mapping.txt").toPath()
// mappingFile = file("../mapping.txt").toPath()
whiteList = [
"*.R.raw.*",
"*.R.drawable.icon"
Expand Down
8 changes: 4 additions & 4 deletions gradle/versions.gradle
@@ -1,10 +1,10 @@
def versions = [:]
versions.gradletools = "3.2.1"
versions.agp = "3.2.0"
versions.kotlin = "1.3.0"
//versions.gradletools = "4.0.0-alpha07"
//versions.agp = "4.0.0-alpha08"
//versions.kotlin = "1.3.61"
versions.java = "8"
versions.aabresguard = "0.1.2"
versions.aabresguard = "0.1.3"
// android
versions.compileSdkVersion = 28
versions.minSdkVersion = 15
Expand All @@ -26,7 +26,7 @@ ext.versions = versions
ext.deps = [:]
// gradle
def gradle = [:]
gradle.tools = "com.android.tools.build:gradle:${versions.gradletools}"
gradle.agp = "com.android.tools.build:gradle:${versions.agp}"
deps.gradle = gradle
// kotlin
def kotlin = [:]
Expand Down
2 changes: 2 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -4,4 +4,6 @@ distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
#distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
#distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-milestone-2-all.zip
#distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-rc-1-all.zip
2 changes: 1 addition & 1 deletion plugin/build.gradle
Expand Up @@ -7,7 +7,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation gradleApi()
implementation localGroovy()
compileOnly deps.gradle.tools
compileOnly deps.gradle.agp

implementation deps.kotlin.stdlib
implementation deps.kotlin.plugin
Expand Down
Expand Up @@ -7,7 +7,7 @@ import java.nio.file.Path
* Email: yangjing.yeoh@bytedance.com
*/
open class AabResGuardExtension {
lateinit var mappingFile: Path
var mappingFile: Path? = null
var whiteList: Set<String>? = HashSet()
lateinit var obfuscatedBundleFileName: String
var mergeDuplicatedRes: Boolean = false
Expand Down
Expand Up @@ -12,23 +12,41 @@ import java.nio.file.Path
internal fun getBundleFilePath(project: Project, variantScope: VariantScope): Path {
val agpVersion = getAGPVersion(project)
val flavor = variantScope.variantData.name
return if (agpVersion.startsWith("3.")) {
getBundleFileForAGP3(project, flavor).toPath()
} else {
getBundleFileForAGP4(project, flavor).toPath()
return when {
// AGP3.2.0 - 3.2.1: packageBundle task class is com.android.build.gradle.internal.tasks.BundleTask
// AGP3.3.0 - 3.3.2: packageBundle task class is com.android.build.gradle.internal.tasks.PackageBundleTask
agpVersion.startsWith("3.2") || agpVersion.startsWith("3.3") -> {
getBundleFileForAGP32To33(project, flavor).toPath()
}
// AGP3.4.0+: use FinalizeBundleTask sign bundle file
// packageBundle task bundleLocation is intermediates dir
// The finalize bundle file path: FinalizeBundleTask.finalBundleLocation
agpVersion.startsWith("3.4") || agpVersion.startsWith("3.5") -> {
getBundleFileForAGP34To35(project, flavor).toPath()
}
// AGP4.0+: removed finalBundleLocation field, and finalBundleFile is public field
else -> {
getBundleFileForAGP40After(project, flavor).toPath()
}
}
}

fun getBundleFileForAGP3(project: Project, flavor: String): File {
// AGP-3.2.1: package{}Bundle task is com.android.build.gradle.internal.tasks.BundleTask
// AGP-3.4.1: package{}Bundle task is com.android.build.gradle.internal.tasks.PackageBundleTask
fun getBundleFileForAGP32To33(project: Project, flavor: String): File {
val bundleTaskName = "package${flavor.capitalize()}Bundle"
val bundleTask = project.tasks.getByName(bundleTaskName)
return File(bundleTask.property("bundleLocation") as File, bundleTask.property("fileName") as String)
}

fun getBundleFileForAGP4(project: Project, flavor: String): File {
// AGP-4.0.0-alpha07: use FinalizeBundleTask to sign bundle file
fun getBundleFileForAGP34To35(project: Project, flavor: String): File {
// use FinalizeBundleTask to sign bundle file
val finalizeBundleTask = project.tasks.getByName("sign${flavor.capitalize()}Bundle")
// FinalizeBundleTask.finalBundleFile is the final bundle path
val location = finalizeBundleTask.property("finalBundleLocation") as File
return File(location, finalizeBundleTask.property("finalBundleFileName") as String)
}

fun getBundleFileForAGP40After(project: Project, flavor: String): File {
// use FinalizeBundleTask to sign bundle file
val finalizeBundleTask = project.tasks.getByName("sign${flavor.capitalize()}Bundle")
// FinalizeBundleTask.finalBundleFile is the final bundle path
val bundleFile = finalizeBundleTask.property("finalBundleFile")
Expand Down
Expand Up @@ -13,7 +13,7 @@ import org.gradle.internal.component.external.model.DefaultModuleComponentIdenti
*/
internal fun getVariantManager(project: Project): VariantManager {
val appPlugin: Plugin<Any>? = when {
// AGP-4.0.0-alpha07: move all methods to com.android.internal.application
// AGP4.0.0-alpha07: move all methods to com.android.internal.application
project.plugins.hasPlugin("com.android.internal.application") -> {
project.plugins.getPlugin("com.android.internal.application")
}
Expand Down
Expand Up @@ -12,13 +12,28 @@ import java.io.File
*/
internal fun getSigningConfig(project: Project, variantScope: VariantScope): SigningConfig {
val agpVersion = getAGPVersion(project)
return if (agpVersion.startsWith("3.")) {
getSigningConfigForAGP3(project, variantScope)
} else {
getSigningConfigForAGP4(project, variantScope)
// get signing config
return when {
// AGP3.2+: use VariantScope.getVariantConfiguration.getSigningConfig
agpVersion.startsWith("3.") -> {
getSigningConfigForAGP3(project, variantScope)
}
// AGP4.0+: VariantScope class removed getVariantConfiguration method.
// VariantManager add getBuildTypes method
// Use BuildType.getSigningConfig method to get signingConfig
else -> {
getSigningConfigForAGP4(project, variantScope)
}
}
}

private fun getSigningConfigForAGP3(project: Project, variantScope: VariantScope): SigningConfig {
val variantData = variantScope.variantData
val variantConfiguration = variantData::class.java.getMethod("getVariantConfiguration").invoke(variantData)
val signingConfig = variantConfiguration::class.java.getMethod("getSigningConfig").invoke(variantConfiguration)
return invokeSigningConfig(signingConfig)
}

private fun getSigningConfigForAGP4(project: Project, variantScope: VariantScope): SigningConfig {
val variantManager = getVariantManager(project)
val buildTypes = variantManager::class.java.getMethod("getBuildTypes").invoke(variantManager) as Map<*, *>
Expand All @@ -30,13 +45,6 @@ private fun getSigningConfigForAGP4(project: Project, variantScope: VariantScope
return invokeSigningConfig(signingConfig)
}

private fun getSigningConfigForAGP3(project: Project, variantScope: VariantScope): SigningConfig {
val variantData = variantScope.variantData
val variantConfiguration = variantData::class.java.getMethod("getVariantConfiguration").invoke(variantData)
val signingConfig = variantConfiguration::class.java.getMethod("getSigningConfig").invoke(variantConfiguration)
return invokeSigningConfig(signingConfig)
}

private fun invokeSigningConfig(any: Any): SigningConfig {
val storeFile: File = any::class.java.getMethod("getStoreFile").invoke(any) as File
val keyAlias: String = any::class.java.getMethod("getKeyAlias").invoke(any) as String
Expand Down
Expand Up @@ -52,14 +52,16 @@ open class AabResGuardTask : DefaultTask() {
val command = ObfuscateBundleCommand.builder()
.setBundlePath(bundlePath)
.setOutputPath(obfuscatedBundlePath)
.setMappingPath(aabResGuard.mappingFile)
.setMergeDuplicatedResources(aabResGuard.mergeDuplicatedRes)
.setWhiteList(aabResGuard.whiteList)
.setFilterFile(aabResGuard.enableFilterFiles)
.setFileFilterRules(aabResGuard.filterList)
.setRemoveStr(aabResGuard.enableFilterStrings)
.setUnusedStrPath(aabResGuard.unusedStringPath)
.setLanguageWhiteList(aabResGuard.languageWhiteList)
if (aabResGuard.mappingFile != null) {
command.setMappingPath(aabResGuard.mappingFile)
}

if (signingConfig.storeFile != null && signingConfig.storeFile!!.exists()) {
command.setStoreFile(signingConfig.storeFile!!.toPath())
Expand Down
28 changes: 28 additions & 0 deletions script/publish.sh
@@ -0,0 +1,28 @@
#!/bin/bash

function showHelp() {
echo "publishToMavenLocal: ./publish.sh l"
echo "publish: ./publish.sh m"
# publish to JCenter
echo "publish: ./publish.sh j"
}

if [ -z $1 ];then
showHelp
exit -1
fi

function publishMaven(){
./gradlew clean :core:$1 :plugin:$1 --no-daemon --stacktrace
}

if [[ $1 == 'l' ]];then
publishMaven publishToMavenLocal
elif [[ $1 == 'm' ]];then
publishMaven publish
elif [[ $1 == 'j' ]];then
publishMaven bintrayUpload
else
showHelp
exit -1
fi
3 changes: 3 additions & 0 deletions wiki/en/CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

# Change log

## 0.1.3(2020/1/8)
- Compatible with `AGP-3.5.2`

## 0.1.2(2020/1/7)
- Compatible with `AGP-4.0.0-alpha07`
- Fix issue [#13](https://github.com/bytedance/AabResGuard/issues/13)
Expand Down
3 changes: 3 additions & 0 deletions wiki/zh-cn/CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

# 版本日志

## 0.1.3(2020/1/8)
- 适配 `AGP-3.5.2`

## 0.1.2(2020/1/7)
- 适配 `AGP-4.0.0-alpha07`
- Fix issue [#13](https://github.com/bytedance/AabResGuard/issues/13)
Expand Down
2 changes: 1 addition & 1 deletion wiki/zh-cn/README.md
Expand Up @@ -4,7 +4,7 @@
<p align="center" style="font-size: 0.3em">针对 aab 文件的资源混淆工具</p>
</h1>

[ ![Download](https://api.bintray.com/packages/yeoh/maven/aabresguard-plugin/images/download.svg) ](https://bintray.com/yeoh/maven/aabresguard-plugin/_latestVersion)
[ ![Download](https://api.bintray.com/packages/yeoh/maven/aabresguard-plugin/images/download.svg?version=0.1.3) ](https://bintray.com/yeoh/maven/aabresguard-plugin/0.1.3/link)
[![License](https://img.shields.io/badge/license-Apache2.0-brightgreen)](../../LICENSE)
[![Bundletool](https://img.shields.io/badge/Dependency-Bundletool/0.10.0-blue)](https://github.com/google/bundletool)

Expand Down

0 comments on commit 0814597

Please sign in to comment.