OSDN Git Service

Fix usage of reflection in unit tests
authorBraden Farmer <farmerbb@gmail.com>
Fri, 1 Oct 2021 04:40:42 +0000 (22:40 -0600)
committerBraden Farmer <farmerbb@gmail.com>
Fri, 1 Oct 2021 04:40:42 +0000 (22:40 -0600)
app/build.gradle
app/src/jacoco/java/org/lsposed/hiddenapibypass/HiddenApiBypass.java [new file with mode: 0644]

index 345c137..6940dd8 100644 (file)
@@ -143,7 +143,8 @@ dependencies {
     implementation 'com.mikepenz:foundation-icons-typeface:3.0.0.5'
     implementation 'moe.banana:toast-compat:1.0.5'
     implementation group:'com.twofortyfouram', name:'android-plugin-api-for-locale', version:'[1.0.2,2.0['
-    implementation "org.lsposed.hiddenapibypass:hiddenapibypass:$HIDDEN_API_BYPASS_VERSION"
+    debugImplementation "org.lsposed.hiddenapibypass:hiddenapibypass:$HIDDEN_API_BYPASS_VERSION"
+    releaseImplementation "org.lsposed.hiddenapibypass:hiddenapibypass:$HIDDEN_API_BYPASS_VERSION"
 
     testImplementation "com.google.truth:truth:1.1.3"
     testImplementation 'junit:junit:4.13.2'
diff --git a/app/src/jacoco/java/org/lsposed/hiddenapibypass/HiddenApiBypass.java b/app/src/jacoco/java/org/lsposed/hiddenapibypass/HiddenApiBypass.java
new file mode 100644 (file)
index 0000000..ce56ec0
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2021 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.lsposed.hiddenapibypass;
+
+import java.lang.reflect.Method;
+
+public final class HiddenApiBypass {
+    public static boolean addHiddenApiExemptions(String... signaturePrefixes) {
+
+        // This is the older logic for U.allowReflection(), kept here to preserve compatibility with unit tests
+        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);
+            Method setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
+
+            Object vmRuntime = getRuntime.invoke(null);
+            setHiddenApiExemptions.invoke(vmRuntime, new Object[]{new String[]{"L"}});
+        } catch (Throwable ignored) {}
+
+        return false;
+    }
+}