NineOldAndroids ObjectAnimators don't work with ProGuard and signed apk
Asked Answered
E

2

5

I am using NineOldAndroids' ObjectAnimators to fade in Android Map v2 markers with the following code:

mMarkerSelected = mMap.addMarker(new MarkerOptions()
        .position(location.getLatLng())
        .title(location.getName())
        .snippet(location.getId())
        .icon(BitmapDescriptorFactory.defaultMarker(location.getMarkerHue())));
mMarkerSelected.setAlpha(0.0f);
ObjectAnimator.ofFloat(mMarkerSelected, "alpha", 0.0f, 1.f)
                .setDuration(300).start();

This works perfectly with debuggable versions of the apk.

However, when I sign my apk and use ProGuard, suddenly marker doesn't fade in. My guess is that the alpha attribute has been obfuscated so that passing "alpha" into ObjectAnimator.ofFloat doesn't match up with the obfuscated alpha attribute of the Marker. How can I get the animation to work when using ProGuard?

Just for completeness, this is the only contents of my proguard-rules.txt

-dontwarn com.squareup.okhttp.**
Ers answered 1/3, 2014 at 21:22 Comment(0)
W
11

It uses reflection so you need your method names intact i.e. setAlpha(), something like this in your proguard config:

 # This is due to ObjectAnimator using reflection to access get/sets
 -keep class com.your.package.ClassThatUsesObjectAnimator { *; }
Wensleydale answered 1/3, 2014 at 21:25 Comment(2)
Thanks! I went with -keepclassmembers class com.google.android.gms.maps.model.Marker { *; }. Can you update the answer, or should I instead keep my personal class that uses ObjectAnimator?Ers
You can do either, personally I would have a class to control the animation but not proguarding the marker is also fineWensleydale
I
2

Nineolddroids library is open sourced already so running proguard on it is pointless.

If you have a choice between obfuscating your own source file or a 3rd party open sourced library it would be wise to choose your own source file.

Add these lines to your proguard-project.txt and your application will work fine.

## Nineolddroid related classes to ignore

-keep class com.nineoldandroids.animation.** { *; }
-keep interface com.nineoldandroids.animation.** { *; }
-keep class com.nineoldandroids.view.** { *; }
-keep interface com.nineoldandroids.view.** { *; }
Ium answered 20/12, 2014 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.