7

I have a flavor that contains the same layout resource that is in my main source set. This is so it overrides the main one with the flavor specific one. I have done this in many places and it works fine until I introduced ViewBinding. For accuracy, the layout is a nested layout that is "included" in a fragment.

Here is the structure:

main
 - res 
 - - layout 
 - - - layout_sign_up_details_fields.xml
flavor 
 - res 
 - - layout 
 - - - layout_sign_up_details_fields.xml

I have a view, whose ID is til_sudf_first_name, in my flavor specific layout resource that I do not have in my main resource. This is intended. I can see this View is "bound" in my binding class by looking in the generated sources when running this build variant.

@NonNull
public final TextInputLayout tilSudfFirstName;

Now the actual binding happens in a "base" class and is shared with child classes using the protected visibility modifier like so:

private var _binding : FragmentSignUpDetailsBinding? = null
protected val binding get() = _binding!!
protected val detailsBinding get() = binding.lSudfDetails

The problem is detailsBinding does not contain the view I require even though it's present in the generated class.

In addition, the other strange thing is even though there are views in the main layout file that are not in the flavor specific layout file, they are still included in the generated layout file.

Is what I am trying to do even possible? The workaround available to me is to use kotlin's synthetic view handling for this instance as I have been but I was trying to use the recommended approach

Rahul
  • 2,374
  • 2
  • 22
  • 36
StuStirling
  • 14,067
  • 20
  • 82
  • 138
  • For now, I have had to go old school in my flavor dependent class and use `findViewById` to reference the flavor specific view. – StuStirling Mar 17 '20 at 13:58

1 Answers1

0

The trick is to use one shared package name for your fragments and viewmodels:

main
 - java
 - - MainFragment (package com.rahul.sample.<..>)
 - - MainViewModel (package com.rahul.sample.<..>)

flavor
 - java
 - - FlavorFragment (package com.rahul.sample.<..>)
 - - FlavorViewModel (package com.rahul.sample.<..>)

This way the binding works.

JuliaKo
  • 612
  • 1
  • 11
  • 17