Herman Cheung
Herman Cheung

Reputation: 324

Android Studio 3.2 does not aware of Jetifier

Here is a little piece of code in an Android app, after "Migrate to AndroidX..." on Android Studio 3.2.

fun configureViewPager( pageIndicator: PageIndicator, pager: androidx.viewpager.widget.ViewPaager) { ... pageIndicator.setViewPager(pager) ... }

Variable pageIndicator is a reference of PageIndicator of https://github.com/JakeWharton/ViewPagerIndicator. It has a method setViewPager(android.support.v4.view.ViewPager view) to connect the object to a viewPager in layout.

On the other hand, variable pager is a view pager of AndroidX. So it is obvious that there is a mismatch by passing an AndroidX's androidx.viewpager.widget.ViewPaager to a method of android.support.v4.view.ViewPager.

However Jetifier of Android SDK can detect that and transform the androidx.viewpager.widget.ViewPaager reference to android.support.v4.view.ViewPager in the library at build time, so it compile successfully.

The problem is, the line of code above still trigger an error in Android Studio code editor, and a read underline is shown. Is it a bug (or lack of feature) that Android Studio does not aware what Jetifier is going to do? Or there are some setting to make it aware of that?

In the mean time, I make the problem a little more tolerable by creating a Kotlin extension function:

fun PageIndicator.setViewPagerX(pager: ViewPager /* AndroidX's */) { setViewPager(pager) }

Then the line of code mentioned above is changed to

pageIndicator.setViewPagerX(pager)

so that the same error all over the code base occurs once only.

Any idea to make it better?

Upvotes: 0

Views: 738

Answers (1)

Bojan Radivojevic
Bojan Radivojevic

Reputation: 737

If you have enabled Jetifier and it doesn't seem to do anything,
try deleting .idea/libraries directory and then do a gradle sync.
File -> Sync Project with Gradle files

It seems that if Jetifier is enabled after Android Studio has already downloaded some dependencies,
it won't properly detect and convert legacy dependencies.

Upvotes: 1

Related Questions