如何修复:ERROR: Failed to resolve: 'androidx.cardview:cardview:1.0.0'

4
我正在使用androidx库开发一款新的Android应用程序,并且我需要在我的布局中使用cardview。
所以我尝试将cardview库添加到我的项目中,但是我遇到了以下错误ERROR: Failed to resolve: cardview 我查看了“Failed: com.android.support:cardview-v7:26.0.0 android”的答案,但这并没有帮助我
例如:
  • jcenter()之前添加google()
  • maven { url“ https://maven.google.com” } 添加到repositories 中

build.gradle(project)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.chavosh.porterageapp"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    testImplementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'

    // recycler view
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04'

    implementation 'androidx.cardview:cardview:1.0.0'

}

build.gradle(moudle)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven { url "https://maven.google.com" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle.properties有以下两行

android.useAndroidX=true
android.enableJetifier=true

error pic


请问您能否清理构建项目或使Android Studio无效并重新启动? - Mehul Kabaria
尝试在您的gradle.properties文件中添加android.useAndroidX=true android.enableJetifier=true。另外,请查看此链接:https://stackoverflow.com/questions/52181574/android-failed-to-resolve-cardview-v7 还请查看https://developer.android.com/jetpack/androidx/migrate,它可以为您提供更多帮助。 - Ajay Mehta
@MehulKabaria 谢谢你的回答。我尝试重新启动了Android Studio,但是什么也没有发生。 - hassan moradnezhad
@AjayMehta-Rlogical,我刚刚编辑了问题,谢谢。但是我的gradle.properties文件中有android.useAndroidX=true android.enableJetifier=true - hassan moradnezhad
3个回答

12

只需移除它们!

只要这三个库,您就可以同时使用 RecyclerView, CardView, 还有一个额外的: ConstraintLayout

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'androidx.core:core-ktx:1.1.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

没问题 :) 我也曾经挣扎过很多。@hassanmoradnezhad 问题是你手动转换成了androidX还是使用了AndroidStudio转换工具?如果是后者,你可能需要向Google报告一个错误,指出该转换器没有从build.gradle文件中删除这些行。 - Luca Nicoletti

0

是的,现在材料卡片视图在材料库中可用,并提供更多选项。但我正在寻找 androidx.cardview:cardview - hassan moradnezhad

-1

在Palette/layout中的CardView中选择Tick download以解决此问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接