TWiStErRob / net.twisterrob.libraries

1 stars 0 forks source link

Fix StrictMode violations #72

Open TWiStErRob opened 1 year ago

TWiStErRob commented 1 year ago

e.g. API 29:

W  Accessing hidden field Ldalvik/system/BaseDexClassLoader;->pathList:Ldalvik/system/DexPathList; (greylist, reflection, allowed)
StrictMode D  StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Ldalvik/system/BaseDexClassLoader;->pathList:Ldalvik/system/DexPathList;
  at android.os.StrictMode.lambda$static$1(StrictMode.java:407)
  at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
  at java.lang.Class.getDeclaredField(Native Method)
  at net.twisterrob.java.utils.ReflectionTools.findDeclaredField(ReflectionTools.java:113)
  at net.twisterrob.java.utils.ReflectionTools.get(ReflectionTools.java:59)
  at net.twisterrob.android.test.junit.internal.DexPathListReflection.scanClassloaderForClasspath(DexPathListReflection.java:109)
  at net.twisterrob.android.test.junit.internal.DexPathListReflection.dexClasspath(DexPathListReflection.java:78)
  at net.twisterrob.android.test.junit.AndroidJUnitRunner.setDefaults(AndroidJUnitRunner.java:137)
  at net.twisterrob.android.test.junit.AndroidJUnitRunner.onCreate(AndroidJUnitRunner.java:50)
  at net.twisterrob.inventory.android.InventoryJUnitRunner.onCreate(InventoryJUnitRunner.java:10)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6452)
  at android.app.ActivityThread.access$1300(ActivityThread.java:219)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
  at android.os.Handler.dispatchMessage(Handler.java:107)
  at android.os.Looper.loop(Looper.java:214)
  at android.app.ActivityThread.main(ActivityThread.java:7356)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

one "solution" might be:

Index: twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/junit/internal/DexPathListReflection.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/junit/internal/DexPathListReflection.java b/twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/junit/internal/DexPathListReflection.java
--- a/twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/junit/internal/DexPathListReflection.java  (revision 56b7c95950e08cea0ce52ddc26e454f0620e436b)
+++ b/twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/junit/internal/DexPathListReflection.java  (date 1693044623579)
@@ -7,6 +7,7 @@
 import org.slf4j.LoggerFactory;

 import android.app.Instrumentation;
+import android.os.Build;
 import android.os.StrictMode;
 import android.os.StrictMode.ThreadPolicy;

@@ -72,12 +73,21 @@
     * @return list of {@link File}s pointing to either an Instrumentation APK or a DEX ZIP file.
     */
    public @NonNull List<File> dexClasspath() {
-       ThreadPolicy originalPolicy = StrictMode.allowThreadDiskWrites(); // see EOF
-       try {
-           installMultiDex();
-           return scanClassloaderForClasspath();
-       } finally {
-           StrictMode.setThreadPolicy(originalPolicy);
+       StrictMode.VmPolicy originalSdkPolicy = StrictMode.getVmPolicy();
+       if (Build.VERSION_CODES.P <= Build.VERSION.SDK_INT) {
+           StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder(originalSdkPolicy)
+                   .permitNonSdkApiUsage().build());
+       }
+       try {
+           ThreadPolicy originalPolicy = StrictMode.allowThreadDiskWrites(); // see EOF
+           try {
+               installMultiDex();
+               return scanClassloaderForClasspath();
+           } finally {
+               StrictMode.setThreadPolicy(originalPolicy);
+           }
+       } finally {
+           StrictMode.setVmPolicy(originalSdkPolicy);
        }
    }