Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大家如果遇到开启混淆后,奔溃的问题加上如下代码即可解决问题 #63

Open
tanranran opened this issue Mar 17, 2023 · 6 comments

Comments

@tanranran
Copy link

tanranran commented Mar 17, 2023

奔溃代码:
`
class TestActivity : BaseViewBindingActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    title = "提交成功"
}

}
`
未奔溃代码:

`
class TestActivity : BaseViewBindingActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    title = "提交成功"
    getBinding().root.setBackgroundColor(Color.RED)
}

}
`
原因:
使用baseViewBind类的时候,如果未调用过getBinding()中的属性,在打release包时,编译器优化会优化掉泛型中的ViewBind类[因为在编译器看来该ViewBind中的类未被使用],导致找不到ViewBind类

解决办法:
-keep class * implements androidx.viewbinding.ViewBinding {*;}

@AIlll
Copy link

AIlll commented May 30, 2023

AGP 8.0,默认开启android.enableR8.fullMode,在fullMode下,就算配置了混淆规则也不行 @DylanCaiCoding 有空帮忙看看

@klxiaoniu
Copy link

AGP 8.0,默认开启android.enableR8.fullMode,在fullMode下,就算配置了混淆规则也不行 @DylanCaiCoding 有空帮忙看看

遇到同样问题,AGP 8.0.1,即使配置了规则依然报错, @DylanCaiCoding 麻烦大佬看看

@tanranran
Copy link
Author

AGP 8.0,默认开启android.enableR8.fullMode,在fullMode下,就算配置了混淆规则也不行 @DylanCaiCoding 有空帮忙看看

@AIlll @klxiaoniu

试试这个,我亲测好使

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

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

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

@klxiaoniu
Copy link

AGP 8.0,默认开启android.enableR8.fullMode,在fullMode下,就算配置了混淆规则也不行 @DylanCaiCoding 有空帮忙看看

@AIlll @klxiaoniu

试试这个,我亲测好使

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

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

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

问题已解决,十分感谢!

@ChawLoo
Copy link

ChawLoo commented Jan 24, 2024

其实把R8的完整模式关了也无伤大雅

@tanranran
Copy link
Author

@ChawLoo 项目大了,对包体积有要求

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants