【问题标题】:Kotlin-DataBinding-Error: Check your module classpath for missing or conflicting dependenciesKotlin-DataBinding-Error:检查您的模块类路径是否存在缺失或冲突的依赖项
【发布时间】:2019-07-01 11:43:19
【问题描述】:

我该如何解决这个问题。我无法使这个数据绑定工作,我已经尝试了一切。 Build.gradle(模块应用)

    apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.fusion.alen.ask"
        minSdkVersion 16
        targetSdkVersion 27
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    androidExtensions {
        experimental = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }

}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:animated-vector-drawable:27.1.1'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:customtabs:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:recyclerview-v7:27.1.1'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android-extensions'
repositories {
    mavenCentral()
}

我尝试添加 kapt "com.android.databinding:compiler:$compiler_version" 但它给了我一个错误。 Build.gradle(模块项目)

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

buildscript {
    ext{
        kotlin_version = '1.2.71'
        compiler_version = '3.2.1'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

这是我的 profile_fragment.xml,我已按照文档说明进行操作

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="user"
            type="com.alen.ask.UserModel.User" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="25dp">


        <LinearLayout
            android:id="@+id/profile_box"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/primaryColor"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/app_bar">

            <LinearLayout
                android:id="@+id/qff_box"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5"
                android:orientation="horizontal"
                android:baselineAligned="false">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/questions_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.questionsNum}"************
                        tools:text="0" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/followers_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.followersNum}"************
                        tools:text="0" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/following_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        **android:text="@{user.followingNum}"***********
                        tools:text="0" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/app_bar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/primaryDarkColor"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/username_text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                **android:text="@{user.name}"**
                android:textColor="@color/primaryTextColor"
                android:textSize="18sp"
                android:layout_marginStart="8dp" />
        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</layout>

我的用户数据类:

data class User(val name: String,
                val email: String,
                val uid: String,
                val photo: String,
                val postsList: ArrayList<String> = ArrayList(),
                val questionsNum: Int = 0,
                val followersNum: Int = 0,
                val followingNum: Int = 0
                ) 

这是在 ProfileFragmen 类中,在 onCreateView 方法中

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = ProfileFragmentBinding.inflate(inflater, container, false)
        binding.user = user
}

该行 binding.user = user 给了我错误:无法访问类“UserModel.User”。检查您的模块类路径是否存在缺失或冲突的依赖项 我该如何解决这个问题,我已按照 DataBinding 文档中的说明进行操作,我认为我没有犯任何错误。我曾尝试清理和重建项目,但没有奏效。我尝试添加那些“kapt”依赖项,但没有奏效.... File->ProjectStructure->Gradle 版本:4.10.1,AndroidPluginVersion:3.2.1,KotlinLanguageVersion:1.2,KotlinPluginVersion:1.2.71-release-Studio3.2-1

谢谢。

【问题讨论】:

    标签: android data-binding kotlin android-databinding


    【解决方案1】:

    多模块 Gradle 项目的解决方案(如果您从代码库构建多个应用程序):

    ViewBinding 和 DataBinding 库在使用许多 Gradle module 时其内部生成的源集存在问题。

    问题:

    您有 2 个名称相同的文件(例如 content_main.xml),它们存在于 layout 目录中的 :first-app:second-app 模块中,IDE 将呈现红色错误。

    但是,应用程序可以正常编译和启动。

    解决方案:

    区分 XML 文件的名称 - 因此绑定将生成为:FirstAppContentMainBinding (first_app_content_main.xml) 和 SecondAppContentMainBinding (second_app_content_main.xml)

    【讨论】:

      【解决方案2】:

      您的variable 标签中缺少“fusion”,尽管您在 gradle 中的 applicationId 中有它。

      【讨论】:

        【解决方案3】:

        遇到了同样的问题,通过将 build.gradle 模块中的构建功能从 viewBinding 更改为 dataBinding 来修复它

        buildFeatures {
            dataBinding = true
        }
        

        【讨论】:

          【解决方案4】:

          原因是因为你的一个包名以大写开头。

          使用数据绑定的类名应以大写开头,包名应以小写开头. [这不仅仅是数据绑定,这是普遍遵循的约定]

          [在内部当 dataBinding 为你的 xml 生成 bindingImplementation 时,它会解析你的包名来识别类, 如果包名以大写开头,则无法将类名与包名分开,并引发错误。]

          【讨论】:

          • 我也遇到了同样的问题,这是一个问题,因为我的包名的第一个字母在 Caps 中,我将其更改为 small 并且它可以工作。谢谢
          【解决方案5】:

          将您的文件夹 UserModel 重命名为 userModel,然后清理/重建您的项目。我遇到了同样的问题,将文件夹的第一个字母设置为小写时错误消失了。将来使用 camelCase 作为您的文件夹名称,就像变量一样。第一个字母大写通常用于类。

          【讨论】:

          • 没关系。我不认为这样的问题:D。谢谢
          • 我正在检查依赖项并且确实使缓存无效并重新启动。谢谢你
          【解决方案6】:

          尝试使用 binding.setVariable 方法。

          【讨论】:

          • 请注意建议不应作为答案发布。
          【解决方案7】:
          <data class="ProfileBinding">
              <variable
                  name="user"
                  type="com.alen.ask.UserModel.User" />
          </data>
          

          在 Fragment 类中使用上面提到的 ProfileBinding 类来膨胀

          【讨论】:

            【解决方案8】:

            在文件profile_fragment.xml 尝试替换它

            <data>
                <variable
                    name="user"
                    type="com.alen.ask.UserModel.User" />
            </data>
            

            有了这个

            <data>
                <variable
                    name="user"
                    type="com.alen.ask.User" />
            </data>
            

            并给我们反馈。

            【讨论】:

            • 我的用户数据类在 UserModel 文件夹中,所以删除它不起作用
            猜你喜欢
            • 1970-01-01
            • 2018-10-22
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多