javaandroidexceptionandroid-asynctaskillegalaccessexception

IllegalAccessError only on Android 9 inside AsyncTask


IllegalAccessError happen only on Android 9 during an AsyncTask function

Is there something to do on Android 9 ? All others version are OK.

The error is seen on production app.

I have already try to make everything public, and in same package. This error is still there on version 9.

    new TestTask(this, "test", true).execute(value);

public class TestTask extends AsyncTask<String, Void, String> {

    private String param1;
    private boolean param2;
    private String number;
    private WeakReference<Context> appReference;

    TestTask(Context context, String param1, boolean param2) {
        this.appReference = new WeakReference<>(context);
        this.param1 = param1;
        this.param2 = param2;
    }

    @Override
    public String doInBackground(String... values) {
        try {
            number = values[0];

            if(param2)
                performVib();

            ResponseEvent response = new TestSnmp(param1).set(number);

        if( appReference.get()!=null && m_debugMode) {
            Toast.makeText(appReference.get(), "Response: " + response, Toast.LENGTH_SHORT).show();
        }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            if( appReference.get()!=null ) {
                Toast.makeText(appReference.get(), "Some text", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is the stack:

java.lang.RuntimeException: 
  at android.os.AsyncTask$3.done (AsyncTask.java:354)
  at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:383)
  at java.util.concurrent.FutureTask.setException (FutureTask.java:252)
  at java.util.concurrent.FutureTask.run (FutureTask.java:271)
  at android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:245)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at java.lang.Thread.run (Thread.java:764)
Caused by: java.lang.IllegalAccessError: 
  at xxx.TestTask.doInBackground (TestTask.java)
  at xxx.TestTask.doInBackground (TestTask.java)
  at android.os.AsyncTask$2.call (AsyncTask.java:333)
  at java.util.concurrent.FutureTask.run (FutureTask.java:266)
  at android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:245)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at java.lang.Thread.run (Thread.java:764)

Solution

  • I found that the issue come from Proguard.

    I have added this line in proguard-rules.pro : -keep class org.snmp4j.** {*;} and it seems to work now !