我有一个 android 项目,它依赖于一个库模块。我的模块中有一个这样的界面:

   public interface SimpleAnimationListener extends Animation.AnimationListener {
    @Override
    default void onAnimationStart(Animation animation) {
        onAnimation(Type.START);
    }

    @Override
    default void onAnimationEnd(Animation animation) {
        onAnimation(Type.END);
    }

    @Override
    default void onAnimationRepeat(Animation animation) {
        onAnimation(Type.REPEAT);
    }

    void onAnimation(Type type);

    enum Type {
        START,
        END,
        REPEAT
    }
}

用法:
public static void fadeViewOut(@NonNull View view, int duration) {
    view.setVisibility(View.VISIBLE);
    AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
    anim.setDuration(duration);
    anim.setAnimationListener((SimpleAnimationListener) type -> {
        if (type == SimpleAnimationListener.Type.END) {
            view.setVisibility(View.INVISIBLE);
        }
    });
    view.startAnimation(anim);
}

每当我在我的应用程序项目中使用此界面时。我得到以下异常:
FATAL EXCEPTION: main
    java.lang.AbstractMethodError: abstract method not implemented
        at Zt.onAnimationStart(lambda)

在我的 mappings.txt 我有这个:
com.example.mymodule.interfaces.SimpleAnimationListener -> Wt:
<...>
com.example.mymodule.utils.-$$Lambda$AnimationUtils$rrH5aPAPZuoC__1-9DiD10TFdgY -> Zt:
        android.view.View f$0 -> a

尝试将以下内容添加到 proguard-rules.pro
-keep public class com.example.mymodule.utils.AnimationUtils
-keep public interface com.example.mymodule.interfaces.SimpleAnimationListener

然后在我的映射中我会得到:
com.example.mymodule.interfaces.SimpleAnimationListener -> com.example.mymodule.interfaces.SimpleAnimationListener:
<...>
com.example.mymodule.utils.-$$Lambda$AnimationUtils$rrH5aPAPZuoC__1-9DiD10TFdgY -> Yt:
    android.view.View f$0 -> a

这也会失败。但错误会指向 Yt 。如果我停止使用覆盖某些方法的界面,则应用程序正在运行,但由于原始界面有多种方法,因此我无法将 lamdas 与它们一起使用。关于如何解决这个问题的任何想法?

最佳答案

你也保持枚举吗?
因为我们不想剥夺枚举权利。

如果没有尝试以下
-keepclassmembers enum * { *; }-keep public class com.example.mymodule.utils.AnimationUtils { *; }-keep public interface com.example.mymodule.interfaces.SimpleAnimationListener { *; }

关于库模块中 Lambdas 的 Android Proguard 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52681685/

10-11 22:44
查看更多