Should I add only one either "androidx.preference:preference:1.1.1" or "androidx.preference:preference-ktx:1.1.1" to build.gradle?

Viewed 1165

In order to use androidx.preference, I should add the dependencies to build.gradle

Image A is from https://developer.android.com/jetpack/androidx/releases/preference

1: I don't think it's good, I should use Code A1 or Code A2, right?

2: And more, Image B is from https://developer.android.com/google/play/billing/getting-ready
I think it's good. I should use Code B1 if my project mix java and kotlin, I should use Code B2 if my project only write by Kotlin, right?

Image A

enter image description here

Code A1

dependencies {
    def preference_version = "1.1.1"

    // Java language implementation
    implementation "androidx.preference:preference:$preference_version"   
}

Code A2

dependencies {
    def preference_version = "1.1.1" 
    // Kotlin
    implementation "androidx.preference:preference-ktx:$preference_version"
}

Image B enter image description here

Code B1

dependencies {
    def billing_version = "3.0.0"
    implementation 'com.android.billingclient:billing:$billing_version'
}

Code B2

dependencies {
    def billing_version = "3.0.0"
    implementation 'com.android.billingclient:billing-ktx:$billing_version'
}
1 Answers

The KTX artifacts usually contain Kotlin extensions to the original artifact.

This means that androidx.preference:preference-ktx already depends on androidx.preference:preference and you don't need to include both separately.

If you don't intend to use the Kotlin extensions, you don't need to use the androidx.preference:preference-ktx artifact. You'll find the documentation for these Kotlin extensions on developer.android.com/reference/kotlin/androidx/.

So a rule of thumb: use the KTX artifact whenever you use Kotlin in your project.

Related