Skip to content

Commit

Permalink
Merge pull request #93 from IdioticMadman/fix_breakAndroidP
Browse files Browse the repository at this point in the history
fix: breakAndroidP failed
  • Loading branch information
JeasonWong committed Jun 10, 2022
2 parents 6adcb4e + cc9241b commit a6133f0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 51 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ allprojects {
jcenter()
mavenCentral()
google()
maven { url 'https://jitpack.io' }
}
}

Expand Down
1 change: 1 addition & 0 deletions uetool-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android {
api "com.android.support:appcompat-v7:27.1.1"
api "com.android.support:recyclerview-v7:27.1.1"
api "com.android.support:support-v4:27.1.1"
implementation "com.github.tiann:FreeReflection:3.1.0"
}

lintOptions {
Expand Down
7 changes: 6 additions & 1 deletion uetool-base/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<manifest package="me.ele.uetool.base"/>
<manifest package="me.ele.uetool.base">

<!-- 压制freeReflection的minSDK为21 -->
<uses-sdk xmlns:tools="http://schemas.android.com/tools"
tools:overrideLibrary="me.weishu.freereflection" />
</manifest>
57 changes: 7 additions & 50 deletions uetool-base/src/main/java/me/ele/uetool/base/ReflectionP.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;
import me.weishu.reflection.Reflection;

/**
* 来自 weishu FreeReflection
Expand All @@ -13,62 +13,19 @@ public class ReflectionP {

private static final String TAG = "Reflection";

private static Object sVmRuntime;
private static Method setHiddenApiExemptions;

static {
try {
Method forName = Class.class.getDeclaredMethod("forName", String.class);
Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);

Class<?> vmRuntimeClass = (Class<?>) forName.invoke(null, "dalvik.system.VMRuntime");
Method getRuntime = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null);
setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
sVmRuntime = getRuntime.invoke(null);
} catch (Throwable e) {
Log.e(TAG, "reflect bootstrap failed:", e);
}
}

public static <T> T breakAndroidP(Func<T> func) {
T result;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
exemptAll();
result = func.call();
} else {
result = func.call();
try {
Reflection.unseal(Application.getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
}
result = func.call();
return result;
}

/**
* make specific methods exempted from hidden API check.
*
* @param methods the method signature prefix, such as "Ldalvik/system", "Landroid" or even "L"
* @return true if success
*/
private static boolean exempt(String... methods) {
if (sVmRuntime == null || setHiddenApiExemptions == null) {
return false;
}

try {
setHiddenApiExemptions.invoke(sVmRuntime, new Object[]{methods});
return true;
} catch (Throwable e) {
return false;
}
}

/**
* Make all hidden API exempted.
*
* @return true if success.
*/
private static boolean exemptAll() {
return exempt(new String[]{"L"});
}

public interface Func<T> {
T call();
}
Expand Down

0 comments on commit a6133f0

Please sign in to comment.