5

通过 gradle 添加 Realm.io 作为依赖项后,我在编译项目时遇到问题。找不到dagger和databinding创建的生成文件。如果我删除 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'
}

错误

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

谢谢

4

4 回答 4

2

数据绑定适用于 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
于 2016-09-04T19:06:24.710 回答
2

我看到你也收到了错误:

找不到字段goalInfo的二传手

确保字段名称与 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;
    }
}
于 2016-01-17T21:05:19.960 回答
1

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

区别在于:

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

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

而我没有

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

在依赖项中。

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

于 2015-11-27T15:00:32.340 回答
0

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

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

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

于 2016-05-08T08:49:55.977 回答