When I build an Android application, the file R.java
used to reference resources in code is generated at the following location:
/app/build/generated/source/r/debug/com/example/R.java
My understanding is that the new Android data binding library works in a similar way, generating binding classes based on layout filenames during the build process. For example, this layout named view_drawer_page_header.xml
:
<data>
<variable name="viewModel" type="com.example.ViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- actual layout -->
</LinearLayout>
should generate a class named ViewDrawerPageHeaderBinding
. The documentation mentions that this class will be located in the com.example.databinding
package. However, if I search my project for a file with this name after building, I get no results. The app runs fine on devices, so I'm assuming the generated files are part of the final .apk. Is it possible to inspect their contents?
Same question applies to e.g. the generated BR
class, used to reference updatable Observable
properties annotated with the @Bindable
annotation.
A binding class is generated for each layout file. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main. xml so the corresponding generated class is ActivityMainBinding .
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.
View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.
After some more poking, I unearthed the generated files in the following locations:
/app/build/intermediates/classes/debug/com/example/databinding/ViewDrawerPageHeaderBinding.java
and
/app/build/intermediates/classes/debug/com/example/BR.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With