将RealmObject类转换为Kotlin时编译时错误

时间:2017-05-25 09:54:07

标签: android realm kotlin parceler

这是我原来的RealmObject Parceler注释。

@Parcel(implementations = {AlbumRealmProxy.class},
        value = Parcel.Serialization.BEAN,
        analyze = {Album.class})
public class Album extends RealmObject {

    @PrimaryKey
    @SerializedName("id")
    private String id;
    @SerializedName("artist_id")
    private String artistId;
    @SerializedName("title")
    private String title;
    @SerializedName("artist_name")
    private String artist;
    @SerializedName("images")
    private RealmList<Artwork> artwork;
    @SerializedName("tracks")
    private RealmList<Track> tracks;
    @SerializedName("artist_bio")
    private String artistBio;

    @ParcelPropertyConverter(RealmListParcelConverter.class)
    public void setArtwork(RealmList<Artwork> artwork) {
        this.artwork = artwork;
    }

    @ParcelPropertyConverter(RealmListParcelConverter.class)
    public void setTracks(RealmList<Track> tracks){
        this.tracks = tracks;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public RealmList<Artwork> getArtwork() {
        return artwork;
    }

    public String getArtist() {
        return artist;
    }

    public String getArtistId() {
        return artistId;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public RealmList<Track> getTracks() {
        return tracks;
    }

    public String getArtistBio() {
        return artistBio;
    }
}

在转换为Kotlin时,我得到以下生成的类;

@Parcel(implementations = arrayOf(AlbumRealmProxy::class), value = Parcel.Serialization.BEAN, analyze = arrayOf(Album::class))
open class Album : RealmObject() {

    @PrimaryKey
    @SerializedName("id")
    var id: String? = null
    @SerializedName("artist_id")
    val artistId: String? = null
    @SerializedName("title")
    var title: String? = null
    @SerializedName("artist_name")
    var artist: String? = null
    @SerializedName("images")
    @set:ParcelPropertyConverter(RealmListParcelConverter::class)
    open var artwork: RealmList<Artwork>? = null
    @SerializedName("tracks")
    @set:ParcelPropertyConverter(RealmListParcelConverter::class)
    open var tracks: RealmList<Track>? = null
    @SerializedName("artist_bio")
    val artistBio: String? = null
}

编译时错误如下:

Error:(5, 17) Unresolved reference: AlbumRealmProxy
Error:(16, 27) Only 'const val' can be used in constant expressions
Error:(16, 35) Unresolved reference: AlbumRealmProxy
Error:(16, 35) An annotation parameter must be a compile-time constant

[KOTLIN] deleting /Users/dev/RAG Apps/Songa/app/build/tmp/kotlin-classes/debug on error
[KOTLIN] deleting /Users/dev/RAG Apps/Songa/app/build/tmp/kotlin-classes/debug on error

我正在为Kotlin kapt使用Parceler注释处理器,但项目仍然无法编译。

是否需要一些额外的配置?

1 个答案:

答案 0 :(得分:0)

就我而言,我有这个包:com.path.model.Album,我必须使用:

import io.realm.com_path_model_AlbumRealmProxy

...
@Parcel(implementations = arrayOf(com_path_model_AlbumRealmProxy::class)