Skip to content

Commit

Permalink
publish 0.1.5 version
Browse files Browse the repository at this point in the history
- Compatible with `AGP-4.0.0-alpha09`
- Add `enableObfuscate` for plugin extension.
  • Loading branch information
JingYeoh committed Apr 13, 2020
1 parent 82dda06 commit 021bdbc
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public static Builder builder() {

public static ObfuscateBundleCommand fromFlags(ParsedFlags flags) throws DocumentException {
Builder builder = builder();
builder.setEnableObfuscate(true);
builder.setBundlePath(BUNDLE_LOCATION_FLAG.getRequiredValue(flags));
// config
Path path = CONFIG_FLAG.getRequiredValue(flags);
Expand Down Expand Up @@ -208,12 +209,14 @@ public Path execute() throws IOException, InterruptedException {
appBundle = merger.merge();
}
// obfuscate bundle
Path mappingPath = null;
if (getMappingPath().isPresent()) {
mappingPath = getMappingPath().get();
if (getEnableObfuscate()) {
Path mappingPath = null;
if (getMappingPath().isPresent()) {
mappingPath = getMappingPath().get();
}
ResourcesObfuscator obfuscator = new ResourcesObfuscator(getBundlePath(), appBundle, getWhiteList(), getOutputPath().getParent(), mappingPath);
appBundle = obfuscator.obfuscate();
}
ResourcesObfuscator obfuscator = new ResourcesObfuscator(getBundlePath(), appBundle, getWhiteList(), getOutputPath().getParent(), mappingPath);
appBundle = obfuscator.obfuscate();
// package bundle
AppBundlePackager packager = new AppBundlePackager(appBundle, getOutputPath());
packager.execute();
Expand Down Expand Up @@ -243,6 +246,8 @@ storeFile, getStorePassword().get(), getKeyAlias().get(), getKeyPassword().get()
return getOutputPath();
}

public abstract Boolean getEnableObfuscate();

public abstract Path getBundlePath();

public abstract Path getOutputPath();
Expand Down Expand Up @@ -276,6 +281,8 @@ storeFile, getStorePassword().get(), getKeyAlias().get(), getKeyPassword().get()

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setEnableObfuscate(Boolean enable);

public abstract Builder setBundlePath(Path bundlePath);

public abstract Builder setOutputPath(Path outputPath);
Expand Down
1 change: 1 addition & 0 deletions gradle/aabresguard.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ if (!"true".equalsIgnoreCase(System.getProperty("enableAabResGuardPlugin", "fals

apply plugin: "com.bytedance.android.aabResGuard"
aabResGuard {
enableObfuscate = false
// mappingFile = file("../mapping.txt").toPath()
whiteList = [
"*.R.raw.*",
Expand Down
6 changes: 3 additions & 3 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def versions = [:]
versions.agp = "3.2.0"
versions.kotlin = "1.3.0"
//versions.agp = "4.0.0-alpha08"
//versions.kotlin = "1.3.61"
versions.agp = "4.0.0-alpha09"
versions.kotlin = "1.3.61"
versions.java = "8"
versions.aabresguard = "0.1.4"
versions.aabresguard = "0.1.5"
// android
versions.compileSdkVersion = 28
versions.minSdkVersion = 15
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ distributionBase=GRADLE_USER_HOME
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.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
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-rc-1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.nio.file.Path
* Email: yangjing.yeoh@bytedance.com
*/
open class AabResGuardExtension {
var enableObfuscate: Boolean = true
var mappingFile: Path? = null
var whiteList: Set<String>? = HashSet()
lateinit var obfuscatedBundleFileName: String
Expand All @@ -19,6 +20,7 @@ open class AabResGuardExtension {

override fun toString(): String {
return "AabResGuardExtension\n" +
"\tenableObfuscate=$enableObfuscate" +
"\tmappingFile=$mappingFile" +
"\twhiteList=${if (whiteList == null) null else whiteList}\n" +
"\tobfuscatedBundleFileName=$obfuscatedBundleFileName\n" +
Expand All @@ -27,6 +29,6 @@ open class AabResGuardExtension {
"\tfilterList=${if (filterList == null) null else filterList}" +
"\tenableFilterStrings=$enableFilterStrings\n" +
"\tunusedStringPath=$unusedStringPath\n" +
"\tlanguageWhiteList=${if (languageWhiteList == null) null else languageWhiteList}"
"\tlanguageWhiteoolean`List=${if (languageWhiteList == null) null else languageWhiteList}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bytedance.android.plugin.internal

import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.initialization.dsl.ScriptHandler
import org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier

/**
* Created by YangJing on 2020/04/13 .
* Email: yangjing.yeoh@bytedance.com
*/
internal fun getAGPVersion(project: Project): String {
var agpVersion: String? = null
for (artifact in project.rootProject.buildscript.configurations.getByName(ScriptHandler.CLASSPATH_CONFIGURATION)
.resolvedConfiguration.resolvedArtifacts) {
val identifier = artifact.id.componentIdentifier
if (identifier is DefaultModuleComponentIdentifier) {
if (identifier.group == "com.android.tools.build") {
if (identifier.module == "gradle") {
agpVersion = identifier.version
}
}
}
}
if (agpVersion == null) {
throw GradleException("get AGP version failed")
}
return agpVersion
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,3 @@ private fun getVariantManagerFromAppPlugin(appPlugin: Any?): VariantManager? {
null
}
}

internal fun getAGPVersion(project: Project): String {
var agpVersion: String? = null
for (artifact in project.rootProject.buildscript.configurations.getByName(ScriptHandler.CLASSPATH_CONFIGURATION)
.resolvedConfiguration.resolvedArtifacts) {
val identifier = artifact.id.componentIdentifier
if (identifier is DefaultModuleComponentIdentifier) {
if (identifier.group == "com.android.tools.build") {
if (identifier.module == "gradle") {
agpVersion = identifier.version
}
}
}
}
if (agpVersion == null) {
throw GradleException("get AGP version failed")
}
return agpVersion
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.bytedance.android.plugin.internal

import com.android.build.gradle.internal.VariantManager
import com.android.build.gradle.internal.scope.VariantScope
import com.android.build.gradle.internal.variant.VariantInputModel
import com.bytedance.android.plugin.model.SigningConfig
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.internal.impldep.org.eclipse.jgit.errors.NotSupportedException
import java.io.File

/**
Expand All @@ -22,7 +25,7 @@ internal fun getSigningConfig(project: Project, variantScope: VariantScope): Sig
// VariantManager add getBuildTypes method
// Use BuildType.getSigningConfig method to get signingConfig
else -> {
getSigningConfigForAGP4(project, variantScope)
getSigningConfigForAGP4(agpVersion, project, variantScope)
}
}
}
Expand All @@ -34,9 +37,15 @@ private fun getSigningConfigForAGP3(project: Project, variantScope: VariantScope
return invokeSigningConfig(signingConfig)
}

private fun getSigningConfigForAGP4(project: Project, variantScope: VariantScope): SigningConfig {
private fun getSigningConfigForAGP4(agpVersion: String, project: Project, variantScope: VariantScope): SigningConfig {
val variantManager = getVariantManager(project)
val buildTypes = variantManager::class.java.getMethod("getBuildTypes").invoke(variantManager) as Map<*, *>
var buildTypes = getBuildTypesForAGPBefore4008(variantManager)
if (buildTypes == null) {
buildTypes = getBuildTypesForAGP4009(variantManager)
}
if (buildTypes == null) {
throw NotSupportedException("AGP $agpVersion is not supported, please Please ask for an issue or pull request.")
}
val flavor = variantScope.variantData.name
val buildTypeData = buildTypes[variantScope.variantData.name]
?: throw GradleException("get buildType failed for $flavor")
Expand All @@ -45,6 +54,34 @@ private fun getSigningConfigForAGP4(project: Project, variantScope: VariantScope
return invokeSigningConfig(signingConfig)
}

/**
* Return SigningConfig.
* Range: 4.* to 4.0.0-alpha08
*/
private fun getBuildTypesForAGPBefore4008(variantManager: VariantManager): Map<*, *>? {
return try {
variantManager::class.java.getMethod("getBuildTypes").invoke(variantManager) as Map<*, *>
} catch (e: Exception) {
return null
}
}

/**
* Return SigningConfig.
* Range: 4.0.0-alpha09 and after all.
*/
private fun getBuildTypesForAGP4009(variantManager: VariantManager): Map<*, *>? {
return try {
val variantInputModelField = variantManager::class.java.getDeclaredField("variantInputModel")
variantInputModelField.isAccessible = true
val variantInputModel = variantInputModelField.get(variantManager) as VariantInputModel
variantInputModel.buildTypes
} catch (e: Exception) {
null
}
}


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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ open class AabResGuardTask : DefaultTask() {
prepareUnusedFile()

val command = ObfuscateBundleCommand.builder()
.setEnableObfuscate(aabResGuard.enableObfuscate)
.setBundlePath(bundlePath)
.setOutputPath(obfuscatedBundlePath)
.setMergeDuplicatedResources(aabResGuard.mergeDuplicatedRes)
Expand Down
5 changes: 5 additions & 0 deletions samples/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

bundle {
Expand Down
35 changes: 35 additions & 0 deletions samples/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,38 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-dontwarn java.awt.**
-flattenpackagehierarchy
-allowaccessmodification
-keepattributes Exceptions,InnerClasses,Signature,SourceFile,LineNumberTable
-dontskipnonpubliclibraryclassmembers
-ignorewarnings
#kotlin
-keep class kotlin.** { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
-keepclassmembers class **$WhenMappings {
<fields>;
}
-keepclassmembers class kotlin.Metadata {
public <methods>;
}
-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keep class **.R$* {*;}
-keepclassmembers enum * { *;}

#-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
-dontwarn com.bumptech.glide.**
4 changes: 4 additions & 0 deletions wiki/en/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Change log

## 0.1.5(2020/4/5)
- Compatible with `AGP-4.0.0-alpha09`
- Add `enableObfuscate` for plugin extension.

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

Expand Down
4 changes: 4 additions & 0 deletions wiki/zh-cn/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# 版本日志

## 0.1.5(2020/4/5)
- 适配 `AGP-4.0.0-alpha09`
- 给插件添加 `enableObfuscate` 参数

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

Expand Down

0 comments on commit 021bdbc

Please sign in to comment.