Butterknife does not find views from Android library module

I have 3 module in my project: common, adminand client.

The first is an Android library with an abstract LoginActivityfrom which other modules (applications) are distributed.

LoginActivityhas its own layout and all implemented methods, but one: tryLogin(user, password)that moves to the main action, which is different for each application.

I followed the official documentation , but when I click the button signIn, OnClickListener is not called. Moreover, all related views have a null value ...

This is all related code from admin(both modules are almost identical)

Build.gradle project level

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath "io.realm:realm-gradle-plugin:1.1.1"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'
        classpath 'io.fabric.tools:gradle:1.21.7'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
}

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

Shared build.gradle library

apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'android-apt'

android {

    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "0.1"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

    ...

    compile 'com.jakewharton:butterknife:8.2.1'
}

Admin app build.gradle

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'

android {

    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "..."
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "0.1"
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    ...

    // Butterknife
    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

LoginActivity ()

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;

import xxx.common.R;
import xxx.common.R2;

import butterknife.BindView;
import butterknife.OnClick;

public abstract class LoginActivity extends BaseActivity {

    @BindView(R2.id.login_user_form)
    protected EditText user;

    @BindView(R2.id.login_password_form)
    protected EditText password;

    @BindView(R2.id.login_sign_in)
    protected Button signIn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick(R2.id.login_sign_in)
    protected void onSignInClick() {
        String user = this.user.toString();
        String password = this.password.getText().toString();

        /* Fields validations ... */

        tryLogin(user, password);
    }

    protected abstract void tryLogin(String user, String password);

}

activity_login.xml()

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="0.4"
        android:scaleType="fitCenter"
        android:src="@drawable/brand" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/login_user_form"
                style="@style/LoginEditText" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/login_password_form"
                style="@style/LoginEditText" />

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/login_sign_in"
            style="@style/LoginButton"
            android:text="@string/login_sign_in" />

    </LinearLayout>

</LinearLayout>

AdminLoginActivity (admin)

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import xxx.activity.LoginActivity;

import butterknife.ButterKnife;

public class AdminLoginActivity extends LoginActivity {

    @Override
    protected void tryLogin(String user, String password) {
        // This is never called!!
        Toast.makeText(this, "Login in...", Toast.LENGTH_SHORT).show();
    }

}

- , .

?

+4

Source: https://habr.com/ru/post/1650805/


All Articles