【问题标题】:Android Proguard issues with Lamdas in library module库模块中的 Lamdas 的 Android Proguard 问题
【发布时间】:2018-10-06 17:38:38
【问题描述】:

我有一个 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 与它们一起使用。关于如何解决这个问题的任何想法?

【问题讨论】:

  • 你得到解决方案了吗

标签: android proguard


【解决方案1】:

你也保留枚举吗? 因为我们不想正确剥离枚举。

如果不行,试试下面的

-keepclassmembers enum * { *; } -keep public class com.example.mymodule.utils.AnimationUtils { *; } -keep public interface com.example.mymodule.interfaces.SimpleAnimationListener { *; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多