Realm.io / Dagger / Databinding在同一个项目中

时间:2015-11-25 14:51:12

标签: android realm dagger-2 android-databinding

在通过gradle添加Realm.io作为依赖项后,我遇到了编译项目的问题。无法找到由dagger和数据绑定创建的生成文件。如果我删除了realm.io,应用程序会正确编译。

这是我的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.android.databinding'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    multiDexEnabled true
    applicationId "com.foo"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.facebook.stetho:stetho:1.2.0'
compile 'com.facebook.stetho:stetho-okhttp:1.2.0'
compile 'io.reactivex:rxandroid:0.24.0'
compile 'io.reactivex:rxjava:1.0.14'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okio:okio:1.4.0'
compile 'com.google.code.gson:gson:2.3'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:design:23.1.0'
compile 'com.jakewharton.timber:timber:4.1.0'
compile 'io.realm:realm-android:0.85.1'

compile 'com.google.dagger:dagger:2.0.1'
provided 'javax.annotation:jsr250-api:1.0'
apt "com.google.dagger:dagger-compiler:2.0.1"
apt 'com.android.databinding:compiler:1.0-rc4'
}

Error

我看到Realm也在生成文件,也许编译器不能很好地协同工作。关于如何使这个工作的任何想法?

由于

4 个答案:

答案 0 :(得分:2)

我看到你也收到了错误:

  

没有为field goalInfo找到setter

确保字段名称与getter和setter相同。不要在前面添加" m"到字段名称。例如:

@RealmClass
public Goal extends RealmObject {
    //private String mGoalInfo;
    private String goalInfo;

    public String getGoalInfo() {
        return goalInfo;
    }

    public void setGoalInfo(String goalInfo) {
        this.goalInfo = goalInfo;
    }
}

答案 1 :(得分:2)

Databinding works with RealmObjects,但你必须实现Observable。

public class Post extends RealmObject implements Observable {
    @PrimaryKey
    private long id;

    private String text;

    @Ignore
    private transient PropertyChangeRegistry mCallbacks;

    @Bindable
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
        if(!isValid()) { // !isManaged() in Realm 2.0
            notifyPropertyChanged(BR.id);
        }
    }

    @Bindable
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        if(!isValid()) { // !isManaged() in Realm 2.0
            notifyPropertyChanged(BR.text);
        }
    }

    @Override
    public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks == null) {
            mCallbacks = new PropertyChangeRegistry();
        }
        mCallbacks.add(callback);
    }

    @Override
    public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }

    /**
     * Notifies listeners that all properties of this instance have changed.
     */
    public synchronized void notifyChange() {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, 0, null);
        }
    }

    /**
     * Notifies listeners that a specific property has changed. The getter for the property
     * that changes should be marked with {@link Bindable} to generate a field in
     * <code>BR</code> to be used as <code>fieldId</code>.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    public void notifyPropertyChanged(int fieldId) {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, fieldId, null);
        }
    }
}

Dagger + Databinding会相互破坏,需要添加

apt 'com.google.guava:guava:19.0' // dagger + databind workaround

答案 2 :(得分:1)

Realm,Dagger2和Databinding都在我的项目中工作。

区别在于:

我正在使用android gradle插件1.5.0并通过以下配置启用数据绑定。

android {
    ...
    dataBinding {
        enabled = true
    }
    ...
}

我没有

apt 'com.android.databinding:compiler:1.0-rc4'

in dependencies。

整个工作项目在这里:https://github.com/zaki50/realm_template

答案 3 :(得分:0)

现在可以实现RealmModel接口并向类中添加注释@RealmClass,而不是扩展RealmObject。这使您可以扩展BaseObservable,但它仍然无法正常工作。

您收到数据绑定错误:包mypackage.databinding不存在

在github上查看此问题:https://github.com/realm/realm-java/issues/2716