Skip to content

Commit

Permalink
Replace 'global' with 'classFilter' callback (#26)
Browse files Browse the repository at this point in the history
* Replace 'global' with 'classFilter' callback

Resolves #20

* Remove `global` property, bump version to 0.6.0
  • Loading branch information
mirfatif authored Oct 24, 2023
1 parent ad8ae05 commit fe042e7
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ plugins {

lsparanoid {
seed = null
global = false
classFilter = null
includeDependencies = false
variantFilter = { true }
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LSParanoidPlugin : Plugin<Project> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ abstract class LSParanoidTask : DefaultTask() {
abstract val seed: Property<Int>

@get:Input
abstract val global: Property<Boolean>
@get:Optional
abstract var classFilter: ((className: String) -> Boolean)?

@get:Input
abstract val projectName: Property<String>
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>): AnalysisResult {
val typesToObfuscate = findTypesToObfuscate(inputs)
val obfuscationConfigurationsByType = typesToObfuscate.associateBy(
Expand All @@ -41,7 +41,7 @@ class Analyzer(private val grip: Grip, private val global: Boolean) {

private fun findTypesToObfuscate(inputs: List<Path>): Set<Type.Object> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ fun ObfuscatedTypeRegistry.withCache(): ObfuscatedTypeRegistry {
return this as? CachedObfuscatedTypeRegistry ?: CachedObfuscatedTypeRegistry(this)
}

fun ObfuscatedTypeRegistry.shouldObfuscate(global: Boolean): (Grip, Typed<Type.Object>) -> Boolean {
fun ObfuscatedTypeRegistry.shouldObfuscate(classFilter: ((className: String) -> Boolean)?): (Grip, Typed<Type.Object>) -> 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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion samples/application-global-obfuscate/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

lsparanoid {
global = true
classFilter = { true }
includeDependencies = true
variantFilter = { variant -> variant.name == "release" }
}
Expand Down
2 changes: 1 addition & 1 deletion samples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion samples/library-obfuscate/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

lsparanoid {
global = true
classFilter = { true }
}

android {
Expand Down

0 comments on commit fe042e7

Please sign in to comment.