forked from foxleezh/apkToJava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompile.gradle
192 lines (173 loc) · 5.68 KB
/
decompile.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import org.apache.tools.ant.taskdefs.condition.Os
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
}
}
ext {
BUILD_TOOLS_VERSION = '27.0.3'
TARGET_SDK_VERSION = 27
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
// 从系统环境变量中或者local.properties配置文件中读取SDK位置
if (System.getenv("ANDROID_HOME") != null) {
sdkDir = System.getenv("ANDROID_HOME")
} else { // 在local.properties中定义
sdkDir = properties.getProperty('sdk.dir')
}
sdk = [:]
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
sdk.aapt = "$sdkDir\\build-tools\\$BUILD_TOOLS_VERSION\\aapt.exe"
sdk.zip = "$rootDir\\tools\\7za\\7za.exe"
sdk.jadx = "$rootDir\\tools\\jadx\\bin\\jadx.bat"
sdk.apktool = "$rootDir\\tools\\apktool\\apktool.bat"
} else if (Os.isFamily(Os.FAMILY_MAC)) {
sdk.aapt = "$sdkDir/build-tools/$BUILD_TOOLS_VERSION/aapt"
sdk.zip = "$rootDir/tools/7za/7za"
sdk.jadx = "$rootDir/tools/jadx/bin/jadx"
sdk.apktool = "$rootDir/tools/apktool/apktool"
} else if (Os.isFamily(Os.FAMILY_UNIX)) {
sdk.aapt = "$sdkDir/build-tools/$BUILD_TOOLS_VERSION/aapt"
sdk.zip = "$rootDir/tools/7za/7za_linux"
sdk.jadx = "$rootDir/tools/jadx/bin/jadx"
sdk.apktool = "$rootDir/tools/apktool/apktool_linux"
}
sdk.androidJar = "$sdkDir/platforms/android-$TARGET_SDK_VERSION/android.jar"
if (TARGET_SDK_VERSION >= 23) {
sdk.apacheJar = "$sdkDir/platforms/android-23/optional/org.apache.http.legacy.jar";
}
}
//清除
task clearBuild(type: Delete) {
delete "$rootDir/app/build"
delete "$rootDir/app/libs"
delete "$rootDir/app/src"
delete "$rootDir/smali/build"
delete "$rootDir/smali/src"
}
//解压apk
task unzip(type: Exec, dependsOn: 'clearBuild') {
executable sdk.zip
def argv = []
argv << 'x'
argv << '-y'
argv << "$rootDir/apk/input.apk"
argv << "-o$rootDir/temp/tmp_apk"
args = argv
doLast {
project.copy {
from "$rootDir/temp/tmp_apk"
into "$rootDir/temp/dex"
include '*.dex'
}
}
}
//用jadx反编译dex为java
task jadx(type: Delete, dependsOn: 'unzip') {
//避免UP-TO-DATE,随便删除下
delete "$rootDir/abc"
def argv = []
doLast {
File file = new File("$rootDir/temp/dex/")
String[] list = file.list()
def size = list.size()
for (int i = 0; i < size; i++) {
project.exec {
executable sdk.jadx
argv = []
argv << '-d'
argv << 'temp/jadx'
argv << "$rootDir/temp/dex/" + list[i]
args = argv
}
System.out.println(list[i])
}
project.copy {
from "$rootDir/temp/jadx/sources"
into "$rootDir/app/src/main/java"
}
project.delete "$rootDir/temp"
}
}
//用apktool反编译apk得到res assets libs manifest
task decompileApk(type: Exec, dependsOn: 'jadx') {
executable sdk.apktool
def argv = []
argv << 'd'
argv << "$rootDir/apk/input.apk"
argv << '-o'
argv << "$rootDir/temp/tmp_apk"
args = argv
doLast {
project.copy {
from "$rootDir/temp/tmp_apk/assets"
into "$rootDir/app/src/main/assets"
}
project.copy {
from "$rootDir/temp/tmp_apk/lib"
into "$rootDir/app/libs"
}
project.copy {
from "$rootDir/temp/tmp_apk/res"
into "$rootDir/app/src/main/res"
}
project.copy {
from "$rootDir/temp/tmp_apk/"
into "$rootDir/app/src/main/"
include 'AndroidManifest.xml'
}
File file = new File("$rootDir/temp/tmp_apk/")
String[] list = file.list()
def size = list.size()
for (int i = 0; i < size; i++) {
if (list[i].startsWith('smali')) {
project.copy {
from "$rootDir/temp/tmp_apk/"+list[i]
into "$rootDir/smali/src/main/java"
}
}
}
project.delete "$rootDir/temp"
}
}
// 直接反编译 Dex 为 Java 和 Smali
task decompileDex(type: Delete) {
//避免UP-TO-DATE,随便删除下
delete "$rootDir/abc"
def argv = []
doLast {
File file = new File("$rootDir/temp/dex/")
String[] list = file.list()
def size = list.size()
for (int i = 0; i < size; i++) {
project.exec {
executable sdk.jadx
argv = []
argv << '-d'
argv << 'app/src/main/java'
argv << "$rootDir/temp/dex/" + list[i]
args = argv
}
project.exec {
executable "java"
argv = []
argv << '-jar'
argv << "$rootDir/tools/smali/lib/baksmali-2.3.4.jar"
argv << 'd'
argv << "$rootDir/temp/dex/" + list[i]
args = argv
}
System.out.println("开始反编译"+list[i])
}
project.copy {
from "$rootDir/out/"
into "$rootDir/smali/src/main/java"
}
project.delete "$rootDir/out"
}
}