Pergunta

in my activity I had some inline AsyncTask that didn't work well on android 11+ so I decided to change all inline code to inner classes. After change all inline AsyncTask to inner classes I got this warning in proguard and I can't compile the project. In debug mode all work well.

This is my proguard blocking warnings:

[proguard] Warning: com.test.MyActivity: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$MyBroadcastReceiver: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$MyBroadcastReceiver: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$MyBroadcastReceiver: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$LoadingTask: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$LoadingTask: can't find referenced class com.test.MyActivity$1
[proguard] Warning: com.test.MyActivity$LoadingTask: can't find referenced class com.test.MyActivity$1



This is my actual activity code:

public class MyActivity extends FragmentActivity {

    private final WakefulBroadcastReceiver messageReceiver = new MyBroadcastReceiver();

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final LoadingTask task = new LoadingTask();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            task.execute();
        }
    }

    private final class MyBroadcastReceiver extends WakefulBroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            if (context == getContext()) {
                GcmBroadcastReceiver.completeWakefulIntent(intent);
                setResultCode(Activity.RESULT_OK);
            } else {
                setResultCode(Activity.RESULT_CANCELED);
            }
        }
    }

    private final class LoadingTask extends AsyncTask<Void, Long, Boolean> {

        private static final int MAX_SHOW_TIME = 120000;
        private long startTime;
        private LoadingDialog loadingDialog;

        protected void onPreExecute() {
            startTime = SystemClock.elapsedRealtime();
            loadingDialog = new LoadingDialog(getContext());
            loadingDialog.show();
        }

        protected Boolean doInBackground(Void... params) {
            synchronized (this) {
                long actualTime = SystemClock.elapsedRealtime();
                while (!initialize) {
                    publishProgress((actualTime - startTime) / 1000);
                    try {
                        this.wait(250);
                    } catch (InterruptedException e) { }
                    actualTime = SystemClock.elapsedRealtime();
                    if (actualTime - startTime > MAX_SHOW_TIME) {
                        return false;
                    }
                }
            }
            return true;
        }

        protected void onProgressUpdate(Long... values) {
            loadingDialog.setProgressValue(values[0]);
        }

        protected void onPostExecute(Boolean result) {
            loadingDialog.dismiss();
        }
    }
}

EDIT:

Default proguard configuration: ${sdk.dir}/tools/proguard/proguard-android.txt
Custom proguard configuration (proguard.txt)

-keepattributes Signature
-keep class android.** { *; }
-keep class com.facebook.** { *; }
-keepclassmembers interface * extends com.facebook.model.GraphObject { *; }

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

# Keep SafeParcelable value, needed for reflection. This is required to support backwards
# compatibility of some classes.
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

# Keep the names of classes/members we need for client functionality.
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

# Needed for Parcelable/SafeParcelable Creators to not get stripped
-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keep class com.google.** { *; }

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

-keepnames class com.test.client.** implements java.io.Serializable
-keepclassmembernames class com.test.client.** implements java.io.Serializable {
    private !static <fields>;
 }

Can anybody help or suggest me the solution? Thanks

Foi útil?

Solução

I've had the same problem, and changing the scope of the AsyncTask worked. You can try with protected:

...
protected final class LoadingTask extends AsyncTask<Void, Long, Boolean> {
...

Outras dicas

In your proguard-project.txt file, you may need to add these lines:

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

Also, if you extends Application or Service, you'll need these directives, too:

-keep public class * extends android.app.Application
-keep public class * extends android.app.Service

These warnings are printed after the input has been read and before any processing, so -keep options are still irrelevant.

The warnings explain that the compiled com/test/MyActivity.class refers to an anonymous inner class com/test/MyActivity$1.class, but that the anonymous inner class can't be found. This suggests that the code hasn't been recompiled properly. Looking at the source code, I don't see an anonymous inner class right away, so either the activity class hasn't been recompiled, or an old copy of it is lingering in some directory and ends up being read as input. You should start from a clean project, without any old .class files anywhere, and build again.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top