5

Now I have three modules,module A ,B,C.module A compile module B,module B compile module C.There is a layout(layout_c.xml) in module C.Then I using layout_c.xml in module A's layout(layout_a.xml).

  1. there is layout_c.xml `

    </variable>
    
    <variable
        name="handler"
        type="xxxxxx">
    
    </variable>
    

    <RelativeLayout
        ......
    </RelativeLayout>
    

    `

  2. there is layout_a.xml

    <include android:id="@+id/layout_c" layout="@layout/layout_c"/>

  3. question:IDE think bindingA.layoutC return a view not a databinding.And module C has BR class and all databinding class.But module A doesn't have.So,what should I do?

    LayoutABinding bindingA = DataBindingUtil.setContentView(this,R.layout.layout_a); newTitleBarViewModel.setDataBinding(bindingA.layoutC);

ZY Song
  • 71
  • 1
  • 7
  • Hmm the compilation should work. Did you try compiling and see what the generated LayoutABinding class has? I'm wondering if this is a studio bug. Also, if so, can you file a bug on b.android.com w/ a sample project attached. Thanks. – yigit Sep 29 '16 at 17:17
  • No,LayoutABinding class not generated.Only C module has LayoutCBinding class. I think ,the reason why LayoutABinding not generated is the IDE compile failed.So it not generated.Do you think so? – ZY Song Oct 08 '16 at 01:02
  • I had create a sample project at the moment,but it also has this problem.So I will file a bug to them and wait reply. – ZY Song Oct 08 '16 at 02:21
  • can you add a link to the bug here ? – yigit Oct 08 '16 at 15:53

1 Answers1

6

In order to get data binding to work across multiple modules - in my case - I had to make sure that each Android Studio module (library, phone/tablet, etc) has data binding enabled in its corresponding build.gradle (not just for the library .gradle file, as that was not enough),

like so:

android {
        ...

    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }

    // Looks like this needs to be set in the app module that uses the lib
    // The lib needs it for the layout binding code there
    dataBinding {
        enabled = true
    }

}

The project structure where solution is applied :

  1. There is a library module, and multiple "app modules" (aka, Android Studio module for Phone or Tablet that can be run) that use Activities/layouts from that library in the same project

  2. The library project has .xml under /res/layout that relies on data binding, like so:

    <TextView
        android:id="@+id/display_name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@={user.displayName}"
        android:textSize="20sp"/>
    

This one was a bit tricky to resolve. Especially in my case where I have multiple modules. After I applied this solution for the main module I was working with, I still continued getting build errors. Finally I noticed in all the error output that there was 1 other module - which I hadn't been working with - that also had the library as dependency , but was missing the data binding enable in build.gradle. When finally that was identified and addressed, the builds are working fine. I'm glad to have found this as it makes things a lot nicer when you can reuse layouts with data binding across multiple modules

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
  • Wondering why this is so. I'm experiencing a lot of weird errors since going multi-module. Seems like some transitive dependencies have to be added to the app module. – funct7 Jan 14 '23 at 02:30