From 8487ddba644b5d2aad762bea586654f9a7891b5e Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 31 Jan 2023 22:48:50 +0100 Subject: [PATCH] Use FakeContext for Application instance This will expose the correct package name and UID to the application context. PR #3757 --- .../java/com/genymobile/scrcpy/Workarounds.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java index e2345e11..64cc1272 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java +++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java @@ -2,14 +2,12 @@ package com.genymobile.scrcpy; import android.annotation.SuppressLint; import android.app.Application; -import android.app.Instrumentation; -import android.content.Context; +import android.content.ContextWrapper; import android.content.pm.ApplicationInfo; import android.os.Looper; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.Method; public final class Workarounds { private Workarounds() { @@ -62,11 +60,10 @@ public final class Workarounds { mBoundApplicationField.setAccessible(true); mBoundApplicationField.set(activityThread, appBindData); - // Context ctx = activityThread.getSystemContext(); - Method getSystemContextMethod = activityThreadClass.getDeclaredMethod("getSystemContext"); - Context ctx = (Context) getSystemContextMethod.invoke(activityThread); - - Application app = Instrumentation.newApplication(Application.class, ctx); + Application app = Application.class.newInstance(); + Field baseField = ContextWrapper.class.getDeclaredField("mBase"); + baseField.setAccessible(true); + baseField.set(app, FakeContext.get()); // activityThread.mInitialApplication = app; Field mInitialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");