我正在尝试将CardView作为正式示例添加到我的活动中。 但这是行不通的。
如果我直接运行项目,则会导致java.lang.ClassNotFoundException:android.support.v7.widget.CardView。
@Blackbelt,其21.0.3
您应该将其添加到您的build.gradle中:
dependencies {
...
implementation 'com.android.support:cardview-v7:21.+'
implementation 'com.android.support:recyclerview-v7:21.+'
...
}
非常感谢你! 我会在5分钟内接受您的回答:)
注意:确保将其添加到正确的build.gradle文件中,对我而言,它是标记为"模块:应用程序"而不是"项目:项目名称"的文件。
当您需要像这样的依赖项时,此页面很好用:gradleplease.appspot.com
对于api 23,应像com.android.support:cardview-v7:23.+和com.android.support:recyclerview-v7:23.+一样进行修订
确保在此之后清理项目(构建)。
谢谢! 任何想法如何找到最新的字符串? 我难以找到API 26的正确字符串。
写我的答案以提供:
1) updated version for compileSdkVersion 27.
2) usage of implementation instead of compile - What's the difference between implementation and compile in Gradle?.
3) a complete version number to heed the Avoid using + in version numbers; can lead to unpredictable and unrepeatable builds.., warning shown by Android Studio.
4) a visual for location of build.gradle (Module:app).
5) And because I was not able to write a comment below @Eun's answer.
您需要将implementation 'com.android.support:cardview-v7:27.1.1'粘贴到位于以下位置的build.gradle [Module:app]中:MyFirstApplication -> app -> build.gradle
粘贴以下依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
...
}
要从Android Studio本身打开,请执行以下操作:(从左侧开始)1:Project ->选择Android视图-> Gradle脚本-> build.gradle(模块:app)
那是:
(如果未显示下面的图像,则可以在此处找到图像。
dependencies {
...
compile 'com.android.support:cardview-v7:26.+'
compile 'com.android.support:recyclerview-v7:26.+'
...
}
由于缓存的cardview aar损坏,我遇到了这个问题。
Gradle正在选择cardview的本地缓存版本。通过更改库版本也可以使用,但请确保您未覆盖configurations.all
解:
关闭Android Studio,
在Windows中,转到C:\Users\Qamar\.gradle\caches并搜索cardview删除您的版本文件夹,清理并重建。
使用:Android Studio 3.3和gradle:3.2.1