Android Proguard 在库模块中与 Lamda 存在问题



我有一个安卓项目,它依赖于一个库模块。我的模块中有一个这样的接口:

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)

在我的映射中.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 { *; }

相关内容

最新更新