Skip to content

Commit

Permalink
Initialize Application object to avoid NPE
Browse files Browse the repository at this point in the history
When an ApplicationInfo is set (commit
9029324), some devices (Nvidia Shield
TV) attempt to access the Application object, causing a
NullPointerException.

As a workaround, initialize an Application object.

Fixes <#940>
  • Loading branch information
rom1v committed Nov 24, 2019
1 parent ebdc2ee commit 2b84568
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.genymobile.scrcpy;

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Instrumentation;
import android.content.Context;
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() {
Expand Down Expand Up @@ -56,6 +60,17 @@ public static void fillAppInfo() {
Field mBoundApplicationField = activityThreadClass.getDeclaredField("mBoundApplication");
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);

// activityThread.mInitialApplication = app;
Field mInitialApplicationField = activityThreadClass.getDeclaredField("mInitialApplication");
mInitialApplicationField.setAccessible(true);
mInitialApplicationField.set(activityThread, app);
} catch (Throwable throwable) {
// this is a workaround, so failing is not an error
Ln.w("Could not fill app info: " + throwable.getMessage());
Expand Down

0 comments on commit 2b84568

Please sign in to comment.