8

I have problem with usage of DataBinding in my Dynamic Feature module.

I can't generate binding object related for my fragment when isMinifyEnabled = true

Generally I try to do it using this code: val viewDataBinding: FragmentFeature1Binding = DataBindingUtil.inflate(inflater, layoutId, container, false) but value returned by inflate() is always null without any additional message. I tried to inflate layout using FragmentFeature1Binding directly, but I had the same results.

When I move my fragment into app module then everything is ok.

My app build.gradle.kts:

android {
    compileSdkVersion(AndroidVersions.compileSdk)
    buildToolsVersion(AndroidVersions.buildTools)

    dataBinding.isEnabled = true

    defaultConfig {
        applicationId = ApplicationConfig.id
        minSdkVersion(AndroidVersions.minSdk)
        targetSdkVersion(AndroidVersions.targetSdk)
        versionCode = ReleaseVersions.versionCode
        versionName = ReleaseVersions.versionName
    }
    buildTypes {
        getByName(BuildTypes.debug) {
            isMinifyEnabled = true
            isShrinkResources = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            buildConfigField("Long", "API_TIMEOUT_IN_SECONDS", "30l")
        }
    }

    compileOptions {
        targetCompatibility = JavaVersion.VERSION_1_8
        sourceCompatibility = JavaVersion.VERSION_1_8
    }

    dynamicFeatures = mutableSetOf(Modules.feature1)
}

My feature1 build.gradle.kts:

android {
    compileSdkVersion(AndroidVersions.compileSdk)

    dataBinding.isEnabled = true

    defaultConfig {
        minSdkVersion(AndroidVersions.minSdk)
        targetSdkVersion(AndroidVersions.targetSdk)
    }

    compileOptions {
        targetCompatibility = JavaVersion.VERSION_1_8
        sourceCompatibility = JavaVersion.VERSION_1_8
    }

    buildTypes {
        create(BuildTypes.instrumentation) {
            buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
        }

        getByName(BuildTypes.debug) {
            buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
        }

        getByName(BuildTypes.release) {
            buildConfigField("String", "API_ENDPOINT", "\"https://api.github.com/\"")
        }
    }
}

My proguard-rules.pro

-dontwarn android.databinding.**
-keep class android.databinding.** { *; }

-keep class kotlin.Metadata {
    *;
}

-keepattributes InnerClasses
 -keep class **.R
 -keep class **.R$* {
    <fields>;
}

Without R8 everything is working correctly, so I am thinking some rule is missing but I tried few configurations and sadly there is no official configuration from Google.

Paweł Dedio
  • 1,338
  • 12
  • 20

1 Answers1

0

This problem will occur if you are using android studio version 3.2 or above...
Reason:
from android studio version 3.2+, "data-binding v2" is enabled as default..
Solution
you have to enable data-binding v1 from gradle.properties like below

android.databinding.enable=true

It works for me...hope you will have the solution. :)

Sanket Vekariya
  • 2,848
  • 3
  • 12
  • 34