You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

每次新建DataBinding相关Activity/Fragment需重建项目生成类,求技术解答

How to Avoid Rebuilding Project Every Time for DataBinding Class Generation

Hey there, I totally get how frustrating it is to stop and rebuild your entire project every time you add a new DataBinding-enabled Activity or Fragment—let’s break down practical fixes to skip this tedious step:

Quick, Effective Solutions

  • Double-Check Your DataBinding Setup
    First, confirm your module-level build.gradle (or build.gradle.kts) has DataBinding enabled properly:

    android {
        ...
        dataBinding {
            enabled = true
        }
        // For Kotlin DSL users:
        // buildFeatures.dataBinding = true
    }
    

    Also, make sure your layout file is wrapped with the <layout> root tag—this is non-negotiable for DataBinding to generate the corresponding class.

  • Use Partial Compilation Instead of Full Rebuild
    Ditch the "Rebuild Project" button (which cleans and recompiles everything) for targeted compilation:

    • From Android Studio’s menu: Build > Make Module '<your-module-name>' (e.g., Make Module 'app')
    • From terminal: Run ./gradlew :app:compileDebugSources (swap debug with release if working on a release build)
      This only compiles the necessary parts of your module, cutting down wait time drastically.
  • Enable Automatic Background Builds
    Let Android Studio handle class generation for you in real time:

    1. Go to File > Settings > Build, Execution, Deployment > Compiler
    2. Check the box for "Build project automatically"
    3. For newer AS versions, enable "Allow auto-make to start even if developed application is currently running"
      Now, whenever you save your layout or add a new DataBinding component, the required classes will generate automatically without you lifting a finger.
  • Clear Stale Caches (As a Last Resort)
    If classes still refuse to generate or stay outdated, clear Android Studio’s cache:

    • Go to File > Invalidate Caches... > Invalidate and Restart
    • Or run ./gradlew clean in terminal, followed by a Make Module command
      This fixes any stuck cache issues that might be blocking class generation.

内容的提问来源于stack exchange,提问作者Insane Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:12:38