DylanCaiCoding / ViewBindingKTX

The most comprehensive utils of ViewBinding. (最全面的 ViewBinding 工具,支持 Kotlin 和 Java 用法,支持 BRVAH,支持封装到基类,支持 DataBinding,支持选择是否使用反射)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

报错,求指教

liguangze opened this issue · comments

image
你好我在基类上写上了,为什么报错呢
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
Class cls = (Class) type.getActualTypeArguments()[0];
try {
Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
viewBinding = (T) inflate.invoke(null, getLayoutInflater());
setContentView(viewBinding.getRoot());
} catch (Exception e) {
e.printStackTrace();
}

2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: java.lang.NoSuchMethodException: inflate [class android.view.LayoutInflater]
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at java.lang.Class.getMethod(Class.java:624)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at java.lang.Class.getDeclaredMethod(Class.java:586)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at com.akax.haofangfa.pen.base.BaseActivity.onCreate(BaseActivity.java:68)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at com.akax.haofangfa.pen.ui.MainActivity.onCreate(MainActivity.java:23)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.Activity.performCreate(Activity.java:6279)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2499)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2606)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1444)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.os.Looper.loop(Looper.java:148)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5647)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
2023-04-06 15:44:54.255 6177-6177/com.akax.haofangfa.pen W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

proguard-rules.pro 中加上这些

AGP 8以上,因为android.enableR8.fullMode会强制开启,还需要加上以下三行

-keep class * implements androidx.viewbinding.ViewBinding {*;}

-keepclassmembers class * implements androidx.viewbinding.ViewBinding {
public static ** bind(***);
public static ** inflate(...);
}

-keep,allowobfuscation,allowshrinking class 你的包名. BaseBindingActivity