数据绑定:ButterKnife,Kotlin:不生成绑定文件

时间:2017-11-28 10:01:17

标签: android android-databinding

Android Studio 3.0

在project / build.gradle中

classpath 'com.android.tools.build:gradle:3.0.1'
在app / build.gradle中

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(rootProject.file("app/keystore.properties")))

android {
    dataBinding {
        enabled = true
    }
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.myproject.android.customer"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "0.1.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //multiDexEnabled true
    }
    signingConfigs {
        release {
            keyAlias keystoreProperties['KEY_ALIAS_RELEASE']
            keyPassword keystoreProperties['KEY_PASSWORD_RELEASE']
            storeFile file(keystoreProperties['STORE_FILE_RELEASE'])
            storePassword keystoreProperties['STORE_PASSWORD_RELEASE']
        }
    }

    /*- exclude buildTypes = "debug" from build Variants
    variantFilter { variant ->
        if (variant.buildType.name.equals('debug')) {
            variant.setIgnore(true);
        }
    }*/


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

            // sign settings
            signingConfig signingConfigs.release

            // config Fabrice (Beta of Crashlytics)
            ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
            ext.betaDistributionEmailsFilePath = "app/beta_distribution_emails.txt"
        }
        debug {
            ext.alwaysUpdateBuildId = false // not need CrashLytics on debug mode

        }
    }

} 

依赖{     实现fileTree(dir:'libs',包括:['* .jar'])

implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }

implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
// must use pdf-viewer:1.6.0
implementation 'com.github.barteksc:android-pdf-viewer:1.6.0'
//implementation 'com.github.markomilos:paginate:0.5.1'
implementation 'com.google.android.gms:play-services-location:11.4.2'
implementation 'com.google.android.gms:play-services-maps:11.4.2'
implementation "com.github.bumptech.glide:glide:$GLIDE_VERSION"
implementation "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION"
implementation 'com.jayway.jsonpath:json-path:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'commons-io:commons-io:2.5'
implementation 'io.realm:android-adapters:2.0.0'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$KOTLIN_VERSION"

implementation project(':common')

kapt "com.github.bumptech.glide:compiler:$GLIDE_VERSION"
kapt "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"

// test dependencies
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

}

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

我的布局名称是:offer_item.xml

因此,结果绑定文件名为:OfferItemBinding

这里是我的RecyclerViewAdapter:

import com.myproject.android.customer.databinding.OfferItemBinding;

    public OfferSortAdapter.OfferViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            OfferItemBinding binding = OfferItemBinding.inflate(inflater, parent, false);
            return new OfferViewHolder(binding.getRoot(), this);
        }

这里是xml布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="offer"
            type="com.myproject.android.customer.api.model.Offer" />
    </data>
</layout>

未生成文件OfferItemBinding

我得到错误:

    D:\dev\MyProject\app\src\main\java\com\myproject\android\customer\ui\adapter\OfferSortAdapter.java:29:

error: package com.myproject.android.customer.databinding does not exist
    import com.myproject.android.customer.databinding.OfferItemBinding;

1 个答案:

答案 0 :(得分:1)

  

在build.gradle

中试试
    dependencies {
        //for butterknife
        implementation 'com.jakewharton:butterknife:8.8.1'
        kapt 'com.jakewharton:butterknife-compiler:8.8.1'

        implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
        kapt "com.android.databinding:compiler:2.3.3"
    }


apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'

kapt {
    generateStubs = true
}
  在您的RecylerviewAdpater中

public class MyViewHolder extends RecyclerView.ViewHolder {
private final ItemBinding binding;

public MyViewHolder(ItemBinding binding) {
    super(binding.getRoot());
    this.binding = binding;
}

public void bind(Item item) {
    binding.setItem(item);
    binding.executePendingBindings();
}
}
  

现在,我的适配器可以使用Android数据绑定创建和绑定:

    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    LayoutInflater layoutInflater =
        LayoutInflater.from(parent.getContext());
    ItemBinding itemBinding = 
        ItemBinding.inflate(layoutInflater, parent, false);
    return new MyViewHolder(itemBinding);
}

public void onBindViewHolder(MyViewHolder holder, int position) {
    Item item = getItemForPosition(position);
    holder.bind(item);
}