Android开发 CardView卡片视图、RecyclerView回收视图
注意开发工具从28变为30以后即src目录下的bulid.grade文件中的buildTools版本改变
1
2
3
4
|
android {
compileSdkVersion 28
buildToolsVersion "28.0.0"
}
|
1
2
3
4
|
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
}
|
CardView、RecyclerView都发生了使用变化
引用:
app目录下的bulid.grade文件中依赖dependencies改变
只要将implementation ‘com.android.support:cardview-v7:28.0.0’换成implementation ‘androidx.cardview:cardview’就行了(recyclerview类似改变);
原因:版本28(面向android pie及以下版本)是遗留支持库的最后一个版本,因此我们建议您在使用android q并继续前进时迁移到androidx库。IDE可以帮助您:重构>迁移到AndroidX…,
1
2
3
4
|
dependencies {
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
|
1
2
3
4
5
6
|
dependencies {
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
// For control over item selection of both touch and mouse driven selection
implementation "androidx.recyclerview:recyclerview-selection:1.1.0"
}
|
依赖请看官方文档Cardview、RecyclerView
XML文件
主要是CardView、RecyclerView的标题
原来CardView
1
2
3
4
5
6
7
8
9
10
11
|
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:elevation="5dp"
app:cardCornerRadius="5dp">
</android.support.v7.widget.CardView>
|
RecyclerView
1
2
3
4
5
6
|
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
|
变为CardView‘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="@+id/content1"
android:text="内容"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
|
RecyclerView’
1
2
3
4
5
6
7
|
<!-- 你的可能是这样的-->
<android.support.v7.widget.RecyclerView
<!-- 这才是对的-->
<androidx.recyclerview.widget.RecyclerView
<!-- xxx-->
<!-- xxx-->
.../>
|
1
2
3
|
Error:A problem occurred configuring project ':app'.
\> Failed to find Build Tools revision 26.0.1
|
在build.gradle 中buildToolsVersion
如何修改。看本地安装了哪些版本的
进入文件夹Android SDK 目录下build-tools,修改为里面有的版本
Could not find method google() for arguments [] on repository container.
问题:
在react native中安装realm数据库后运行react-native run-android 时报如下错:
开发环境:
react-native:^0.55.4
reaml:^2.27.0
解决方案:
1、打开项目根目录下android/gradle/wrapper/gradle-wrapper.properties
将distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip中的2.14.1改成4.1
2、打开项目根目录下的android/build.gradle
1)、在buildscript和allprojects下的repositories分别添加google()
2)、将dependencies中的classpath ‘com.android.tools.build:gradle:2.2.3’中的2.2.3【具体看自己的是多少】改成3.0.1
重新运行编译项目命令,要下载新的gradle,这个过程有点慢。至此,问题已解决!
Could not find com.android.support:appcompat-v7:25.0.0
老的Android工程导入as报错如下:
1
2
3
|
10:36 Gradle sync failed: Could not find com.android.support:appcompat-v7:25.0.0.
Required by:
FuNongTong:app:unspecified (18 s 207 ms)
|
如果是gradle4.0及以下,增加maven { url “https://maven.google.com” },如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
buildscript {
repositories {
jcenter()
maven { url "https://maven.google.com" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://maven.google.com" }
}
}
|
如果是gradle4.0及以上,maven { url “https://maven.google.com” }替换为google()
如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
buildscript {
repositories {
jcenter()
google()
}
}
allprojects {
repositories {
jcenter()
google()
}
}
|
Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=M
android studio升级到3.1.4之后gradle里的很多配置也相应发生了一些改变。在打包的时候我就遇到了这样的问题。
报错为:Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
截图如下:
大家可以注意看一下,AS升级到3.0以上版本后,截图上的红框处的代码都要改动,否则是无法正常打包的。那要改成什么样呢,如下图所示:
改成上图所示的样子就可以正常打包了,另外我也把代码贴出来方便大家复制。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//打包后应用名称
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {//如果是release包
fileName = "anjian_release_v${defaultConfig.versionName}.apk"
} else if (variant.buildType.name.equals('debug')) {//如果是debug包
fileName = "anjian_debug_v${defaultConfig.versionName}.apk"
}
outputFileName = fileName
}
}
}
|
build.grade.dependence随版本引用关键字改变
1
2
3
|
// testCompile->testImplementation
// compile->implementation
// provided->compileOnly
|
Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use ‘annotationProce
AS从2.3.3升级到3.0后,项目没有任何改动就出现了这个错误,解决步骤如下:
一、把module/build.gradle下的apt插件应用全部注释掉
二、把dependencies下的apt全部改为annotationProcessor
annotationProcessor 在as最新版本也失效,要如下操作
annotationProcessor ‘org.projectlombok:lombok:1.18.6’ 修改为
compile ‘org.projectlombok:lombok:1.18.6’ 即可
三、把project/build.gradle中的apt插件声明注释
更新Gradle项目时报错Gradle sync failed: Unsupported method: BaseConfig.getApplicationIdSuffix
查看Android Gradle 插件版本说明。
https://developer.android.google.cn/studio/releases/gradle-plugin.html#updating-plugin
修改对应的项目根目录下的build.gradle的依赖(dependencies)下的gradle的build工具版本
1
|
dependencies {classpath "com.android.tools.build:gradle:4.1.3"}
|
和项目根目录下的gradle文件夹中的wrapper文件夹中的build.gradle文件中的gradle版本
1
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
|
注意:第一个文件要在原来jcenter()位置加上google()
1
2
3
|
repositories{ google()
jcenter()}
|
Could not resolve all files for configuration ‘:app:debugRuntimeClasspath’.
Could not find com.android.support:appcompat-v7:23.2.1.
Searched in the following locations:
1
2
3
4
5
|
repositories {
google()
maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}//加上这行
//jcenter()
}
|
修改"targetSdkVersion"和"compileSdkVersion" 为30报错"NoSuchMethodException" error for class “android.graphics.FontFamily”
报错
如题修改gradle文件中版本问题报错如下
1
|
2020-06-22 13:43:21.205 14538-14538/com.gravty.everyday W/gravty.everyda: Accessing hidden method Landroid/graphics/FontFamily;->()V (greylist-max-q, reflection, denied) 2020-06-22 13:43:21.206 14538-14538/com.gravty.everyday E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException java.lang.NoSuchMethodException: android.graphics.FontFamily. [] at java.lang.Class.getConstructor0(Class.java:2332) at java.lang.Class.getConstructor(Class.java:1728) at androidx.core.graphics.TypefaceCompatApi26Impl.obtainFontFamilyCtor(TypefaceCompatApi26Impl.java:321) at androidx.core.graphics.TypefaceCompatApi26Impl.(TypefaceCompatApi26Impl.java:84) at androidx.core.graphics.TypefaceCompatApi28Impl.(TypefaceCompatApi28Impl.java:36) at androidx.core.graphics.TypefaceCompat.(TypefaceCompat.java:47) at androidx.core.graphics.TypefaceCompat.findFromCache(TypefaceCompat.java:76) at androidx.core.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:393) at androidx.core.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:361) at androidx.core.content.res.ResourcesCompat.getFont(ResourcesCompat.java:339) at androidx.appcompat.widget.TintTypedArray.getFont(TintTypedArray.java:119) at androidx.appcompat.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:430) at androidx.appcompat.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:204) at androidx.appcompat.widget.AppCompatTextView.(AppCompatTextView.java:105) at androidx.appcompat.widget.AppCompatTextView.(AppCompatTextView.java:95) at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182) at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407) at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457) at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1261) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1117) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082) at android.view.LayoutInflater.inflate(LayoutInflater.java:680) at android.view.LayoutInflater.inflate(LayoutInflater.java:532) at android.view.LayoutInflater.inflate(LayoutInflater.java:479) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) at com.gravty.everyday.views.activities.LoginActivity.onCreate(LoginActivity.java:53) at android.app.Activity.performCreate(Activity.java:7989) at android.app.Activity.performCreate(Activity.java:7978)
|
解决办法
法1
添加一个稳定的核心库implementation 'androidx.core:core:1.3.0'
(https://developer.android.com/jetpack/androidx/releases/core) 在gradle build文件中
法2
如果使用了Appcompat库可以用其中的i.e. androidx.appcompat:appcompat:1.3.1
(https://developer.android.com/jetpack/androidx/releases/appcompat#version_131_3)
法3
或者委屈求全改回原来的版本如28https://developer.android.com/topic/libraries/support-library/packages#v4
end