Search code examples
androidandroid-asynctasksigned-apkandroid-signing

Asynctask executes properly in debug version, but fails to update UI in release version (signed apk)


I am using an Asynctask to do some processing, after which a UI List is updated with the remaining/ additional items (addition or deletion case).

The functionality works properly in the debug apk that I work on in Android Studio. However, it is not working in the release APK file.

When I analysed further, I am seeing that the functionality is showing correct result when I lock and unlock the screen, or if I close and reopen the app. It fails only in producing an instantaneous update.

This is why I felt there is something wrong in the asynctask.

public class RefreshDisplayAsyncTask extends AsyncTask<Void, Void, Void> {
private Activity curActivity = null;
private boolean shouldProceed = false;

public RefreshDisplayAsyncTask(Activity activity)
{
    curActivity = activity;
}
protected Void doInBackground(Void... params) {
    //Log.d("UIBug","RefreshTask entered");
    ((MainActivity)curActivity).setUpList();
    shouldProceed = false;
    curActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ((MainActivity)curActivity).listView.setAdapter(new listArrayAdapter(curActivity,((MainActivity)curActivity).list1,((MainActivity)curActivity).list2));

            shouldProceed = true;
        }
    });
    while(!shouldProceed){}
    //Log.d("UIBug","RefreshTask completed");
    return null;
}

@Override
protected void onPreExecute() {

    super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {

//        if(progressDialog!=null)
//      {
//        progressDialog.dismiss();
  //  }
 }


}

Why is the signed apk producing incorrect behaviour while the unsigned apk is behaving properly? How to rectify this?


Solution

  • First of all: NEVER pass your Activity to an AsyncTask, this creates memory leaks when the AsyncTask is made to persist across Activity recreations and is considered bad practice. If you have to pass data around, use something like EventBus instead.

    Your code example might fail because of tougher restrictions on non-debug apps by the system.

    In any case, there is a lot of unsave code in your example. For instance the while(!shouldProceed) part is atrocious.

    Generally you are not using the AsyncTask as indended. You should rewrite it to utilize onPostExecute instead of the above mentioned while loop for starters. Please see here for further info on how an AsyncTask should be structured and follow that structure.

    As discussed in the comments, cleaning up the AsyncTask solved your problem.