为什么'androidx.constraintlayout:constraintlayout'可以省略?

hat*_*ata 3 android android-gradle-plugin android-constraintlayout

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'我注意到,尽管我在应用程序中使用 ConstraintLayout,但我可以注释掉而不会出现任何错误。

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    // implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.3'
}
Run Code Online (Sandbox Code Playgroud)

为什么可以呢?com.google.android.material 库是否也包含constraintLayout?

Mar*_*ler 5

它确实来自com.google.android.material:material:1.3.0- 但当您没有明确声明它时,您将根据版本构建2.0.1- 而不是当前版本2.0.4

只需运行即可./gradlew app:dependencies查看它将解析不同的版本。


免责声明:我不太确定为什么这些依赖项相当过时
......但通常可以排除它们,因此必须提供它们:

implementation ("com.google.android.material:material:1.3.0") {
    exclude group: "androidx.annotation"       // 1.0.1 < 1.1.0
    exclude group: "androidx.appcompat"        // 1.1.0 < 1.2.0
    exclude group: "androidx.constraintlayout" // 2.0.1 < 2.0.4
    exclude group: "androidx.core"             // 1.2.0 < 1.3.2
    exclude group: "androidx.fragment"         // 1.0.0 < 1.3.0
    exclude group: "androidx.lifecycle"        // 2.0.0 < 2.3.0
    exclude group: "androidx.recyclerview"     // 1.0.0 < 1.1.0
    exclude group: "androidx.transition"       // 1.2.0 < 1.4.0
}

implementation "androidx.annotation:annotation:1.1.0"
implementation "androidx.appcompat:appcompat:1.2.0"
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
implementation "androidx.core:core:1.3.2"
implementation "androidx.fragment:fragment:1.3.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.3.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.transition:transition:1.4.0"
Run Code Online (Sandbox Code Playgroud)

至少 Maven Central 是这么说的;我假设没有副作用。