4

In my Android app I am building in release mode. With Proguard activated an rare issue is caused, a specific for-loop never is executed:

List<MyClass> objectList = getObjectList();
Log.d("Step 1", String.valueOf(objectList.size())); //Print size > 0

for(MyClass object: objectList) {
  Log.d("Step 2", object.toString()); //Never printed
  ...
}

The "Step 1" Log is printed correctly and objectList.size() > 0. I don't understand what is causing "Step 2" Log is never printed (and all code into for-loop never executed). I am using the Android Device manager Logcat.

In debug mode or with Proguard disabled this snippet works correctly.

Thanks in advance.

Update

I just added -dontoptimize but the problem was not resolved. This is my proguard-rules file:

-dontoptimize
-dontpreverify
-repackageclasses ''
-allowaccessmodification
#-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*,EnclosingMethod,SourceFile,LineNumberTable
-renamesourcefileattribute SourceFile

-keepnames class com.androidplot.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class org.acra.** { *; }
-keep,allowoptimization class com.mypackage.myapp.model.** { *; }

-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.preference.PreferenceFragment

-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 * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

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

-dontwarn com.mypackage.myapp.**
-dontwarn com.fasterxml.jackson.databind.ext.**
-dontwarn com.google.common.**
-dontwarn android.support.**
Manuel Lopera
  • 2,268
  • 1
  • 16
  • 16

1 Answers1

3

https://stuff.mit.edu/afs/sipb/project/android/sdk/android-sdk-linux/tools/proguard/docs/index.html#manual/troubleshooting.html

Disappearing loops

If your code contains empty busy-waiting loops, ProGuard's optimization step may remove them. More specifically, this happens if a loop continuously checks the value of a non-volatile field that is changed in a different thread. The specifications of the Java Virtual Machine require that you always mark fields that are accessed across different threads without further synchronization as volatile. If this is not possible for some reason, you'll have to switch off optimization using the -dontoptimize option.

Community
  • 1
  • 1
terencey
  • 3,282
  • 4
  • 32
  • 40
  • Thanks @Terence, I added `-dontoptimize` to proguard rules file, but the problem persist. I just update the question with proguard rules file content. – Manuel Lopera May 13 '15 at 19:06
  • I don't know that proguard is able to tell that `Log.d` doesn't do anything useful. It does write to a stream that goes out of the app, so I don't think proguard would be entitled to consider it useless. – njzk2 May 13 '15 at 21:10
  • Are you using Jackson or Gson in your code? Your classes specific to Json data will have to be prevented from obsfucation. – terencey May 14 '15 at 03:42
  • I am using `-keepattributes *Annotation*` and `-keepnames class com.fasterxml.jackson.** { *; }` in the proguard rules file. Is it enough for prevent obfuscation in Jackson classes?. The problem still remains, I had to release without proguard... It is a nightmare!!! – Manuel Lopera May 19 '15 at 13:44
  • Yes @squeeish, I rewrote the code. I never found the error cause. – Manuel Lopera Aug 04 '16 at 18:16