Android 应用许可在 Android Pie 上崩溃

Android App Licensing crashes on Android Pie

错误提示 URLEncodedUtils 未找到。有没有解决方法。

Caused by java.lang.ClassNotFoundException
    Didn't find class "org.apache.http.client.utils.URLEncodedUtils" on path: DexPathList[[zip file "/data/app/com.app.p-MY70To6m946K0_uiYLCsSg==/base.apk"],nativeLibraryDirectories=[/data/app/com.app.p-MY70To6m946K0_uiYLCsSg==/lib/arm64, /data/app/com.app.p-MY70To6m946K0_uiYLCsSg==/base.apk!/lib/arm64-v8a, /system/lib64]]

我想知道这在 Pie 上是否仍然可用,但除了另一个可能重复的问题的建议之外,还有一个 Gradle 配置:useLibrary 'org.apache.http.legacy',以便使用旧版类(仍然可用)。否则,一般来说,迁移到 okhttp3 可能仍然是最佳选择。

Google 不支持遗留的 apache 库所以在原来的 class APKEpansionPolicy 这个方法使用 URLEncodedUtilsNameValuePair

private Map<String, String> decodeExtras(String extras) {
        Map<String, String> results = new HashMap<String, String>();
        try {
            URI rawExtras = new URI("?" + extras);
            List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
            for (NameValuePair item : extraList) {
                String name = item.getName();
                int i = 0;
                while (results.containsKey(name)) {
                    name = item.getName() + ++i;
                }
                results.put(name, item.getValue());
            }
        } catch (URISyntaxException e) {
            Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
        }
        return results;
    }

将此方法和任何其他 class 中的此方法替换为拆分查询并使用 Map 而不是 NameValuePair 的这段代码。

private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        Map<String, String> extraList = splitQuery(new URL(rawExtras.toString()));
        for (Map.Entry<String, String> entry : extraList.entrySet())
        {
            String name = entry.getKey();
            int i = 0;
            while (results.containsKey(name)) {
                name = entry.getKey() + ++i;
            }
            results.put(name, entry.getValue());
        }
    } catch (URISyntaxException e) {
        Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return results;
}

public static Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException {
    Map<String, String> query_pairs = new LinkedHashMap<String, String>();
    String query = url.getQuery();
    String[] pairs = query.split("&");
    for (String pair : pairs) {
        int idx = pair.indexOf("=");
        query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
    }
    return query_pairs;
}

Apache 库已被删除。如果您之前使用

useLibrary 'org.apache.http.legacy 

在您的 build.gradle 中,它不再适用于 Android 饼图。

相反,您必须添加

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

给你的 AndroidManifest.

请参阅 here 了解官方发行说明