7

i have a pojo class

run this code

Field[] fields = clazz.getDeclaredFields();

i got a field under Android Studio IDE :

its type is interface com.android.tools.fd.runtime.IncrementalChange its name is $change

My Android Studio version is 2.0 Preview 4

the pojo class which i definded by myself didn't have $change field

when i run the code in eclipse, it works normal.

where did the field come from? how can i avoid this field , is there some setting in Android Studio ?

Jackie Cheng
  • 190
  • 1
  • 9
  • This is not Android-Studio or even Android specific. `getDeclaredFields()` returns synthetic fields in regular Java as well. It is Java SE standard behavior. (My guess is that in Eclipse the class you were looking at didn't have any synthetic fields.) – Stephen C Aug 02 '22 at 14:36

3 Answers3

9

Most likely this field is added in order to support the Instant Run feature added in Android Studio 2.0, and will not appear if you turn off Instant Run.

yole
  • 92,896
  • 20
  • 260
  • 197
  • Great! in setting search Instant Run ,there 's a checkbox default is checked. when i unchecked it , the field disappared! thanks! – Jackie Cheng Jan 07 '16 at 10:16
9

Instead of turning off instant run we can resolve this issue by utilising synthetic modifier check. 'com.android.tools.fd.runtime.IncrementalChange' is synthetic so we can check whether the field is synthetic using isSynthetc method.

Field[] fields = objClass.getFields();
for (Field field : fields) {
            String name = field.getName();
            Object value;

            if(field.isSynthetic()){
                continue;
            }
          //add your code here
            }
diordna
  • 550
  • 5
  • 14
  • doesnt' work for me. ```java for (Field field : raw.getDeclaredFields()) { if (field.isSynthetic()) { continue; } field.setAccessible(true); ``` – Seth Apr 20 '17 at 15:59
0

I think diordna answer is best link。install run is Android Studio new function,I won't close it。

I use JsonTool link in my sdk library ,when I run my app with androidStudio2.2 JsonTooll.objectToJson() give me a bad json String ,I add code
if (name.contains("this$") || field.isSynthetic()) continue;

solve it

Community
  • 1
  • 1
burulangtu
  • 341
  • 2
  • 7