【问题标题】:Data binding class not generated in latest studio 3.6最新的 studio 3.6 中未生成数据绑定类
【发布时间】:2020-06-09 03:28:43
【问题描述】:

今天我将我的 android studio 3.5.3 更新到了 3.6。现在,我无法生成任何数据绑定类。 Android studio 自动生成 data-binding-iml 文件。

有人遇到过这样的问题吗?

Gradle wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

项目级 gradle 文件:

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
}

应用级 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'
android {
 dataBinding {
        enabled = true
    }
    // Using Lambda Expressions
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

gradle.properties:

kotlin.code.style=official
android.databinding.enableV2=true
kotlin.incremental=true
kapt.incremental.apt=true

以下是我的 Activity 和 XML 文件: 活动:

class ActivityMain : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="android.view.View" />
     <!--   <variable
            name="loading"
            type="Boolean" />-->
        <variable
            name="bottomMenu"
            type="Boolean" />
        <variable
            name="clickListener"
            type="com.ecom.side_menu.SideMenuClickHandler" />
    </data>
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/layToolbar"
                layout="@layout/layout_toolbar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/splash_host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/white"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/layToolbar"
                app:navGraph="@navigation/splash_graph" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginEnd="0dp"
                android:background="@color/colorPrimary"
                android:visibility="@{safeUnbox(bottomMenu) ? View.VISIBLE : View.GONE}"
                app:itemBackground="@color/colorPrimary"
                app:itemIconTint="@android:color/white"
                app:itemTextColor="@android:color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/menu_navigation_dashboard" />


         <!--   <include
                android:id="@+id/progressLayoutId"
                layout="@layout/layout_progress"
                android:visibility="@{safeUnbox(loading) ? View.VISIBLE : View.GONE}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />-->

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:fitsSystemWindows="true"
            android:visibility="visible">
            <include
                android:id="@+id/customDrawerList"
                app:clickListener="@{clickListener}"
                layout="@layout/drawer_list" />
        </com.google.android.material.navigation.NavigationView>

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>

官方解决方案:

Google 已解决此问题。使用 android studio 3.6.2 的新补丁更新后,我可以创建具有多个源集的数据绑定类

【问题讨论】:

  • 我今天自己也更新了工作室 && 也使用数据绑定类.. 兄弟,它工作得很好
  • 只是使缓存无效并重新启动您的工作室;)
  • @Niceumang 我尝试过使缓存无效并重新启动,同时我也尝试删除构建文件夹。但仍然是同样的错误
  • 你会在你创建绑定类的地方展示你的 XML 和类吗?
  • @Niceumang 类和有问题添加的 XML

标签: android android-studio android-studio-3.6


【解决方案1】:

