Reputation: 2166
I have a method to create RippleDrawables in code
public class StateApplier {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void add_Ripple(Resources res, StateListDrawable states
, int color, int pressedColor){
Drawable rd = new android.graphics.drawable.RippleDrawable(get_Ripple_ColorSelector(pressedColor)
, new ColorDrawable(color), null);
states.addState(new int[] {}, rd);
}
Which works fine when I run it on a Lollipop, but when I run it on KitKat device, it crashes. Here is the error log.
03-12 21:36:47.734: E/dalvikvm(26295): Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.acme.applib.Render.StateApplier.add_Ripple
03-12 21:36:47.734: W/dalvikvm(26295): VFY: unable to resolve new-instance 149 (Landroid/graphics/drawable/RippleDrawable;) in Lcom/acme/applib/Render/StateApplier;
03-12 21:36:47.734: D/dalvikvm(26295): VFY: replacing opcode 0x22 at 0x0000
03-12 21:36:47.738: W/dalvikvm(26295): VFY: unable to find class referenced in signature (Landroid/graphics/drawable/RippleDrawable;)
03-12 21:36:47.738: W/dalvikvm(26295): VFY: returning Ljava/lang/Object; (cl=0x0), declared Landroid/graphics/drawable/Drawable; (cl=0x0)
03-12 21:36:47.738: W/dalvikvm(26295): VFY: rejecting opcode 0x11 at 0x0004
03-12 21:36:47.738: W/dalvikvm(26295): VFY: rejected Lcom/acme/applib/Render/StateApplier;.create_Ripple (Landroid/content/res/Resources;II)Landroid/graphics/drawable/Drawable;
03-12 21:36:47.738: W/dalvikvm(26295): Verifier rejected class Lcom/acme/applib/Render/StateApplier;
I thought using @TargetApi(Build.VERSION_CODES.LOLLIPOP) would force the the code to be skipped on devices lower than a lollipop. What's strange it that is crashing on activity that does not even call the method add_Ripple() but calls another method in the StateApplier class.
Note I am also using an api check before calling the method
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
add_Ripple(res, states, color, pressedColor);
What would be the appropriate way of referencing classes in newer APIs without it crashing on older devices.
Upvotes: 4
Views: 2159
Reputation: 2166
What I did was create another class called StateApplierLollipop and moved all code dealing with RippleDrawable there, and the code in StateApplier will only call that code in StateApplierLollipop for Lollipop+ devices. This stopped the crashes on KitKat.
public class StateApplierLollipop {
public static void add_Ripple(Resources res, StateListDrawable states
, int color, int pressedColor){
...........
}
}
public class StateApplier{
public static void add_Ripple(Resources res, StateListDrawable states
, int color, int pressedColor){
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
StateApplierLollipop.add_Ripple(....
}
Upvotes: 3