Fix Map.of crash on certain Motorola devices (#186)

Using Map.of here results in compiler optimization generating a class
`a.a` with a method `a` that returns the Map.

The class looks like this (jadx decompilation):
```java
package a;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/* loaded from: classes.dex */
public abstract /* synthetic */ class a {
    public static /* synthetic */ Map a() {
        Map.Entry[] entryArr = {new AbstractMap.SimpleEntry("arm", "armeabi-v7a"), new AbstractMap.SimpleEntry("arm64", "arm64-v8a"), new AbstractMap.SimpleEntry("x86", "x86"), new AbstractMap.SimpleEntry("x86_64", "x86_64")};
        HashMap hashMap = new HashMap(4);
        for (int i = 0; i < 4; i++) {
            Map.Entry entry = entryArr[i];
            Object key = entry.getKey();
            Objects.requireNonNull(key);
            Object value = entry.getValue();
            Objects.requireNonNull(value);
            if (hashMap.put(key, value) != null) {
                throw new IllegalArgumentException("duplicate key: " + key);
            }
        }
        return Collections.unmodifiableMap(hashMap);
    }
}
```

For whatever ridiculous reason, on a moto g stylus 5G (2022), when this
method is called in `LSPAppComponentFactoryStub`, the app crashes with
`java.lang.NoSuchMethodError: No static method a()Ljava/util/Map; in
class La/a; or its super classes`. Using a normal HashMap here instead
prevents this optimization from occurring, which prevents the crash.
This commit is contained in:
AAGaming 2023-05-01 15:56:24 +00:00 committed by GitHub
parent 4d8bafd74f
commit e4657377b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -19,6 +19,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipFile;
@ -27,17 +28,17 @@ import java.util.zip.ZipFile;
public class LSPAppComponentFactoryStub extends AppComponentFactory {
private static final String TAG = "LSPatch-MetaLoader";
private static final Map<String, String> archToLib = Map.of(
"arm", "armeabi-v7a",
"arm64", "arm64-v8a",
"x86", "x86",
"x86_64", "x86_64"
);
private static final Map<String, String> archToLib = new HashMap<String, String>(4);
public static byte[] dex;
static {
try {
archToLib.put("arm", "armeabi-v7a");
archToLib.put("arm64", "arm64-v8a");
archToLib.put("x86", "x86");
archToLib.put("x86_64", "x86_64");
var cl = Objects.requireNonNull(LSPAppComponentFactoryStub.class.getClassLoader());
Class<?> VMRuntime = Class.forName("dalvik.system.VMRuntime");
Method getRuntime = VMRuntime.getDeclaredMethod("getRuntime");