'Android aar library does not include BR class

I develop a small library for testing aar library creation with databinding.

My problem is when I generate the aar file it doesn't include the BR class generated for databinding references in layout. But the class has been generated by Android Studio.

Do you have a solution to solve this problem ?

Thanks in advance.

build.gradle

apply plugin: 'com.android.library'
apply plugin: 'android-maven'

android {
    signingConfigs {
        release {
            keyAlias 'homelightcore'
            keyPassword 'overcraft'
            storeFile file('/Users/acidspike/Development/Certificats/Android/HomeLight/Core/homelightcore.jks')
            storePassword 'overcraft'
        }
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    dataBinding {
        enabled = true
    }
}

version '1.0'
group "fr.acidspike.homelight"

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.4.0'
    testCompile 'junit:junit:4.12'
    compile 'com.loopj.android:android-async-http:1.4.9'
}

proguard-rules.pro

-keep public class *

-keepclassmembers public class * {
   public *;
}

-keepattributes Exceptions


Solution 1:[1]

I found the solution, it's because proguard doesn't detect usage of the BR class members.

I have added :

-keepclassmembers class **.R$* {
    public static <fields>;
}

into the proguard-rules.pro and the BR class become available from the .aar

Solution 2:[2]

aar file is supposed to not include BR class. Instead it contains a file located at /data-binding/*-br.bin. The file gives the compiler information to generate the real BR class for apk when compiling application module. In this way, BR fields with same names but defined in different modules are guaranteed to have same values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anthony Briand
Solution 2 hqzxzwb