androidbutterknifeandroid-app-bundledynamic-featuredynamic-delivery

Butterknife not working in dynamic-feature sub-project: error: package R2 does not exist


I'm migrating an app to use dynamic delivery and a dynamic-feature module and butterknife is not working anymore with the new configuration.

Currently butterknife works in the base :app module, it also works in standard library modules marked with apply plugin: 'com.android.library'. The issue is only inside my new dynamic-feature module, marked with plugin: 'com.android.dynamic-feature'.

Here is my project-level gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Now my base app gradle file (here in :app butternife works with R as usual)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.elksa.ddsample"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    bundle {
        language {
            enableSplit = true
        }
        density {
            enableSplit = true
        }
        abi {
            enableSplit = true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dynamicFeatures = [":images"]
}

dependencies {
    api 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api 'androidx.appcompat:appcompat:1.0.2'
    api 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Butter Knife
    api 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

    // Dynamic features
    implementation 'com.google.android.play:core:1.6.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Finally here is the gradle file of my dynamic-feature module where butterknife is not working, binding with R gives me a null reference as expected and R2 is totally missing.

apply plugin: 'com.android.dynamic-feature'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':app')
}

Finally this is what I get inside my dynamic-feature module when trying to use butterknife (error: package R2 does not exist)

enter image description here

Any ideas? Thanks in advance!


Solution

  • Finally I was able to solve the issue. The documentation only mentions library sub-projects, not dynamic features. Since I was migrating from a library, I assumed the same configuration should be applied and I got the issue. Turns out that despite the lack of information in the official documentation (here is the link just in case they update it)

    https://github.com/JakeWharton/butterknife

    R works just fine in modules marked with apply plugin: 'com.android.dynamic-feature' the same way you would work on the base :app module. My issue was a silly mistake not butterknife related, I fixed it and everything works as it should.

    Now summing up, for dynamic-feature modules, just use R instead of R2 in order to perform the binding. The above code is working as is (only with this change).