AsyncTask导致NoClassDefFoundError

时间:2017-05-13 16:20:51

标签: android android-asynctask jsoup

我定义了一个asynctask来定期执行任务。有用。我已经无数次测试了它。但是,出于某种原因,它偶尔会崩溃。我找不到问题的根源,因为堆栈跟踪非常模糊。这是堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: An error occured while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:299)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
       at java.util.concurrent.FutureTask.run(FutureTask.java:239)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
       at java.lang.Thread.run(Thread.java:856)
Caused by java.lang.NoClassDefFoundError: android.system.ErrnoException
       at dita.dev.myportal.SyncManager$GetData.doInBackground(Unknown Source)
       at dita.dev.myportal.SyncManager$GetData.doInBackground(Unknown Source)
       at android.os.AsyncTask$2.call(AsyncTask.java:287)
       at java.util.concurrent.FutureTask.run(FutureTask.java:234)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
       at java.lang.Thread.run(Thread.java:856)

起初我以为是因为我使用的是proguard和jsoup,但我在添加jsoup规则后对其进行了测试,并且它可以正常工作。这是我对jsoup的规则:

-keeppackagenames org.jsoup.nodes
-keep public class org.jsoup.** {
    public *;
}

这是正在执行的任务:

private static class GetData extends AsyncTask<Void, Integer, Boolean> {

        private Context context;
        private String message;

        GetData(Context context) {
            this.context = context;
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            String username = Preferences.getString(context, "username");
            String password = Preferences.getString(context, "password");
            if (username == null) {
                return false;
            }
            PortalCrawler crawler = new PortalCrawler(Config.BASE_URL1);

            try {
                crawler.start(username, password);
                crawler.saveData();
                return true;
            } catch (Exception e) {
                if (e instanceof SocketTimeoutException || e instanceof UnknownHostException ||
                        e instanceof ErrnoException || e instanceof SocketException) {
                    crawler.setBaseUrl(Config.BASE_URL2);
                    try {
                        crawler.start(username, password);
                        crawler.saveData();
                        return  true;
                    } catch (Exception e1) {
                        e1.printStackTrace();

                        if (e1 instanceof SocketTimeoutException || e1 instanceof UnknownHostException
                                || e1 instanceof ErrnoException || e1 instanceof SocketException) {
                            message = "Unable to connect";
                        } else {
                            Crashlytics.logException(e.getCause());
                            message = "An error occurred";
                        }
                    }

                } else {
                    e.printStackTrace();
                    Crashlytics.logException(e.getCause());
                    message = "An error occurred";
                }

            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean success) {
            if (success) {
                Preferences.setBoolean(context, "initialSync", true);
                message = "Sync completed";
            }
            EventBus.getDefault().post(new Events.SyncDone(success, message));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题在于:

e instanceof ErrnoException

我怀疑您是在尝试在不存在android.system.ErrnoException的pre-API-21设备上运行此应用程序。它是在21年推出的。

您针对post 21 API编译了它,但之后尝试在21之前的设备上运行它。