Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Proguard is shrinking position of viewPager

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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading