java.lang.NoSuchMethodError Lorg/apache/http/util/ByteArrayBuffer 启用混淆器后

java.lang.NoSuchMethodError Lorg/apache/http/util/ByteArrayBuffer after enabling progurd

我的应用程序正在崩溃,如下面的日志所示。

这些代码我用了很久,现在突然崩溃了。

应用程序只有在我启用程序时才会崩溃。在不启用 progurd 的情况下,它正在运行

 java.lang.NoSuchMethodError: No virtual method g([BII)V in class Lorg/apache/http/util/ByteArrayBuffer; or its super classes (declaration of 'org.apache.http.util.ByteArrayBuffer' appears in /system/framework/org.apache.http.legacy.boot.jar)
     at android_support.bee.a(AbstractMultipartForm.java:55)
     at android_support.bee.<clinit>(AbstractMultipartForm.java:92)
     at android_support.bem.Iu(MultipartEntityBuilder.java:192)
     at android_support.bem.Iv(MultipartEntityBuilder.java:204)
     at android_support.bcx.mo(BaseUploadHttpRequest.java:119)
     at android_support.mp.b(HurlStack.java:245)
     at android_support.mp.a(HurlStack.java:219)
     at android_support.mp.a(HurlStack.java:97)
     at android_support.mi.c(BasicNetwork.java:131)
     at android_support.lz.processRequest(NetworkDispatcher.java:120)
     at android_support.lz.run(NetworkDispatcher.java:87)

Profurd 规则

-overloadaggressively
-useuniqueclassmembernames


-dontwarn org.apache.http.**
#-keep interface modules.** { *; }


#Crashlytics
#-keepattributes *Annotation*
#-keepattributes SourceFile,LineNumberTable

#-keep class com.crashlytics.** { *; }
#-dontwarn com.crashlytics.**

#end crashlytics



-dontpreverify
-repackageclasses 'android_spt'
-allowaccessmodification
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
      public <init>(android.content.Context);
      public <init>(android.content.Context, android.util.AttributeSet);
      public <init>(android.content.Context, android.util.AttributeSet, int);
      public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
    public void *(android.view.View);
    public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

这是代码,我正在使用 volley 向服务器发送多部分数据,应用程序在 mBuilder.build();

时崩溃
public class BaseUploadHttpRequest<T> extends Request<T> {

    private static final int TIMEOUT_RETRY_POLICY = 15000;

    private static Map<String, String> mParams = new HashMap<String, String>();
    private static Map<String, File> mFiles = new HashMap<String, File>();



    private final Object deserializer;
    private final Class<T> clazz;
    private final Listener<T> listener;
    private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();


    public BaseUploadHttpRequest(String url, Class<T> clazz, Map<String, String> params, Map<String, File> files, Listener<T> listener,
                                 ErrorListener errorListener, Object deserializer) {
        super(Method.POST, url, errorListener);
        setRetryPolicy(new DefaultRetryPolicy(
                TIMEOUT_RETRY_POLICY,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        this.clazz = clazz;
        this.listener = listener;
        this.deserializer = deserializer;
        mFiles.putAll(files);
        //mHeader.putAll(appendHeader);
        mParams.putAll(params);
        buildMultipartEntity();
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = super.getHeaders();

        if (headers == null
                || headers.equals(Collections.emptyMap())) {
            headers = new HashMap<String, String>();
        }

        headers.put("Accept", "application/json");

        return headers;
    }

    private void buildMultipartEntity() {
        int count = 0;
        for (String key : mFiles.keySet()) {
            File file = mFiles.get(key);
            String fileName = file.getName();
            mBuilder.addBinaryBody(key, file, ContentType.create("image/jpeg"), fileName);
        }

        try {
            for (String key : mParams.keySet()) {
                mBuilder.addTextBody(key, mParams.get(key));
            }
        } catch (Exception e) {
            VolleyLog.e("Exception");
        }
        mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));
    }

    @Override
    public String getBodyContentType() {
        return mBuilder.build().getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
           HttpEntity httpEntity= mBuilder.build(); /// application crash our here
           httpEntity.writeTo(bos);
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
        }

        return bos.toByteArray();
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }


}

我正在使用 implementation files('libs/httpclient-osgi-4.3.jar') 和 volley implementation 'com.android.volley:volley:1.1.0'

试试这个。 Proguard 应该忽略外部库,因为外部库已经被压缩。

有时,当再次re-compressing外部库

时,混淆器会导致错误
-keep class org.apache.** { *; }

将上面添加到下面

-dontwarn org.apache.http.**
-keep class org.apache.** { *; } // add it here

参考:How to make Proguard ignore external libraries?

首先,您将 httpclient-osgi-4.3.jar 更新为 here

的最新版本

然后修改 progurd 规则以保持 class 如下所示

-keep class org.apache.** { *; }