[英]Android Studio is giving errors and warnings after enabling view binding. How to fix?
I am getting errors after i enabled view binding in my project.在我的项目中启用视图绑定后出现错误。
I have tried resyncing the project, invalidating cache.我试过重新同步项目,使缓存无效。 My app gradle file:我的应用程序 gradle 文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.learning.aboutme"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
viewBinding.enabled = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
My project gradle file我的项目 gradle 文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
My activity main.xml:我的活动 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingStart="@dimen/padding"
android:paddingEnd="@dimen/padding"
tools:context=".MainActivity">
<TextView
android:id="@+id/name_text"
style="@style/name_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:textAlignment="center" />
<EditText
android:id="@+id/nickname_edit"
style="@style/name_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/name_it_what_is_your_nickname"
android:inputType="textPersonName"
android:textAlignment="center" />
<Button
android:id="@+id/done_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/layout_margin"
android:fontFamily="@font/roboto"
android:text="@string/done" />
<TextView
android:id="@+id/nickname_text"
style="@style/name_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:visibility="gone" />
<ImageView
android:id="@+id/star_Image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin"
android:contentDescription="@string/yellow_star"
app:srcCompat="@android:drawable/btn_star_big_on" />
<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="@dimen/spacing_multiplier"
android:text="@string/bio"
android:textColor="@android:color/black" />
</ScrollView>
</LinearLayout>
Here the android studio gives a warning on android:contentDescription
, textAlignment
, lineSpacingMultiplier
, text
etc这里 android 工作室在android:contentDescription
、 textAlignment
、 lineSpacingMultiplier
、 text
等
Here is my main activity.kt file:这是我的主要 activity.kt 文件:
package com.learning.aboutme
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.learning.aboutme.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(R.layout.activity_main)
}
private fun addNickname(view: View) {
}
}
Here android studio says: Cannot access android.viewbinding.ViewBinding
which is a supertype of com.learning.aboutme.databinding.ActivityMainBinding
.这里 android 工作室说:无法访问android.viewbinding.ViewBinding
这是com.learning.aboutme.databinding.ActivityMainBinding
的超类型。 On ActivityMainBinding.inflate()
关于ActivityMainBinding.inflate()
I tried creating a new project and moving my files there but the same errors ( Tried moving activity_main.xml
and the same errors.我尝试创建一个新项目并将我的文件移动到那里但出现了相同的错误(尝试移动activity_main.xml
和相同的错误。
How to fix this?如何解决这个问题?
For me the error went away when I added对我来说,当我添加时错误消失了
implementation 'com.android.databinding:viewbinding:4.0.1'
to the build.gradle
file.到build.gradle
文件。
This dependency contains the class android.viewbinding.ViewBinding
which AndroidStudio could not access.此依赖项包含 AndroidStudio 无法访问的 class android.viewbinding.ViewBinding
。
In my case the following steps (clearing of cache) did help:在我的情况下,以下步骤(清除缓存)确实有帮助:
Step 1: open Terminal and goto your project path in terminal.第 1 步:打开终端并在终端中转到您的项目路径。
Step 2: Optional Hit this command git clean -xfd
or safe variant git clean
.第 2 步:可选点击此命令git clean -xfd
或安全变体git clean
。 Be careful with -xfd
this as this removes untracked files and .gitignore files
小心使用-xfd
,因为这会删除未跟踪的文件和.gitignore files
Step 3: Goto: File -> Invalidate caches/restart.第 3 步:转到:文件 -> 使缓存无效/重新启动。
Facing same issue exact error yesterday.昨天面临同样的问题确切的错误。 Solution work for me is对我来说解决方案是
and Error is gone并且错误消失了
Making sure确保
buildFeatures { viewBinding true }
I found the solution from here: https://cdmana.com/2021/05/20210511224308742x.html .我从这里找到了解决方案: https://cdmana.com/2021/05/20210511224308742x.html 。 Just add at gradle.properties
只需添加gradle.properties
//... android.enableD8=true android.injected.testOnly=false
I hope it will solving your problem我希望它能解决你的问题
Had the same problem with that error该错误有同样的问题
android.viewbinding.ViewBinding which is a supertype of com.learning.aboutme.databinding.ActivityMainBinding. On ActivityMainBinding.inflate()
What I did and solved it was erasing from the build.grade module我所做并解决的问题是从 build.grade 模块中删除
viewBinding{
enabled = true
}
And Sync it, after re added the erased code from the module and sync again and voila.并同步它,从模块中重新添加已擦除的代码并再次同步,瞧。 That solved it for me.这为我解决了它。 Thanks for all other responses tho感谢所有其他回复
Error Solved for me By: Adding below line in the gradle.properties(Project Propoerties) file:错误为我解决了:在 gradle.properties(Project Propoerties) 文件中添加以下行:
android.useAndroidX=true android.useAndroidX=true
You can try this solution:你可以试试这个解决方案:
add "multiDexEnabled true" in gradle file like below在 gradle 文件中添加“multiDexEnabled true”,如下所示
android {
compileSdk 30
defaultConfig {
applicationId "com.example.test"
minSdk 19
targetSdk 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}}
I faced this exact error today when I used viewBinding in my fragment so what helped was我今天在片段中使用 viewBinding 时遇到了这个确切的错误,所以有帮助的是
Somehow the Invalidate caches and restart to be specific solved the issue.不知何故,无效缓存并重新启动具体解决了这个问题。
i adding dependencies in build.gradle我在 build.gradle 中添加依赖项
I faced the same problem and I fix it by doing the steps below:我遇到了同样的问题,我通过执行以下步骤来解决它:
1- go to build.gradle file and remove buildFeatures { viewBinding = true }
if you already put it there 1- go 到 build.gradle 文件并删除buildFeatures { viewBinding = true }
如果你已经把它放在那里
2-synch 2同步
3-Clean and Rebuild the project. 3-清理并重建项目。
4-put viewBinding back again buildFeatures { viewBinding = true}
4-将 viewBinding 放回去buildFeatures { viewBinding = true}
5-synch again再次同步 5
For me the error went away when I added对我来说,当我添加时错误消失了
implementation 'com.android.databinding:viewbinding:7.3.1'
to the build.gradle file. implementation 'com.android.databinding:viewbinding:7.3.1'
到 build.gradle 文件。
This dependency contains android.viewbinding.ViewBinding
which android studio may not be able to access without this dependency.此依赖项包含android.viewbinding.ViewBinding
,如果没有此依赖项,android 工作室可能无法访问。
Here's what worked for me:这对我有用:
I initially had the build.gradle(Module: App_name.app) file opened in Android Studio.我最初在 Android Studio 中打开了 build.gradle(Module: App_name.app) 文件。
Then I opened the "Project Structure" window... There, at the left pane, I opened the "suggestions" tab.然后我打开了“项目结构”window... 在左侧窗格中,我打开了“建议”选项卡。
Several Suggestions for updating things were there, which I selected and pressed OK, after which they started getting updated from internet, and after that, did Gradle sync, and all Errors were gone in the MainActivity.kt file.那里有几个更新的建议,我选择并按下确定,之后他们开始从互联网上更新,之后,Gradle 同步,所有错误都在 MainActivity.kt 文件中消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.