1

I'm trying to marquee title in toolbar, but the marquee is not working if I build it with proguard enable.



    Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
    f.setAccessible(true);

    TextView titleTextView = (TextView) f.get(toolbar);
    titleTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    titleTextView.setMarqueeRepeatLimit(-1);
    titleTextView.setSelected(true);


Seem like "mTitleTextView" is obfuscated by proguard.

 java.lang.NoSuchFieldException: mTitleTextView
        at java.lang.Class.getDeclaredField(Class.java:631)

But it doesn't work, any idea?

jefry jacky
  • 799
  • 7
  • 11

2 Answers2

1

You can instruct proguard not to touch private fields with following syntax:


    -keepclassmembers class android.widget.Toolbar {
        private android.widget.TextView mTitleTextView;   
    }

For the toolbar from support library:


    -keepclassmembers class android.support.v7.widget.Toolbar {
        private android.widget.TextView mTitleTextView;
     }

See this question for more details.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • Helpful: (https://stackoverflow.com/questions/23365021/how-to-tell-proguard-to-keep-private-fields-without-specifying-each-field) – ColinWa Nov 08 '18 at 11:33
1

Avoid doing reflection. Private field names can change at any time, not to mention that using reflection is slow! Also, from Android 9 Pie (API 28), they're locking down API access so it's not a long-term solution.

Instead, define your own Toolbar in a layout instead. See this question and this answer.

Tim Kist
  • 1,164
  • 1
  • 14
  • 38