androidbroadcast / ViewBindingPropertyDelegate

Make work with Android View Binding simpler
https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719
Apache License 2.0
1.42k stars 102 forks source link

Problem with ProGuard in custom view #38

Closed Anton111111 closed 3 years ago

Anton111111 commented 3 years ago

You can see issue with proguard on this example. If set minifyEnabled to true then app will crash on start: https://github.com/Anton111111/NavigationLikeInstagram/blob/vbpd-bug/app/src/main/java/com/anton111111/navigation/ui/CustomView.kt

If change minifyEnabled to false app started without problem: https://github.com/Anton111111/NavigationLikeInstagram/blob/vbpd-bug/app/build.gradle

alaegin commented 3 years ago

@Anton111111 Try to add this rule to your proguard config. It seems that the original distribution lucks of some rules.

-keep class * implements androidx.viewbinding.ViewBinding {
  public static *** bind(android.view.View);
  public static *** inflate(android.view.LayoutInflater);
  public static *** inflate(android.view.LayoutInflater, android.view.ViewGroup, boolean);
}
Anton111111 commented 3 years ago

@alaegin , doesn't work with this rules too

kirich1409 commented 3 years ago

@Anton111111 , please send your crash log?

Anton111111 commented 3 years ago

@kirich1409 ,i get the following exception on the sample (to check sample see first message): 2021-02-21 11:09:32.132 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.anton111111.navigation, PID: 7283 android.view.InflateException: Binary XML file line #9 in com.anton111111.navigation:layout/auth_fragment: Binary XML file line #9 in com.anton111111.navigation:layout/auth_fragment: Error inflating class com.anton111111.navigation.ui.CustomView Caused by: android.view.InflateException: Binary XML file line #9 in com.anton111111.navigation:layout/auth_fragment: Error inflating class com.anton111111.navigation.ui.CustomView Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at android.view.LayoutInflater.createView(LayoutInflater.java:852) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082) at android.view.LayoutInflater.inflate(LayoutInflater.java:680) at android.view.LayoutInflater.inflate(LayoutInflater.java:532) at androidx.fragment.app.Fragment.l0(:1922) at androidx.fragment.app.Fragment.L0(:2950) at b.k.b.s.f(:515) at b.k.b.s.m(:282) at b.k.b.m.Z(:2177) at b.k.b.m.T0(:2094) at b.k.b.m.W(:1990) at b.k.b.m$g.run(:524) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.NoSuchMethodException: com.anton111111.navigation.databinding.CustomViewBinding.inflate [class android.view.LayoutInflater, class android.view.ViewGroup] at java.lang.Class.getMethod(Class.java:2072) at java.lang.Class.getMethod(Class.java:1693) at c.a.a.h.f.a(:60) at c.a.a.h.e.b(:21) at c.a.a.f$b.a(:37) at c.a.a.f$b.j(Unknown Source:2) at c.a.a.d.b(:33) at c.a.a.d.a(:25) at com.anton111111.navigation.ui.CustomView.getViewBinding(Unknown Source:7) at com.anton111111.navigation.ui.CustomView.a(:38) at com.anton111111.navigation.ui.CustomView.<init>(:30) at com.anton111111.navigation.ui.CustomView.<init>(:20) at com.anton111111.navigation.ui.CustomView.<init>(:19) at java.lang.reflect.Constructor.newInstance0(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:343)  at android.view.LayoutInflater.createView(LayoutInflater.java:852)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)  at android.view.LayoutInflater.inflate(LayoutInflater.java:680)  at android.view.LayoutInflater.inflate(LayoutInflater.java:532)  at androidx.fragment.app.Fragment.l0(:1922)  at androidx.fragment.app.Fragment.L0(:2950)  at b.k.b.s.f(:515)  at b.k.b.s.m(:282)  at b.k.b.m.Z(:2177)  at b.k.b.m.T0(:2094)  at b.k.b.m.W(:1990)  at b.k.b.m$g.run(:524)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Anton111111 commented 3 years ago

@kirich1409 , @alaegin ,i've fixed my problem with small tuned of @alaegin advice. I expanded the advice by adding a line: "public static *** inflate(android.view.LayoutInflater, android.view.ViewGroup);"

My complet proguard rule:

-keep class * implements androidx.viewbinding.ViewBinding { public static bind(android.view.View); public static inflate(android.view.LayoutInflater); public static inflate(android.view.LayoutInflater, android.view.ViewGroup); public static inflate(android.view.LayoutInflater, android.view.ViewGroup, boolean); }

kirich1409 commented 3 years ago

Will be fixed in 1.4.3

Anton111111 commented 3 years ago

@kirich1409 , on last release (1.4.4) still doesn't works. To fix it i remove "allowshrinking" from proguard rules.

kirich1409 commented 3 years ago

Will be fixed in the next release