'ViewPager2 dependency confusion
I was about to try ViewPager2 but I don't understand when I saw someone add the following dependency declaration in their gradle file:
implementation 'androidx.viewpager2:viewpager2:1.0.0'
I am using AndroidX and, even without adding this in my app-level gradle file, I still have access to the ViewPager2 class and I am able to add it in my code. Is this line only needed when a project is still using legacy Android support libraries and you just want to use a specific AndroidX component such as ViewPager2?
Solution 1:[1]
If you have visibility of the ViewPager2
classes in your project without adding an explicit dependency on the androidx.viewpager2:viewpager
library, then you have a transitive dependency on the androidx.viewpager2:viewpager
library. It will be the case that one of the libraries which you have an explicit dependency on has a dependency on the androidx.viewpager2:viewpager
library.
You can run the dependencies
Gradle task to see your application's dependency graph and to work out from it which of your explicit dependencies is bringing in the androidx.viewpager2:viewpager
library. In my project, when I run the gradle :app:dependencies
task, I see in the dependency graph that it's my app's dependency on com.google.android.material:material
which brings in the androidx.viewpager2:viewpager
library.
If you prefer, you can run the dependencyInsight
Gradle task instead to get a more direct answer to the question. For example, in my project, if I run gradle :app:dependencyInsight --configuration debugCompileClasspath --dependency viewpager2
, then I see output as follows:
androidx.viewpager2:viewpager2:1.0.0
\--- com.google.android.material:material:1.4.0
\--- debugCompileClasspath
Solution 2:[2]
To use ViewPager2, add the following AndroidX dependency to your project's build.gradle file:
dependencies { implementation "androidx.viewpager2:viewpager2:1.0.0" }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Farzaneh Hoseini |