Skip to content

Commit 91066a0

Browse files
committed
Remove dependency on Gson
Patch-loader might use Gson provided by the apk classLoader
1 parent bd136d8 commit 91066a0

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

patch-loader/src/main/java/org/lsposed/lspatch/loader/LSPApplication.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
import android.system.Os;
1414
import android.util.Log;
1515

16-
import com.google.gson.Gson;
17-
1816
import org.lsposed.lspatch.loader.util.FileUtils;
1917
import org.lsposed.lspatch.loader.util.XLog;
2018
import org.lsposed.lspatch.service.LocalApplicationService;
2119
import org.lsposed.lspatch.service.RemoteApplicationService;
22-
import org.lsposed.lspatch.share.PatchConfig;
2320
import org.lsposed.lspd.core.Startup;
2421
import org.lsposed.lspd.service.ILSPApplicationService;
22+
import org.json.JSONObject;
2523

2624
import java.io.BufferedReader;
2725
import java.io.File;
@@ -39,6 +37,7 @@
3937
import java.util.Collections;
4038
import java.util.Map;
4139
import java.util.function.BiConsumer;
40+
import java.util.stream.Collectors;
4241
import java.util.zip.ZipFile;
4342

4443
import de.robv.android.xposed.XposedHelpers;
@@ -58,7 +57,7 @@ public class LSPApplication {
5857
private static LoadedApk stubLoadedApk;
5958
private static LoadedApk appLoadedApk;
6059

61-
private static PatchConfig config;
60+
private static JSONObject config;
6261

6362
public static boolean isIsolated() {
6463
return (android.os.Process.myUid() % PER_USER_RANGE) >= FIRST_APP_ZYGOTE_ISOLATED_UID;
@@ -78,7 +77,7 @@ public static void onLoad() throws RemoteException, IOException {
7877

7978
Log.d(TAG, "Initialize service client");
8079
ILSPApplicationService service;
81-
if (config.useManager) {
80+
if (config.optBoolean("useManager")) {
8281
service = new RemoteApplicationService(context);
8382
} else {
8483
service = new LocalApplicationService(context);
@@ -94,7 +93,7 @@ public static void onLoad() throws RemoteException, IOException {
9493
Log.i(TAG, "Modules initialized");
9594

9695
switchAllClassLoader();
97-
SigBypass.doSigBypass(context, config.sigBypassLevel);
96+
SigBypass.doSigBypass(context, config.optInt("sigBypassLevel"));
9897

9998
Log.i(TAG, "LSPatch bootstrap completed");
10099
}
@@ -110,13 +109,13 @@ private static Context createLoadedApkWithContext() {
110109

111110
try (var is = baseClassLoader.getResourceAsStream(CONFIG_ASSET_PATH)) {
112111
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
113-
config = new Gson().fromJson(streamReader, PatchConfig.class);
114-
} catch (IOException e) {
115-
Log.e(TAG, "Failed to load config file");
112+
config = new JSONObject(streamReader.lines().collect(Collectors.joining()));
113+
} catch (Throwable e) {
114+
Log.e(TAG, "Failed to parse config file", e);
116115
return null;
117116
}
118-
Log.i(TAG, "Use manager: " + config.useManager);
119-
Log.i(TAG, "Signature bypass level: " + config.sigBypassLevel);
117+
Log.i(TAG, "Use manager: " + config.optBoolean("useManager"));
118+
Log.i(TAG, "Signature bypass level: " + config.optInt("sigBypassLevel"));
120119

121120
Path originPath = Paths.get(appInfo.dataDir, "cache/lspatch/origin/");
122121
Path cacheApkPath;
@@ -126,7 +125,9 @@ private static Context createLoadedApkWithContext() {
126125

127126
appInfo.sourceDir = cacheApkPath.toString();
128127
appInfo.publicSourceDir = cacheApkPath.toString();
129-
appInfo.appComponentFactory = config.appComponentFactory;
128+
if (config.has("appComponentFactory")) {
129+
appInfo.appComponentFactory = config.optString("appComponentFactory");
130+
}
130131

131132
if (!Files.exists(cacheApkPath)) {
132133
Log.i(TAG, "Extract original apk");
@@ -165,11 +166,11 @@ private static Context createLoadedApkWithContext() {
165166
Log.i(TAG, "hooked app initialized: " + appLoadedApk);
166167

167168
var context = (Context) XposedHelpers.callStaticMethod(Class.forName("android.app.ContextImpl"), "createAppContext", activityThread, stubLoadedApk);
168-
if (config.appComponentFactory != null) {
169+
if (config.has("appComponentFactory")) {
169170
try {
170-
context.getClassLoader().loadClass(config.appComponentFactory);
171+
context.getClassLoader().loadClass(appInfo.appComponentFactory);
171172
} catch (ClassNotFoundException e) { // This will happen on some strange shells like 360
172-
Log.w(TAG, "Original AppComponentFactory not found: " + config.appComponentFactory);
173+
Log.w(TAG, "Original AppComponentFactory not found: " + appInfo.appComponentFactory);
173174
appInfo.appComponentFactory = null;
174175
}
175176
}

patch-loader/src/main/java/org/lsposed/lspatch/loader/SigBypass.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import android.util.Base64;
1313
import android.util.Log;
1414

15-
import com.google.gson.Gson;
1615
import com.google.gson.JsonSyntaxException;
1716

1817
import org.lsposed.lspatch.loader.util.XLog;
1918
import org.lsposed.lspatch.share.Constants;
20-
import org.lsposed.lspatch.share.PatchConfig;
19+
import org.json.JSONException;
20+
import org.json.JSONObject;
2121

2222
import java.io.IOException;
2323
import java.nio.charset.StandardCharsets;
@@ -46,8 +46,12 @@ private static void replaceSignature(Context context, PackageInfo packageInfo) {
4646
if (metaData != null) encoded = metaData.getString("lspatch");
4747
if (encoded != null) {
4848
var json = new String(Base64.decode(encoded, Base64.DEFAULT), StandardCharsets.UTF_8);
49-
var patchConfig = new Gson().fromJson(json, PatchConfig.class);
50-
replacement = patchConfig.originalSignature;
49+
try {
50+
var patchConfig = new JSONObject(json);
51+
replacement = patchConfig.getString("originalSignature");
52+
} catch (JSONException e) {
53+
Log.w(TAG, "fail to get originalSignature", e);
54+
}
5155
}
5256
} catch (PackageManager.NameNotFoundException | JsonSyntaxException ignored) {
5357
}

0 commit comments

Comments
 (0)