2

I am trying to iterate over public static fields in a class with private constructor, but getFields/getDeclaredField returns a zeroSize array if Proguard is On, so I never see Log messages from loop. In profuard I added

-keep public class com.ello.bases.SyncKeysConstants

But it did not help Constants

public final class SyncKeysConstants {
  private SyncKeysConstants() {
  }
  public static final String HOME = "home";

}

Method

public class ConnectionBroadcastReceiver extends BroadcastReceiver {
  public ConnectionBroadcastReceiver() {
  }

  @Override public void onReceive(Context context, Intent intent) {
    boolean connected = true;
    Log.e("updateConnection", " current - " + connected);
    Field[] fields = SyncKeysConstants.class.getFields();
    Log.e("updateConnection", "fields length - " + fields.length);

    for (Field f : fields) {
      Log.e("updateConnection", "field name - " + f.getName());
      try {
        String value = (String) f.get(null);
        Log.e("updateConnection", "field name - " + f.getName() + ", data - " + value);
        if (connected) {
          SyncStatus.setNoConnection(context, value, false);
        }
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
  }
}

Log

E/updateConnection:  current - true
E/updateConnection: fields length - 0

Full proguard

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify

# Reduce the size of the output some more.

-repackageclasses ''
-allowaccessmodification

# Switch off some optimizations that trip older versions of the Dalvik VM.
-dontobfuscate
-optimizations !code/allocation/variable/!field

# keep annotations for otto, retrofit, butterknife and others
-keepattributes *Annotation*

#keep signatures
-keepattributes Signature
# Preserve all fundamental application classes.

-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 com.ello.bases.SyncKeysConstants
# Preserve all View implementations, their special context constructors, and
# their setters.

-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*(...);
    *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# Preserve all classes that have special context constructors, and the
# constructors themselves.

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

# Preserve all classes that have special context constructors, and the
# constructors themselves.

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve all possible onClick handlers.

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

# Preserve the special fields of all Parcelable implementations.

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.

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

# Preserve annotated Javascript interface methods.

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# The Android Compatibility library references some classes that may not be
# present in all versions of the API, but we know that's ok.
-dontwarn android.support.**

# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}
-dontwarn org.apache.**


# Preserve the special static methods that are required in all enumeration
# classes.

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your application doesn't use serialization.
# If your code contains serializable classes that have to be backward 
# compatible, please refer to the manual.

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
# Removes loggin calls

#-assumenosideeffects class android.util.Log {
#    public static boolean isLoggable(java.lang.String, int);
#    public static int v(...);
#    public static int i(...);
#    public static int w(...);
#    public static int d(...);
#    public static int e(...);
#}
######################################################################################################

# facebook
-keep class com.facebook.** { *; }

#
#play services
#

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

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

#
#otto
#

-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

#
# butterknife
#

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}
#
# retrofit
#

-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

-dontwarn rx.**
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}
-keep class com.ello.GsonModels.** { *; }
-keep class com.ello.data.** { *; }

#
#twitter
-dontwarn twitter4j.**
-keep class twitter4j.** { *; }
#
# comscore
#
-keep class com.comscore.** { *; }

-dontwarn com.comscore.**

-keep class com.google.android.maps.** { *; }
-keep interface com.google.android.maps.** { *; }

#
#okio
#

-dontwarn okio.**

#
# Simple XML
#

-keep public class org.simpleframework.**{ *; } 
-keep class org.simpleframework.xml.**{ *; } 
-keep class org.simpleframework.xml.core.**{ *; } 
-keep class org.simpleframework.xml.util.**{ *; }
-dontwarn org.simpleframework.xml.stream.**
#
# aptitude
#
-dontwarn com.amplitude.**
-keep class com.amplitude.**{*;}

How to keep file to be able use reflection methods on it??

Yarh
  • 4,459
  • 5
  • 45
  • 95
  • Possible duplicate of [Retrieve only static fields declared in Java class](http://stackoverflow.com/questions/3422390/retrieve-only-static-fields-declared-in-java-class) – dotvav Dec 02 '15 at 11:32
  • @dotvav there is another problem in provided post, OP ask to separrate static field, while he had no problem of getting fields. My problem - I dont recieve fiels – Yarh Dec 02 '15 at 11:34
  • 4
    @Yarh Your code works fine: http://ideone.com/RDMiWh – assylias Dec 02 '15 at 11:36
  • 1
    Maybe your unrelated `check(context);` operation fails with an exception and you never get to the point of invoking `getFields()`? Or your classes mismatch and the `SyncKeysConstants` you evaluate at runtime is not the `SyncKeysConstants` whose source code you have posted here. – Holger Dec 02 '15 at 11:37
  • @Holger but I see message "current - true/false" – Yarh Dec 02 '15 at 11:39
  • @Yarh You could try printing `SyncKeysConstants.HOME` before the for loop to confirm that the class really has that field... – assylias Dec 02 '15 at 11:43
  • 1
    @assylias: not helping here as `HOME` is a compile-time constant, thus, you won’t notice if the class is different at runtime. – Holger Dec 02 '15 at 11:44
  • @Yarh: you may check the location of the class `SyncKeysConstants` you are inspecting at runtime and whether it is up-to-date: http://ideone.com/Iu7hcH – Holger Dec 02 '15 at 11:49
  • @Yarh please provide a [minimal complete example](http://stackoverflow.com/help/mcve) that illustrates the problem in a way we can reproduce. I suspect the method is actually working fine just you're not seeing the effect - one possible thing is you're using Log in a wrong way (is that Android's logging? What happens if you use "updateConnection" instead of "field" in the first parameter?) – Jiri Tousek Dec 02 '15 at 11:54
  • @JiriTousek Yest it is android logs. `SyncStatus.setNoConnection(context, value, false);` set global variable, whichi is not change (I can observe variable change, when I call setnoConnection in other places). Thus its not just missing Logs. I updated question with extra log, which checks length of Fields array, it is always 0. – Yarh Dec 02 '15 at 12:13
  • So I suppose, it’s an Android implementation-specific issue. – Holger Dec 02 '15 at 12:17
  • @Holger You actually were right about class was inaccecible. After I turn proguard of, getField returned non zero length field. But approach of geting file path as url and finding it last modified value returnes NPE in both cases – Yarh Dec 02 '15 at 12:19
  • So most likely, ProGuard removed the field as being unused (ordinary Java access to the field doesn’t appear at bytecode level because this is a compile-time constant, so the constant value is used directly). So you may configure ProGuard to not perform this kind of optimization or to exclude this specific field… – Holger Dec 02 '15 at 12:23
  • instead of using `SyncKeysConstants.class.getFields()`, use `SyncKeysConstants.class.getDeclaredFields()` – Gleison Dec 02 '15 at 12:28
  • @Gleison It return same zerolength result. Problem occures cause proguard removes/changes SyncKeysConstants file – Yarh Dec 02 '15 at 12:51

1 Answers1

2

I need to keep not only class but also all its members

-keepclassmembers class com.ello.bases.SyncKeysConstants {
    public *;    
}
Yarh
  • 4,459
  • 5
  • 45
  • 95
  • 1
    I think, when you use a class literal like in `SyncKeysConstants.class.getFields()`, you don’t need to keep the class but only the members. – Holger Dec 02 '15 at 14:00
  • @Holger you are right again:) Only keeping constants is required – Yarh Dec 02 '15 at 14:51