Skip to content

Commit 3fff44c

Browse files
committed
1.增加apk混淆功能
1 parent 4271b11 commit 3fff44c

File tree

7 files changed

+91
-68
lines changed

7 files changed

+91
-68
lines changed

common/build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ kotlin {
2424
api(compose.material)
2525
api(compose.preview)
2626
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
27+
api("net.lingala.zip4j:zip4j:2.9.1")
2728
}
2829
}
2930
val commonTest by getting {
@@ -43,7 +44,7 @@ kotlin {
4344
}
4445
}
4546
val desktopMain by getting
46-
47+
4748
val desktopTest by getting
4849
}
4950
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package cn.kaicity.common.platform
22

3-
expect fun chooseFile(): String
3+
expect fun chooseFile(callback:(String)->Unit)
44

5-
expect fun saveFile(): String
5+
expect fun saveFile(callback: (String) -> Unit)

common/src/commonMain/kotlin/cn/kaicity/common/repository/DexObf.kt

+31-44
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,48 @@ package cn.kaicity.common.repository
22

33
import cn.kaicity.common.bean.InputBean
44
import cn.kaicity.common.platform.IObfuscator
5-
import kotlinx.coroutines.DelicateCoroutinesApi
65
import kotlinx.coroutines.Dispatchers
7-
import kotlinx.coroutines.GlobalScope
8-
import kotlinx.coroutines.Job
96
import kotlinx.coroutines.flow.catch
107
import kotlinx.coroutines.flow.collect
118
import kotlinx.coroutines.flow.flow
129
import kotlinx.coroutines.flow.flowOn
1310
import kotlinx.coroutines.flow.onCompletion
14-
import kotlinx.coroutines.launch
1511
import java.io.File
16-
import kotlin.math.log
1712

