Search code examples
androidandroid-studiobutterknife

How to integrate Butterknife in android library?


I am working on existing project in android and I want to convert Android project to Android Library. In that project butterknife library is used for binding UI components. When I tried to convert project to library using reference the project synced successfully but all file where butterknife is used, I am getting below error "Attribute value must be constant" for all @BindView components.

Below is the changes I made on gradle file.

  1. App level gradle :
    apply plugin: 'com.android.library'
    apply plugin: 'com.jakewharton.butterknife'
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    def verionMajor = 2
    def versionMinor = 1
    def versionPatch = 1
    
    android {
        compileSdkVersion 28
        flavorDimensions "general"
    
        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 6
            versionName "$verionMajor.$versionMinor.$versionPatch"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        packagingOptions {
            exclude 'META-INF/proguard/androidx-annotations.pro'
        }
    
        productFlavors {
            general {
                buildConfigField 'String', 'appName', '"Finetra"'
                buildConfigField 'String', 'applicationType', '"General"'
                resValue "string", "app_name", "Finetra"
            }
    
            flavor1 {
                buildConfigField 'String', 'appName', '"Finetra"'
                buildConfigField 'String', 'applicationType', '"Flavor"'
                resValue "string", "app_name", "Finetra"
            }
        }
    
        dexOptions {
            javaMaxHeapSize "4g" //specify the heap size for the dex process
        }
    
        packagingOptions {
            exclude 'META-INF/atomicfu.kotlin_module'
        }
    
    }
    
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    repositories {
        maven { url 'http://dl.bintray.com/dev-fingerlinks/maven' }
        maven { url "https://jitpack.io" }
    
        dependencies {
            configurations {
                all*.exclude group: 'com.android.support', module: 'support-v13'
            }
            implementation fileTree(dir: 'libs', include: ['*.jar'])
            implementation 'androidx.appcompat:appcompat:1.1.0'
            implementation 'androidx.multidex:multidex:2.0.1'
            implementation 'com.android.support:design:28.0.0'
            implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
            implementation "androidx.cardview:cardview:1.0.0"
    
            testImplementation 'junit:junit:4.12'
            androidTestImplementation 'com.android.support.test:runner:1.0.2'
            androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    
            implementation 'com.google.firebase:firebase-messaging:17.6.0'
            implementation 'com.google.firebase:firebase-core:16.0.5'
    
            implementation 'com.jakewharton:butterknife:10.2.3'
            annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
    
            implementation 'com.github.lzyzsd:circleprogress:1.2.1'
    
            implementation 'org.fingerlinks.mobile.android:Navigator:0.1.5'
    
            implementation 'com.loopj.android:android-async-http:1.4.9'
            implementation 'com.google.code.gson:gson:2.8.5'
    
            implementation 'com.squareup.retrofit2:retrofit:2.1.0'
            implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
            implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
    
            implementation 'com.wdullaer:materialdatetimepicker:3.0.0'
    
            implementation 'com.pixplicity.easyprefs:library:1.9.0'
    
            
            implementation 'com.facebook.fresco:fresco:2.0.0'
            implementation 'com.facebook.fresco:animated-gif:1.9.0'
    
            implementation 'com.github.crosswall:Android-Coverflow:release-v1.0.5'
    
            implementation 'io.github.inflationx:calligraphy3:3.1.1'
            implementation 'io.github.inflationx:viewpump:2.0.3'
    
            implementation 'com.kaopiz:kprogresshud:1.2.0'
    
            implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    
            implementation 'com.mikhaellopez:circularimageview:3.2.0'
    
            implementation 'com.google.android.gms:play-services-auth:17.0.0'
    
            implementation "com.priyankvasa.android:cameraview-ex:3.5.5-alpha"
    
            implementation(name: 'highcharts-release', ext: 'aar')
    
            implementation 'com.bignerdranch.android:expandablerecyclerview:3.0.0-RC1'
    
            implementation 'com.github.warkiz.widget:indicatorseekbar:2.1.2'
    
           
            implementation 'com.google.firebase:firebase-analytics:17.2.2'
        }
    }
    
    ext {
        versions = [
                'javacv': '1.4.2',
                'ffmpeg': '4.0.1'
        ]
    }
    
    dependencies {
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.41"
    
        implementation(group: 'org.bytedeco', name: 'javacv-platform', version: versions.javacv) {
            exclude group: 'org.bytedeco.javacpp-presets'
        }
        implementation group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: "${versions.ffmpeg}-${versions.javacv}"
        implementation group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: "${versions.ffmpeg}-${versions.javacv}", classifier: 'android-arm'
        implementation group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: "${versions.ffmpeg}-${versions.javacv}", classifier: 'android-arm64'
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'com.google.android.material:material:1.0.0'
        implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
        implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
        implementation project(path: ':mylibrary')
    }
    
    android {
       
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }

  1. Project level gradle :

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

    buildscript {
        ext.kotlin_version = '1.3.50'
    
    
        repositories {
            mavenCentral()
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.3'
            classpath 'com.google.gms:google-services:4.3.3'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
            // Add the Crashlytics Gradle plugin.
    //        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.0.0-beta02'
    
            classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
    
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
            flatDir {
                dirs 'libs'
            }
        }
    }
    
    ext {
        rxJava = '2.2.0'
        rxAndroid = '2.1.0'
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

I also refer butterknife doc and made changes accordingly. But I am not able to resolve the error for R2.

@BindView(R2.id.user) EditText userName;

How can I achieve this?

Any help will be appreciated.


Solution

  • ButterKnife can't be used in library projects. Library developer JakeWharton itself confirmed. Use view binding instead.

    android {
        ...
        buildFeatures {
            viewBinding = true
        }
    }