SqueezyMo SqueezyMo - 9 months ago 76
Android Question

Kotlin: @Ignore'd properties persisted by Sugar ORM

In my Android app, I'm using Kotlin in conjunction with SugarORM and I have encountered an issue trying to prevent some properties from being persisted. Ironically, the

annotation seems to be ignored when used in Kotlin classes.

As an example,

1) let's declare two seemingly identical models:

// JavaUser.java
public class JavaUser extends SugarRecord {
public String login = "login";
@Ignore public String password = "password";

// KotlinUser.kt
class KotlinUser : SugarRecord() {
var login: String = "login"
@Ignore var password: String = "password"

2) persist their instances


3) and take a look at what's actually being persisted:

sqlite> select * from java_user;

sqlite> select * from kotlin_user;

I realize that it may have something to do with Kotlin annotation processing but I'm just not sure how I can go about it. Any suggestions are most welcome.

Answer Source

The core difference between your Java and Kotlin code is that in Java you use fields, but in Kotlin you use properties. See the Properties and Fields section in documentation.

You may try the following solutions and see what works best with SugarORM:

1. Make Kotlin expose fields:

@Ignore @JvmField var password: String = "password"

2. Apply your annotation to the private backing field:

@field:Ignore var password: String = "password"