1813
object DexObf {
1914

2015
private var obfuscatorImpl: IObfuscator? = null
2116

22-
private var job: Job? = null
23-
24-
@OptIn(DelicateCoroutinesApi::class)
25-
fun run(inputBean: InputBean, logCallback: (log: String) -> Unit) {
26-
job?.cancel()
27-
28-
job = GlobalScope.launch(Dispatchers.IO) {
29-
val params = arrayListOf<String>()
30-
params.add("-i")
31-
params.add(inputBean.input)
32-
33-
params.add("-o")
34-
params.add(inputBean.output)
35-
36-
params.add("-d")
37-
params.add(inputBean.depth)
38-
39-
params.add("-a")
40-
41-
val filterFile = File(File(inputBean.output).parentFile, "filter.txt")
42-
filterFile.writeText(inputBean.rule)
43-
params.add(filterFile.absolutePath)
44-
45-
46-
flow<String> { }.flowOn(Dispatchers.IO)
47-
.collect()
48-
flow {
49-
obfuscatorImpl = IObfuscator(this)
50-
obfuscatorImpl?.doObfuscator(params.toTypedArray())
51-
}.catch {
52-
logCallback.invoke("Error")
53-
}.onCompletion {
54-
filterFile.delete()
55-
logCallback.invoke("Finish")
56-
}.flowOn(Dispatchers.IO)
57-
.collect {
58-
logCallback.invoke(it)
59-
}
60-
}
17+
suspend fun run(inputBean: InputBean, logCallback: (log: String) -> Unit) {
18+
val params = arrayListOf<String>()
19+
params.add("-i")
20+
params.add(inputBean.input)
21+
22+
params.add("-o")
23+
params.add(inputBean.output)
24+
25+
params.add("-d")
26+
params.add(inputBean.depth)
27+
28+
params.add("-a")
29+
30+
val filterFile = File(File(inputBean.output).parentFile, "filter.txt")
31+
filterFile.writeText(inputBean.rule)
32+
params.add(filterFile.absolutePath)
33+
34+
35+
flow {
36+
obfuscatorImpl = IObfuscator(this)
37+
obfuscatorImpl?.doObfuscator(params.toTypedArray())
38+
}.catch {
39+
logCallback.invoke("Error")
40+
}.onCompletion {
41+
filterFile.delete()
42+
logCallback.invoke("Finish")
43+
}.flowOn(Dispatchers.IO)
44+
.collect {
45+
logCallback.invoke(it)
46+
}
6147
}
48+
6249
}

common/src/commonMain/kotlin/cn/kaicity/common/widget/MainView.kt

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import androidx.compose.material.Button
1010
import androidx.compose.material.Icon
1111
import androidx.compose.material.OutlinedTextField
1212
import androidx.compose.material.Text
13-
import androidx.compose.material.TextField
1413
import androidx.compose.material.icons.Icons
15-
import androidx.compose.material.icons.filled.Menu
1614
import androidx.compose.material.icons.filled.Search
17-
import androidx.compose.material.icons.filled.Settings
1815
import androidx.compose.runtime.Composable
1916
import androidx.compose.runtime.getValue
2017
import androidx.compose.runtime.mutableStateOf
@@ -43,6 +40,7 @@ fun MainView(modifier: Modifier, btnClick: (InputBean) -> Unit) {
4340
var depthIsError by remember { mutableStateOf(false) }
4441
var rule by remember { mutableStateOf("") }
4542

43+
4644
val textFieldModifier = Modifier.padding(12.dp).fillMaxWidth()
4745

4846

@@ -53,7 +51,9 @@ fun MainView(modifier: Modifier, btnClick: (InputBean) -> Unit) {
5351
isError = inputIsError,
5452
trailingIcon = {
5553
Icon(Icons.Default.Search, "Menu", modifier = Modifier.clickable {
56-
input = chooseFile()
54+
chooseFile {
55+
input = it
56+
}
5757
})
5858
},
5959
label = {
@@ -71,7 +71,9 @@ fun MainView(modifier: Modifier, btnClick: (InputBean) -> Unit) {
7171
Text("Output")
7272
}, trailingIcon = {
7373
Icon(Icons.Default.Search, "Menu", modifier = Modifier.clickable {
74-
output = saveFile()
74+
saveFile {
75+
output = it
76+
}
7577
})
7678
},
7779
placeholder = {
@@ -82,13 +84,17 @@ fun MainView(modifier: Modifier, btnClick: (InputBean) -> Unit) {
8284

8385
Row(modifier = Modifier.fillMaxWidth()) {
8486
Box(modifier = Modifier.weight(0.3F)) {
85-
OutlinedTextField(value = depth, modifier = textFieldModifier, isError = depthIsError,
87+
OutlinedTextField(value = depth,
88+
modifier = textFieldModifier,
89+
isError = depthIsError,
90+
singleLine = true,
8691
label = {
8792
Text("Depth")
8893
},
8994
placeholder = {
90-
Text("Obfuscator Depth")
91-
}, onValueChange = {
95+
Text("Depth")
96+
},
97+
onValueChange = {
9298
depth = it
9399
})
94100

@@ -107,6 +113,7 @@ fun MainView(modifier: Modifier, btnClick: (InputBean) -> Unit) {
107113

108114
}
109115
}
116+
110117
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
111118
Button(modifier = Modifier.padding(12.dp), onClick = {
112119
inputIsError = input.isEmpty()

common/src/desktopMain/kotlin/cn/kaicity/common/DesktopApp.kt

+33-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import androidx.compose.runtime.mutableStateOf
99
import androidx.compose.runtime.remember
1010
import androidx.compose.runtime.setValue
1111
import androidx.compose.ui.Modifier
12+
import cn.kaicity.common.bean.InputBean
13+
import cn.kaicity.common.repository.ApkObf
1214
import cn.kaicity.common.repository.DexObf
1315
import cn.kaicity.common.widget.LogView
1416
import cn.kaicity.common.widget.MainView
17+
import kotlinx.coroutines.Dispatchers
18+
import kotlinx.coroutines.GlobalScope
19+
import kotlinx.coroutines.launch
20+
import kotlin.math.log
1521

1622
@Preview
1723
@Composable
@@ -25,14 +31,37 @@ fun AppMain() {
2531
var log by remember { mutableStateOf("") }
2632

2733

34+
val logCallback = { it: String ->
35+
log = "$log\n$it"
36+
}
37+
2838
Row(modifier = Modifier.fillMaxWidth()) {
2939
MainView(modifier = Modifier.weight(0.5F)) {
30-
log = "Black Obfuscator Start\n"
31-
32-
DexObf.run(it) { l ->
33-
log = log + "\n" + l
40+
if (it.input.endsWith(".dex")) {
41+
log = "Start Obfuscator Dex"
42+
obfDex(it, logCallback)
43+
} else if (it.input.endsWith(".apk")) {
44+
log = "Start Obfuscator Apk"
45+
obfApk(it, logCallback)
46+
} else {
47+
log = "Only support Dex or Apk"
3448
}
49+
3550
}
3651
LogView(modifier = Modifier.weight(0.5F), log)
3752
}
3853
}
54+
55+
56+
private fun obfDex(inputBean: InputBean, logCallback: (log: String) -> Unit) {
57+
GlobalScope.launch(Dispatchers.IO){
58+
DexObf.run(inputBean,logCallback)
59+
}
60+
}
61+
62+
private fun obfApk(inputBean: InputBean, logCallback: (log: String) -> Unit) {
63+
GlobalScope.launch(Dispatchers.IO){
64+
ApkObf.run(inputBean,logCallback)
65+
logCallback.invoke("Please Sign APK!!!")
66+
}
67+
}

common/src/desktopMain/kotlin/cn/kaicity/common/platform/IFileHelper.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ package cn.kaicity.common.platform
33
import androidx.compose.ui.awt.ComposeWindow
44
import java.awt.FileDialog
55

6-
actual fun chooseFile(): String {
6+
actual fun chooseFile(callback:(String)->Unit) {
77
val fileDialog = FileDialog(ComposeWindow(), "InputDex", FileDialog.LOAD)
88
fileDialog.isVisible = true
99
if (fileDialog.directory.isNullOrEmpty() || fileDialog.file.isNullOrEmpty()) {
10-
return ""
10+
callback("")
11+
return
1112
}
12-
return fileDialog.directory + fileDialog.file
13+
return callback(fileDialog.directory + fileDialog.file)
1314
}
1415

15-
actual fun saveFile(): String {
16+
actual fun saveFile(callback:(String)->Unit) {
1617
val fileDialog = FileDialog(ComposeWindow(), "OutputDex", FileDialog.SAVE)
1718
fileDialog.isVisible = true
1819
if (fileDialog.directory.isNullOrEmpty() || fileDialog.file.isNullOrEmpty()) {
19-
return ""
20+
callback("")
21+
return
2022
}
21-
return fileDialog.directory + fileDialog.file
23+
return callback(fileDialog.directory + fileDialog.file)
2224
}
2325

common/src/desktopMain/kotlin/cn/kaicity/common/platform/IObfuscator.kt

-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ actual class IObfuscator actual constructor(private var flow: FlowCollector<Stri
3737
}
3838
}
3939

40-
close()
4140
}
4241

43-
suspend fun close() {
44-
}
4542
}
4643

0 commit comments

Comments
 (0)