我正在使用 android studio 3.6.1 在build.gradle 中添加viewBinding.enabled = trueandroid{ 后问题解决了:

android {

...
    dataBinding {
        enabled = true
    }
    viewBinding.enabled = true

更新: 确保在 App Module gradle 文件的顶部添加 apply plugin: 'kotlin-android-extensions'

  plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'kotlin-android-extensions' //<--
  }

【讨论】:

  • 有效!!我错过了 dataBinding { enabled = true }
【解决方案2】:

这也发生在我身上。绑定类实际上是生成的。该项目构建良好。只有 Android Studio 3.6.1(或底层 Gradle 构建系统,我不在乎)有问题,看不到这些类。

作为中间解决方案,我只是破解了源集(请注意,下面片段中的构建变体是特定于我的项目的,您需要重写它)。

android {
    ...
    sourceSets {
        demoDebug {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/demoDebug/out'
        }
        fullDebug {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/fullDebug/out'
        }
        espressoDebug {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/espressoDebug/out'
        }
        demoRelease {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/demoRelease/out'
        }
        fullRelease {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/fullRelease/out'
        }
        espressoRelease {
            java.srcDirs += 'build/generated/data_binding_base_class_source_out/espressoRelease/out'
        }
    }
    ...
}

正如上面史蒂夫所指出的:与此同时,我们必须耐心等待谷歌修复它......

编辑

我刚刚意识到它比我预期的要多得多,布局也被破坏了:

我希望 Google 能尽快解决这个问题……

编辑 2

我再次意识到 Android Studio 3.6 比上面描述的漏洞更多。

现有 Espresso 测试的执行也被破坏了。

我强烈建议大家不要升级到 Android Studio 3.6。

同时,我们可能会降级回 Android Studio 3.5。

【讨论】:

  • 谢谢.. 这个 hack 工作正常.. 现在我可以创建 Binding 类了。
  • 但仍然无法在 xml 中正确访问绑定变量
  • 不幸的是,正如我在上面的编辑中提到的那样:(
【解决方案3】:

只需将您的 Gradle 版本更新到最新版本。前往:

文件 > 项目结构 > Gradle 版本

并选择最新的稳定版本(目前为 6.3)。如果它也指向最新版本,还要检查您的 Android Gradle 插件版本。

【讨论】:

  • 谢谢,这对我有帮助! +1
  • 很好的答案,感谢菜单指针...这也解决了我的问题。我花了很多时间在这上面(事后看来很明显)。
【解决方案4】:

对于 AndroidStudio 3.6.1,您可以将以下代码添加到应用级别 build.gradle(:app)。 我的问题通过添加这一行得到解决,希望你也是。

sourceSets {
     main {
          java.srcDirs += 'build/generated/data_binding_base_class_source_out/debug/out'
        }
    }

【讨论】:

  • 按照 Jiří Křivánek 和你的建议,我添加了提到的解决方案,但现在,每个变量都显示为红色下划线.. 无法访问
【解决方案5】:

最新的 Android 数据绑定库似乎确实存在错误。清理项目没有奏效。重建项目没有奏效。使缓存无效并重新启动不起作用。

对我有用的唯一解决方案是将数据绑定版本从版本 2(这是 Android Studio 3.6.1 及更高版本中的新默认设置)回滚到版本 1。为此,您不必滚动返回所有的 Android Studio。相反,您可以将以下行添加到gradle.properties

android.databinding.enableV2=false

TLDR;我想知道谷歌是否已经决定彻底​​改变我们应该使用他们最新的数据绑定编译器进行数据绑定的方式。如果团队正在努力提供 YAS(另一种语法),那就不足为奇了。毕竟,DataBindingUtil.inflate&lt;MyClassBindingImpl&gt; 必须是所有计算机编程中最奇怪的使用模式之一,它要求编译器自动生成通用数据绑定类的具体实现,然后才能引用自动生成的类。代码。顺便说一句,这就是为什么存在像 Make Project 这样的工具的原因。我有一半希望对数据绑定语法进行彻底改革。

【讨论】:

    【解决方案6】:

    我遇到了这个问题,因为我将我的布局分成了几个目录,并像下面这样在 gradle 中定义它们

    sourceSets {
        main {
            res.srcDirs =
                    [
                            'src/main/res', 'src/main/drawable/button_icons', 'src/main/res/drawable/button_icons',
    
                            'src/main/res', 'src/main/layouts/user', 'src/main/res/layouts/user',
                            'src/main/res/layouts', 'src/main/layouts/user', 'src/main/res/layouts/user/register',
                            'src/main/res/layouts', 'src/main/layouts/user', 'src/main/res/layouts/user/login',
                            'src/main/res/layouts', 'src/main/layouts/user', 'src/main/res/layouts/user/profile',
                            'src/main/res/layouts', 'src/main/layouts/user', 'src/main/res/layouts/user/wallet',
    
                            'src/main/res/layouts/splash_layouts', 'src/main/res/layouts', 'src/main/res',
    
                            'src/main/res/layouts/main_layouts', 'src/main/res/layouts', 'src/main/res',
                            'src/main/res/layouts/main_layouts/sellers', 'src/main/res/layouts/main_layouts', 'src/main/res/layouts',
    
                            'src/main/res/layouts/dashboard_layouts', 'src/main/res/layouts', 'src/main/res',
    
                            'src/main/res/layouts/basket_layouts', 'src/main/res/layouts', 'src/main/res',
                            'src/main/res/layouts/factor_layouts', 'src/main/res/layouts', 'src/main/res',
    
                            'src/main/res/layouts/setting_layouts', 'src/main/res/layouts', 'src/main/res',
                    ]
        }
    
    }
    

    所以我把所有布局都放在主布局目录下,删除其他子布局目录

    【讨论】:

      【解决方案7】:

      这似乎已在 Android Studio 3.6.3 中得到修复。虽然

      viewBinding.enabled = true
      

      也为我工作,而不是

      viewBinding {
          enabled = true
      }
      

      【讨论】:

      • 请提供一些上下文。这是什么代码。去哪儿了?
      • 在你的 gradle 文件中,在 android block android { ... }
      【解决方案8】:

      尝试重新构建项目并查看生成的文件中是否有数据绑定文件夹

      【讨论】:

        【解决方案9】:

        也发生在我身上。这是因为在 AS 3.6.0 中,如果我们将 include 标记与 android:id 一起使用,我们将无法访问来自另一个模块的绑定。我认为这个错误来自 IDE,因为我可以成功运行该项目。唯一要做的就是等待修复或忽略错误。

        【讨论】:

          【解决方案10】:

          这也发生在我身上。我刚刚在项目 build.graddle 文件中将 Gradle 版本升级到 3.6.0,现在它又可以工作了。

          【讨论】:

            【解决方案11】:

            Android Gradle Plugin 4.0.0-alpha05 开始,有一个名为buildFeatures 的新块用于启用构建功能。

            buildFeatures {
                dataBinding true
                viewBinding true // for view binding:
            }
            

            您需要viewBinding true 来解决您的问题。

            【讨论】:

            【解决方案12】:

            谷歌在其网站中没有提到的重要一点是,您必须将布局转换为数据绑定布局,如下所示:

            enter image description here

            你也最好访问这个网站 https://developer.android.com/codelabs/android-databinding#2

            【讨论】:

              【解决方案13】:

              确保在 XML 文件中添加以下标记:

              <layout xmlns:android="http://schemas.android.com/apk/res/android">
              
              <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center">
              
              <ImageView
                  android:id="@+id/userIMG"
                  android:layout_width="300dp"
                  android:layout_height="300dp"
                  android:layout_gravity="center"
                  android:scaleType="fitCenter"
                  android:layout_centerInParent="true"
                  android:src="@drawable/logo"/>
              
              
                  </LinearLayout>
                   </layout>
              

              【讨论】:

                猜你喜欢
                • 2017-01-21
                • 2020-06-10
                • 1970-01-01
                • 2018-09-05
                • 2019-01-16
                • 1970-01-01
                • 2016-08-07
                • 2019-07-28
                相关资源
                最近更新 更多