Skip to content

Commit

Permalink
1. add samples
Browse files Browse the repository at this point in the history
2. bugfix
3. update doc
  • Loading branch information
JingYeoh committed Oct 16, 2019
1 parent 894864b commit 6e52dea
Show file tree
Hide file tree
Showing 39 changed files with 452 additions and 51 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ buildscript {
classpath deps.gradle.tools
classpath deps.kotlin.plugin
classpath deps.plugin.digital
classpath "com.bytedance.android:aabresguard-plugin:${versions.aabresguard}"
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: "com.google.protobuf"
configurations.all {
resolutionStrategy.dependencySubstitution {
resolutionStrategy {
force 'com.android.tools.build:bundletool:0.10.0'
force deps.bundletool
}
}
}
Expand All @@ -36,6 +36,7 @@ dependencies {
annotationProcessor "com.google.auto.value:auto-value:1.5.2"
implementation "com.google.auto.value:auto-value:1.5.2"
compile group: 'org.dom4j', name: 'dom4j', version: '2.1.0'
compile deps.bundletool

testImplementation "junit:junit:4.12"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.android.tools.build.bundletool.flags.ParsedFlags;
import com.android.tools.build.bundletool.model.AppBundle;
import com.android.tools.build.bundletool.model.exceptions.CommandExecutionException;
import com.google.auto.value.AutoValue;
import com.bytedance.android.aabresguard.bundle.AppBundleAnalyzer;
import com.bytedance.android.aabresguard.bundle.AppBundlePackager;
import com.bytedance.android.aabresguard.bundle.AppBundleSigner;
import com.bytedance.android.aabresguard.executors.DuplicatedResourcesMerger;
import com.bytedance.android.aabresguard.utils.FileOperation;
import com.bytedance.android.aabresguard.utils.TimeClock;
import com.google.auto.value.AutoValue;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -128,7 +128,7 @@ storeFile, getStorePassword().get(), getKeyAlias().get(), getKeyPassword().get()

long rawSize = FileOperation.getFileSizes(getBundlePath().toFile());
long filteredSize = FileOperation.getFileSizes(getOutput().toFile());
logger.info(String.format(
System.out.println(String.format(
"duplicate resources done, coast %s\n" +
"-----------------------------------------\n" +
"Reduce bundle file size: %s, %s -> %s\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ storeFile, getStorePassword().get(), getKeyAlias().get(), getKeyPassword().get()

long rawSize = FileOperation.getFileSizes(getBundlePath().toFile());
long filteredSize = FileOperation.getFileSizes(getOutputPath().toFile());
logger.info(String.format(
System.out.println(String.format(
"filter bundle files done, coast %s\n" +
"-----------------------------------------\n" +
"Reduce bundle file size: %s, %s -> %s\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ storeFile, getStorePassword().get(), getKeyAlias().get(), getKeyPassword().get()

long rawSize = FileOperation.getFileSizes(getBundlePath().toFile());
long filteredSize = FileOperation.getFileSizes(getOutputPath().toFile());
logger.info(String.format(
System.out.println(String.format(
"obfuscate resources done, coast %s\n" +
"-----------------------------------------\n" +
"Reduce bundle file size: %s, %s -> %s\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import com.android.tools.build.bundletool.model.BundleModuleName;
import com.android.tools.build.bundletool.model.ModuleEntry;
import com.android.tools.build.bundletool.model.ZipPath;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.bytedance.android.aabresguard.bundle.AppBundleUtils;
import com.bytedance.android.aabresguard.bundle.NativeLibrariesOperation;
import com.bytedance.android.aabresguard.utils.TimeClock;
import com.bytedance.android.aabresguard.utils.Utils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -93,6 +93,7 @@ public AppBundle filter() throws IOException {
}

private BundleModule filterBundleModule(BundleModule bundleModule) throws IOException {
BundleModule.Builder builder = bundleModule.toBuilder();
List<ModuleEntry> filteredModuleEntries = new ArrayList<>();
List<ModuleEntry> entries = bundleModule.getEntries().stream()
.filter(entry -> {
Expand All @@ -107,26 +108,28 @@ private BundleModule filterBundleModule(BundleModule bundleModule) throws IOExce
return true;
})
.collect(Collectors.toList());
builder.setRawEntries(entries);
filterTotalCount += filteredModuleEntries.size();
// update pb
Files.NativeLibraries nativeLibraries = updateLibDirectory(bundleModule, filteredModuleEntries);
return bundleModule.toBuilder()
.setRawEntries(entries)
.setNativeConfig(nativeLibraries)
.build();
if (nativeLibraries != null) {
builder.setNativeConfig(nativeLibraries);
}
return builder.build();
}

private Files.NativeLibraries updateLibDirectory(BundleModule bundleModule, List<ModuleEntry> entries) throws UnexpectedException {
List<ModuleEntry> libEntries = entries.stream()
.filter(entry -> entry.getPath().startsWith(BundleModule.LIB_DIRECTORY))
.collect(Collectors.toList());
Files.NativeLibraries nativeLibraries = bundleModule.getNativeConfig().orElse(null);
if (nativeLibraries == null) {
throw new UnexpectedException("can not find nativeLibraries file `native.pb`");
}
if (libEntries.isEmpty()) {
return nativeLibraries;
}
if (nativeLibraries == null) {
throw new UnexpectedException(String.format("can not find nativeLibraries file `native.pb` in %s module", bundleModule.getName().getName()));
}

Files.NativeLibraries filteredNativeLibraries = nativeLibraries;
for (Files.TargetedNativeDirectory directory : nativeLibraries.getDirectoryList()) {
int directoryNativeSize = libEntries.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
issues/dmt
issues/vigo
issues/bytedance/
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
issues/dmt
issues/vigo
issues/bytedance/
14 changes: 14 additions & 0 deletions gradle/aabresguard.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apply plugin: "com.bytedance.android.aabResGuard"
aabResGuard {
mappingFile = file("../mapping.txt").toPath()
whiteList = [
"*.R.raw.*",
"*.R.drawable.icon"
]
obfuscatedBundleFileName = "duplicated-app.aab"
mergeDuplicatedRes = true
enableFilterFiles = true
filterList = [
"*/arm64-v8a/*"
]
}
10 changes: 10 additions & 0 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ versions.kotlin = "1.3.0"
versions.digital = "3.4.0"
versions.java = "8"
versions.aabresguard = "0.1.0"
// android
versions.compileSdkVersion = 28
versions.minSdkVersion = 15
versions.targetSdkVersion = 28
versions.support = "28.0.0"

versions.bundletool = "0.10.0"

versions["aabresguard-core"] = versions.aabresguard
versions["aabresguard-plugin"] = versions.aabresguard
Expand All @@ -26,5 +33,8 @@ deps.kotlin = kotlin
def plugin = [:]
plugin.digital = "digital.wup:android-maven-publish:${versions.digital}"
deps.plugin = plugin
// library
deps.appcompatV7 = "com.android.support:appcompat-v7:${versions.support}"
deps.bundletool = "com.android.tools.build:bundletool:${versions.bundletool}"

ext.deps = deps
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ class AabResGuardPlugin : Plugin<Project> {

private fun createAabResGuardTask(project: Project, scope: VariantScope) {
val variantName = scope.variantData.name.capitalize()
val bundleTaskName = "bundle$variantName"
if (project.tasks.findByName(bundleTaskName) == null) {
return
}
val aabResGuardTaskName = "aabresguard$variantName"
val aabResGuardTask: AabResGuardTask
aabResGuardTask = if (project.tasks.findByName(aabResGuardTaskName) == null) {
project.tasks.create(aabResGuardTaskName, AabResGuardTask::class.java)
} else {
project.tasks.getByName(aabResGuardTaskName) as AabResGuardTask
}
aabResGuardTask.setVariantScope(scope)

val bundleTask: Task = project.tasks.getByName(bundleTaskName)
val bundlePackageTask: Task = project.tasks.getByName("package${variantName}Bundle")
val bundleTask: Task = project.tasks.getByName("bundle$variantName")
bundleTask.dependsOn(aabResGuardTask)
aabResGuardTask.mustRunAfter(bundlePackageTask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import java.nio.file.Path
* Created by YangJing on 2019/10/15 .
* Email: yangjing.yeoh@bytedance.com
*/
data class AabResGuardExtension(
val mappingFile: Path,
val whiteList: Set<String>,
val obfuscatedBundleFileName: String,
val mergeDuplicatedRes: Boolean,
val enableFilterFiles: Boolean,
val filterList: Set<String>
)
open class AabResGuardExtension {
lateinit var mappingFile: Path
lateinit var whiteList: Set<String>
lateinit var obfuscatedBundleFileName: String
var mergeDuplicatedRes: Boolean = false
var enableFilterFiles: Boolean = false
lateinit var filterList: Set<String>

override fun toString(): String {
return "AabResGuardExtension\n" +
"\tmappingFile=$mappingFile\n" +
"\twhiteList=$whiteList\n" +
"\tobfuscatedBundleFileName=$obfuscatedBundleFileName\n" +
"\tmergeDuplicatedRes=$mergeDuplicatedRes\n" +
"\tenableFilterFiles=$enableFilterFiles\n" +
"\tfilterList=$filterList"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ import java.nio.file.Path
* Created by YangJing on 2019/10/15 .
* Email: yangjing.yeoh@bytedance.com
*/
class AabResGuardTask(private val variantScope: VariantScope) : DefaultTask() {
open class AabResGuardTask : DefaultTask() {

private lateinit var variantScope: VariantScope
lateinit var signingConfig: CoreSigningConfig
var aabResGuard: AabResGuardExtension = project.extensions.getByName("aabResGuard") as AabResGuardExtension
private val bundlePath: Path
private val obfuscatedBundlePath: Path
private lateinit var bundlePath: Path
private lateinit var obfuscatedBundlePath: Path

init {
description = "Assemble resource proguard for bundle file"
group = "bundle"
outputs.upToDateWhen { false }
}

fun setVariantScope(variantScope: VariantScope) {
this.variantScope = variantScope
val bundlePackageTask: BundleTask = project.tasks.getByName("package${variantScope.variantData.name.capitalize()}Bundle") as BundleTask
bundlePath = File(bundlePackageTask.bundleLocation, bundlePackageTask.fileName).toPath()
obfuscatedBundlePath = File(bundlePackageTask.bundleLocation, aabResGuard.obfuscatedBundleFileName).toPath()
Expand Down Expand Up @@ -67,8 +72,8 @@ class AabResGuardTask(private val variantScope: VariantScope) : DefaultTask() {
}

private fun encrypt(value: String): String {
if (value.length > 1) {
return "${value.substring(0, value.length)}****"
if (value.length > 2) {
return "${value.substring(0, value.length / 2)}****"
}
return "****"
}
Expand Down
1 change: 1 addition & 0 deletions samples/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
42 changes: 42 additions & 0 deletions samples/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apply plugin: 'com.android.application'
apply from: "$rootDir/gradle/aabresguard.gradle"

android {
compileSdkVersion versions.compileSdkVersion

defaultConfig {
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

bundle {
language {
enableSplit = false
}
density {
enableSplit = true
}
abi {
enableSplit = true
}
}
dynamicFeatures = [":df_module1", ":df_module2"]
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation deps.appcompatV7

implementation 'com.mikepenz:ionicons-typeface:2.0.1.5-kotlin@aar'
implementation 'com.mikepenz:pixeden-7-stroke-typeface:1.2.0.3-kotlin@aar'
implementation 'com.mikepenz:material-design-icons-dx-typeface:5.0.1.0-kotlin@aar'
}
21 changes: 21 additions & 0 deletions samples/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions samples/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.bytedance.android.app">
</manifest>
3 changes: 3 additions & 0 deletions samples/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">app</string>
</resources>
1 change: 1 addition & 0 deletions samples/dynamic-features/df_module1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions samples/dynamic-features/df_module1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.dynamic-feature'

android {
compileSdkVersion versions.compileSdkVersion

defaultConfig {
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation deps.appcompatV7
implementation project(":app")

implementation 'com.mikepenz:google-material-typeface:3.0.1.4.original-kotlin@aar'
implementation 'com.mikepenz:material-design-iconic-typeface:2.2.0.6-kotlin@aar'
implementation 'com.mikepenz:fontawesome-typeface:5.9.0.0-kotlin@aar'
implementation 'com.mikepenz:octicons-typeface:3.2.0.6-kotlin@aar'
implementation 'com.mikepenz:meteocons-typeface:1.1.0.5-kotlin@aar'
implementation 'com.mikepenz:community-material-typeface:3.5.95.1-kotlin@aar'
}
21 changes: 21 additions & 0 deletions samples/dynamic-features/df_module1/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Loading

0 comments on commit 6e52dea

Please sign in to comment.