diff --git a/README.md b/README.md index 70098d0..c9eb408 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ plugins { lsparanoid { seed = null - global = false + classFilter = null includeDependencies = false variantFilter = { true } } @@ -51,19 +51,19 @@ lsparanoid { The extension object contains the following properties: - `seed` - `Integer`. A seed that can be used to make obfuscation stable across builds. Default value is `null`. Set it to non-null can make the obfuscation task cacheable. -- `global` - `boolean`. If `true`, the obfuscation will be applied to all classes, not only annotated ones. Default value is `false`. +- `classFilter` - `(String) -> boolean`. If set, it allows to filter out classes that should be obfuscated. Use `classFilter = { true }` to turn on global obfuscation i.e. obfuscate all classes, not only annotated ones. Or apply a filter like `classFilter = { it.startsWith("com.example.") }` or `classFilter = { it != "module-info" }`. Default value is `null`. - `includeDependencies` - `boolean`. If `true`, the obfuscation will be applied to all dependencies. Default value is `false`. -- `variantFilter` - `(Variant) -> boolean`. Allows to filter out variants that should be obfuscated. Default value always returns `true`. Note that you can set `seed`, `global` and `includeDependencies` dynamically for each variant in `variantFilter`. For example +- `variantFilter` - `(Variant) -> boolean`. Allows to filter out variants that should be obfuscated. Default value always returns `true`. Note that you can set `seed`, `classFilter` and `includeDependencies` dynamically for each variant in `variantFilter`. For example ```kotlin variantFilter = { variant -> // enable global obfuscate for globalObfuscate flavor release build if (variant.flavorName == "globalObfuscate" && variant.buildType == "release") { seed = 114514 - global = true + classFilter = { true } true } else if (variant.buildType == "release") { seed = 1919810 - global = false + classFilter = null true } else { false diff --git a/build.gradle.kts b/build.gradle.kts index de87d2e..d2b4c98 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ plugins { allprojects { group = "org.lsposed.lsparanoid" - version = "0.5.2" + version = "0.6.0" plugins.withType(JavaPlugin::class.java) { extensions.configure(JavaPluginExtension::class.java) { diff --git a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidExtension.kt b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidExtension.kt index 3cab563..42e9ac4 100644 --- a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidExtension.kt +++ b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidExtension.kt @@ -21,7 +21,7 @@ import com.android.build.api.variant.Variant open class LSParanoidExtension { var seed: Int? = null - var global: Boolean = false + var classFilter: ((className: String) -> Boolean)? = null var includeDependencies: Boolean = false var variantFilter: (Variant) -> Boolean = { true } } diff --git a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidPlugin.kt b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidPlugin.kt index b03f1e3..09feb8a 100644 --- a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidPlugin.kt +++ b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidPlugin.kt @@ -45,7 +45,7 @@ class LSParanoidPlugin : Plugin { it.bootClasspath.set(components.sdkComponents.bootClasspath) it.classpath = variant.compileClasspath it.seed.set(extension.seed ?: SecureRandom().nextInt()) - it.global.set(extension.global) + it.classFilter = extension.classFilter it.projectName.set("${project.rootProject.name}\$${project.name}") } variant.artifacts.forScope(if (extension.includeDependencies) Scope.ALL else Scope.PROJECT).use(task) diff --git a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidTask.kt b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidTask.kt index 0c16581..c390015 100644 --- a/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidTask.kt +++ b/gradle-plugin/src/main/java/org/lsposed/lsparanoid/plugin/LSParanoidTask.kt @@ -53,7 +53,8 @@ abstract class LSParanoidTask : DefaultTask() { abstract val seed: Property @get:Input - abstract val global: Property + @get:Optional + abstract var classFilter: ((className: String) -> Boolean)? @get:Input abstract val projectName: Property @@ -75,7 +76,7 @@ abstract class LSParanoidTask : DefaultTask() { .toSet() + classpath.files.map { it.toPath() }, output = jarOutput, projectName = projectName.get(), - global = global.get(), + classFilter = classFilter ).process() } } diff --git a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Analyzer.kt b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Analyzer.kt index 18c9e2b..7e34645 100644 --- a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Analyzer.kt +++ b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/Analyzer.kt @@ -29,7 +29,7 @@ import com.joom.grip.mirrors.Type import com.joom.grip.withFieldInitializer import java.nio.file.Path -class Analyzer(private val grip: Grip, private val global: Boolean) { +class Analyzer(private val grip: Grip, private val classFilter: ((className: String) -> Boolean)?) { fun analyze(inputs: List): AnalysisResult { val typesToObfuscate = findTypesToObfuscate(inputs) val obfuscationConfigurationsByType = typesToObfuscate.associateBy( @@ -41,7 +41,7 @@ class Analyzer(private val grip: Grip, private val global: Boolean) { private fun findTypesToObfuscate(inputs: List): Set { val registry = newObfuscatedTypeRegistry(grip.classRegistry).withCache() - val query = grip select classes from inputs where registry.shouldObfuscate(global) + val query = grip select classes from inputs where registry.shouldObfuscate(classFilter) return query.execute().types.toHashSet() } diff --git a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ObfuscatedTypeRegistryExtensions.kt b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ObfuscatedTypeRegistryExtensions.kt index 40aaad4..840c8a8 100644 --- a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ObfuscatedTypeRegistryExtensions.kt +++ b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ObfuscatedTypeRegistryExtensions.kt @@ -31,10 +31,10 @@ fun ObfuscatedTypeRegistry.withCache(): ObfuscatedTypeRegistry { return this as? CachedObfuscatedTypeRegistry ?: CachedObfuscatedTypeRegistry(this) } -fun ObfuscatedTypeRegistry.shouldObfuscate(global: Boolean): (Grip, Typed) -> Boolean { +fun ObfuscatedTypeRegistry.shouldObfuscate(classFilter: ((className: String) -> Boolean)?): (Grip, Typed) -> Boolean { return objectType { grip, type -> grip.fileRegistry.findPathForType(type) != null && !type.className.startsWith("org.lsposed.lsparanoid.Deobfuscator") && - (global || shouldObfuscate(type)) + ((classFilter?.invoke(type.className) ?: false) || shouldObfuscate(type)) } } diff --git a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ParanoidProcessor.kt b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ParanoidProcessor.kt index be8c6f8..b0ed272 100644 --- a/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ParanoidProcessor.kt +++ b/processor/src/main/kotlin/org/lsposed/lsparanoid/processor/ParanoidProcessor.kt @@ -38,7 +38,7 @@ class ParanoidProcessor( private val output: JarOutputStream, private val asmApi: Int = Opcodes.ASM9, private val projectName: String, - private val global: Boolean + private val classFilter: ((className: String) -> Boolean)? ) { private val logger = getLogger() @@ -49,7 +49,7 @@ class ParanoidProcessor( fun process() { dumpConfiguration() - val analysisResult = Analyzer(grip, global).analyze(inputs) + val analysisResult = Analyzer(grip, classFilter).analyze(inputs) analysisResult.dump() val deobfuscator = createDeobfuscator() diff --git a/samples/application-global-obfuscate/build.gradle.kts b/samples/application-global-obfuscate/build.gradle.kts index 2d25176..6eea3a6 100644 --- a/samples/application-global-obfuscate/build.gradle.kts +++ b/samples/application-global-obfuscate/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } lsparanoid { - global = true + classFilter = { true } includeDependencies = true variantFilter = { variant -> variant.name == "release" } } diff --git a/samples/build.gradle.kts b/samples/build.gradle.kts index 2f7cc94..abf6058 100644 --- a/samples/build.gradle.kts +++ b/samples/build.gradle.kts @@ -1,5 +1,5 @@ plugins { id("com.android.application") version "7.4.2" apply false id("com.android.library") version "7.4.2" apply false - id("org.lsposed.lsparanoid") version "0.5.2" apply false + id("org.lsposed.lsparanoid") version "0.6.0" apply false } diff --git a/samples/library-obfuscate/build.gradle.kts b/samples/library-obfuscate/build.gradle.kts index 77a6739..0614a00 100644 --- a/samples/library-obfuscate/build.gradle.kts +++ b/samples/library-obfuscate/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } lsparanoid { - global = true + classFilter = { true } } android {