Hot questions for Using Butter Knife in realm

Question:

My current Android Application was working fine with ButterKnife

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

I then added Realm and all my bindings fail with NPE

01-13 21:38:59.646 27712-27712/com.crucifix.software.coffeeshop D/ButterKnife: Looking up binding for com.crucifix.software.coffeeshop.MainActivity
01-13 21:38:59.647 27712-27712/com.crucifix.software.coffeeshop D/ButterKnife: Not found. Trying superclass com.crucifix.software.coffeeshop.BaseActivity
01-13 21:38:59.650 27712-27712/com.crucifix.software.coffeeshop D/ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity
01-13 21:38:59.650 27712-27712/com.crucifix.software.coffeeshop D/ButterKnife: MISS: Reached framework class. Abandoning search.

These are my build.gradle

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

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        classpath 'io.realm:realm-gradle-plugin:1.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.crucifix.software.coffeeshop"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

    compile 'com.yarolegovich:lovely-dialog:1.0.4'
    compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'
    compile group: 'com.thomashaertel', name: 'multispinner', version: '0.1.1'

    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:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'

    compile 'com.google.firebase:firebase-database:10.0.1'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

How do I resolve this issue?


Answer:

I think your project level gradle file is a mess. it should like something like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.2.3'
            classpath 'io.realm:realm-gradle-plugin:2.2.2'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            jcenter()
        }
    }

    task clean(type: Delete) {
        delete rootProject.buildDir
    }

While your app level build.gralde file should like something like this:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "your.app.id"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.android.support:cardview-v7:24.2.1'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

Question:

This is a part of my project level build.gradle:

buildscript {
    ext.kotlin_version = '1.1.51'
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        classpath "io.realm:realm-gradle-plugin:4.3.3"

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

aapplication level build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    defaultConfig {
        applicationId "com.reader.hanli.reader"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

realm {
    syncEnabled = true
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:' + rootProject.supportVersion
    implementation 'com.android.support:design:' + rootProject.supportVersion
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

Before i add the realm , butterknife is fine . And i don't received any exception thrown by butterknife , just a

NullPointer Exception

on the view. So what's the problem?Has anyone encountered the seem problem?


Answer:

You are using Kotlin, so you should have

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt' // <-- !!!!
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'realm-android'

And

kapt 'com.jakewharton:butterknife-compiler:8.4.0'