I am using AndroidX viewpager having multiple clickable views on it.
Everything is working perfectly in debug the application when proguard is not enabled but when I enable minifyEnabled true for release application view of Viewpager is not getting changed.
Looking for proguard rules to keep viewpager position?
Like I have a button and animation, whenever I click the button animation should play.
private fun getCurrentView(viewPager: ViewPager): View? {
try {
val currentItem = viewPager.currentItem
for (i in 0 until viewPager.childCount) {
val child = viewPager.getChildAt(i)
val layoutParams = child.layoutParams as ViewPager.LayoutParams
val f: Field =
layoutParams.javaClass.getDeclaredField("position") //NoSuchFieldException
f.isAccessible = true
val position = f.get(layoutParams) as Int //IllegalAccessException
if (!layoutParams.isDecor && currentItem == position) {
return child
}
}
} catch (e: NoSuchFieldException) {
Log.e("TAG", e.toString())
} catch (e: IllegalArgumentException) {
Log.e("TAG", e.toString())
} catch (e: IllegalAccessException) {
Log.e("TAG", e.toString())
}
return null
}
>Solution :
The solution for this is you need to protect the ViewPager.LayoutParams#position from obfuscation and shrink with following ProGuard rules if you enable ProGuard or R8 will keep your position.
-keep class androidx.viewpager.widget.ViewPager$LayoutParams { int position; }
Happy Coding