❝
The gradle dependencies of jetpack components are often confusing when learning and using them, so here we sort out the dependencies and transitive relationships of the main components of Jetpack
❞
jetpcak
Component source code address- The query
jetpcak
Component Version:Google’s Maven Repository - View the dependency tree: Execute in the project root directory
./gradlew :app:dependencies
“You can skip to the final section to see the summary“
For details on how activities and fragments change on androidx, see this article
Appcompat
The introduction of
dependencies {
def appcompat_version = "1.1.0"
implementation "androidx.appcompat:appcompat:$appcompat_version"
// For loading and tinting drawables on older versions of the platform
implementation "androidx.appcompat:appcompat-resources:$appcompat_version"
}
Copy the code
Rely on the tree
Transfer rely on
Androidx. Annotation: the annotation: 1.1.0
Androidx. Core: the core: 1.1.0
Androidx. Cursoradapter: cursoradapter: 1.0.0
Androidx. Fragments: fragments: 1.1.0
Androidx. Appcompat: appcompat - resources: 1.1.0
Androidx. Drawerlayout: drawerlayout: 1.0.0
Androidx. Collection: collection: 1.0.0
❝
“
appcompat
Is introduced by defaultfragment
Library, if you want to use a newer versionfragment
Library, which can be referenced separately“❞
❝
Appcompat build.gradle
❞
Fragment
The introduction of
dependencies {
def fragment_version = 1.2.2, ""
// Java language implementation
implementation "androidx.fragment:fragment:$fragment_version"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
// Testing Fragments in Isolation
implementation "androidx.fragment:fragment-testing:$fragment_version"
}
Copy the code
❝
⚠ ️ Note: The Kotlin dependant libraries of this version (fragment-ktx,fragment-testing) target Java 8 programming language bytecode. Please read Use Java 8 language features to learn how to use it in your project.
❞
Rely on the tree
Transfer rely on
Org. Jetbrains. Kotlin: kotlin - stdlib: 1.3.50
Androidx. Activity: activity - KTX: 1.1.0
Androidx. Core: the core - KTX: 1.1.0
Androidx. Collection: collection - KTX: 1.1.0
Androidx. Lifecycle: lifecycle - livedata - core - KTX: 2.2.0
Androidx. Lifecycle: lifecycle - viewmodel - KTX: 2.2.0
❝
“
fragment
Libraries are introduced by defaultactivity
core-ktx
lifecycle-livedata-core-ktx
lifecycle-viewmodel-ktx
库“Fragment build. Grdle source address
❞
Activity
The introduction of
dependencies {
def activity_version = "1.1.0"
// Java language implementation
implementation "androidx.activity:activity:$activity_version"
// Kotlin
implementation "androidx.activity:activity-ktx:$activity_version"
}
Copy the code
❝
⚠ ️ Note: The Kotlin dependant libraries of this version (activity-ktx) target Java 8 programming language bytecode. Please read Use Java 8 language features to learn how to use it in your project.
❞
Rely on the tree
Depend on the transfer
Org. Jetbrains. Kotlin: kotlin - stdlib: 1.3.50
Androidx. Core: the core - KTX: 1.1.0
Androidx. Lifecycle: lifecycle - runtime - KTX: 2.2.0
Androidx. Lifecycle: lifecycle - viewmodel - KTX: 2.2.0
❝
Activity build.gradle source address
❞
Core
The introduction of
dependencies {
def core_version = "1.2.0"
// Java language implementation
implementation "androidx.core:core:$core_version"
// Kotlin
implementation "androidx.core:core-ktx:$core_version"
// To use RoleManagerCompat
implementation "Androidx. Core: the core - role: 1.0.0 - alpha01"
}
Copy the code
Rely on the tree
Lifecycle
The introduction of
dependencies {
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0."
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" // optional - ReactiveStreams support for LiveData implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version" // optional - Test helpers for LiveData testImplementation "androidx.arch.core:core-testing:$arch_version" } Copy the code
❝
- ⚠️ “lifecycle- Extensions deprecated, if using LifecycleService please rely on lifecycle-service; If using ProcessLifecycleOwner please rely on lifecycle-process. Lifecycle – ExtensionSL will not be available in version 2.3.0”
- “After 2.1.0
ViewModelProviders.of()
Is rejected. You can be inFragmentActivity
orFragment
useViewModelProvider(ViewModelStoreOwner)
Constructor to achieve the same function. (Fragment
Library 1.2.0 or above)“❞
Rely on the tree
Navigation
The introduction of
dependencies {
def nav_version = "2.3.0 - alpha02"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}
Copy the code
Rely on the tree
Paging
The introduction of
dependencies {
def paging_version = 2.1.1 ""
implementation "androidx.paging:paging-runtime:$paging_version" // For Kotlin use paging-runtime-ktx
// alternatively - without Android dependencies for testing
testImplementation "androidx.paging:paging-common:$paging_version" // For Kotlin use paging-common-ktx
// optional - RxJava support
implementation "androidx.paging:paging-rxjava2:$paging_version" // For Kotlin use paging-rxjava2-ktx
}
Copy the code
Rely on the tree
Room
The introduction of
dependencies {
def room_version = 2.2.4 ""
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers testImplementation "androidx.room:room-testing:$room_version" } Copy the code
❝
⚠ ️ Note: For Kotlin-based apps, make sure you use kapt instead of annotationProcessor. You should also add the kotlin-kapt plugin.
❞
Rely on the tree
conclusion
Release notes
❝
The androidx library follows strict semantic versioning. The version string (for example, 1.0.1-beta02) contains three digits representing the major level, minor level, and fix level. Pre-release releases also have a suffix that specifies pre-release phases (Alpha, Beta, release candidate) and release numbers (01, 02, etc.).
❞
Each version of a library goes through three pre-release phases before it becomes a stable release. The criteria for each pre-release phase are as follows:
“Alpha“
- The Alpha version is stable, but may be incomplete.
- Apis can be added, removed, or changed while the version is in Alpha.
“Beta“
- The Beta is stable and features a fully functional Surface API. They can be put to practical use, but may contain errors.
- Experimental compiler features (such as @useexperimental) are not available in the Beta.
- Dependencies for other libraries must be Beta, RC, or stable. Alpha version dependencies are not allowed.
“Release Candidate (RC)“
- Release candidates are future stable releases.
- This release may contain important fixes provided at the last minute.
- This version of the Surface API cannot be changed.
- Dependencies for other libraries can only be RC or stable.
A library can have multiple versions at the same time. Each release has a different release phase. For example, while stable versions of Androidx. activity can be 1.0.0, there may also be versions 1.0-beta02 and 2.0.0-alpha01.
Use of kotlin coroutines
❝
ViewModel LiveData Activity Fragment Service can use coroutines
❞
- for
ViewModelScope
, please use theAndroidx. Lifecycle: lifecycle - viewmodel - KTX: 2.1.0 - beta01
Or later. - for
LifecycleScope
, please use theAndroidx. Lifecycle: lifecycle - runtime - KTX: 2.2.0 - alpha01
Or later. - for
liveData
, please use theAndroidx. Lifecycle: lifecycle - livedata - KTX: 2.2.0 - alpha01
Or later.
class MyViewModel: ViewModel() {
init {
viewModelScope.launch {
// Coroutine that will be canceled when the ViewModel is cleared.}}}Copy the code
class MyFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?). {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
val params = TextViewCompat.getTextMetricsParams(textView)
val precomputedText = withContext(Dispatchers.Default) {
PrecomputedTextCompat.create(longTextContent, params)
}
TextViewCompat.setPrecomputedText(textView, precomputedText)
}
}
}
Copy the code
val user: LiveData<User> = liveData { val data = database.loadUser() // loadUser is a suspend function. emit(data)}Copy the code
dependencies
-
Libraries with -ktx have kotlin’s features, with many commonly used extension functions
-
Libraries with -ktx depend on the Java version; libraries with -ktx and Java versions can be used separately
-
Appcompat library contains fragmnet, Fragment contains activity, when you introduce androidX AppCompat library you can use AndroidX Fragment and AndroidX Activity
-
Due to the update speed of the stable version of AppCompat, you can choose to use AndroidX Fragment/Activity alone
-
AppCompatActivity inherits from FragmentActivity, which can use fragments directly
-
The DataBinding ViewBinding relies on the Android Build Gradle plugin without introducing additional dependencies
-
Androidx Fragment/Activity are dependent on ViewModel and LiveData, you can directly use
-
The full functionality to use ViewModel and LiveData needs to be introduced separately, both under the Lifecycle family
-
Lifecycle – Extensions are deprecated, do not use them
-
If you want to use a Service that implements LifecycleOwner, you need to introduce lifecycle- Service
About me
I am a Fly_with24
-
The Denver nuggets
-
Jane’s book
-
Github
Welcome to follow my public number