when i use ToOne , if i not use proguard in debug type ,my app work fine 。
If i use proguard , application will crash ,I have not been able to find out from the readme and the wiki about the issues that need proguard !
Could you post a stack trace?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxxx.yyyy, PID: 4580
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.yyyy/com.xxxx.yyyy.mvp.activity.SetActivity}: java.lang.RuntimeException: Callable threw exception
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2733)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2794)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
Caused by: java.lang.RuntimeException: Callable threw exception
at io.objectbox.BoxStore.a(Unknown Source)
at io.objectbox.query.Query.c(Unknown Source)
at com.xxxx.yyyy.persistence.b.e(Unknown Source)
at com.xxxx.yyyy.mvp.activity.SetActivity.f(Unknown Source)
at com.xxxx.yyyy.mvp.activity.SetActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6801)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2686)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2794)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
Caused by: io.objectbox.exception.DbException: Could not prepare entity field UserConfig.home (Lio/objectbox/relation/ToOne;) - does the field exist with the expected type?
at io.objectbox.query.Query.nativeFindUnique(Native Method)
at io.objectbox.query.Query$3.call(Unknown Source)
at io.objectbox.BoxStore.a(Unknown Source)
at io.objectbox.query.Query.c(Unknown Source)
at com.xxxx.yyyy.persistence.b.e(Unknown Source)
at com.xxxx.yyyy.mvp.activity.SetActivity.f(Unknown Source)
at com.xxxx.yyyy.mvp.activity.SetActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6801)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2686)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2794)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
@Entity
public class UserConfig {
@Id
public long id;
public ToOne<XXXXBean> home;
}
````
@Entity
public class XXXXBean {
@Id
public long id;
public String name
}
```
tl;dr; Do @Keep on entity or provide database field name for all fields.
Longer:
Objectbox config get field names from fields in code. So if you have "name" field for objectbox will be named "name".
But proguard changes(obfuscates) field names and your "name" field now is for example "a". Objectbox can't find field named "a" in config and you have error.
You can use @Keep annotation on whole class or use @NameInDb to let objectbox know which field is which one :)
I try to keep XXXXBean and use
@Entity
public class UserConfig {
@Id
public long id;
@NameInDb("home")
public ToOne<XXXXBean> home;
}
-keep class com.xxxx.yyyy.persistence.** { * ; }
it also not work Could not prepare entity field UserConfig.home (Lio/objectbox/relation/ToOne;) - does the field exist with the expected type?
@Mistic92 Actually the objectbox-android AAR artifact (added by the plugin automatically) already contributes rules to keep constructors and fields:
# Keep entity constructors
-keep @io.objectbox.annotation.Entity class * { <init>(...); }
# For relation ID fields
-keepclassmembers @io.objectbox.annotation.Entity class * {
<fields>;
}
@AllenCoder Could you check your ProGuard rules that you do not re-include some files (for example by adding the allowobfuscation keyword)? Can you inspect the class in your APK and check if the field exists?
-ut
if i just use your rule
# Keep entity constructors
-keep @io.objectbox.annotation.Entity class * { <init>(...); }
# For relation ID fields
-keepclassmembers @io.objectbox.annotation.Entity class * {
<fields>;
}
it also crash
i fix it by this rule,
# Keep entity constructors
-keep @io.objectbox.annotation.Entity class * { <init>(...); }
-keep class io.objectbox.**{*;}
# For relation ID fields
-keepclassmembers @io.objectbox.annotation.Entity class * {
<fields>;
}
I think ToOne and ToMany must keep
Could you verify if adding just the following rules works as well:
-keep class io.objectbox.relation.ToOne
-keep class io.objectbox.relation.ToMany
If so, those rules will be added with the next release.
-ut
I use your rules to test , it's ok 。
maybe just keep ToOne and ToMany
thank you !!!
We just released 1.2.1 - could you please verify that you do not need to define those proguard rules anymore?
i upgrade 1.2.1 and remove proguard rule ,it's also work ok
@greenrobot
v1.2.1 still problem with proguard and ToOne :
java.lang.NoSuchMethodError: no non-static method "Lio/objectbox/relation/ToOne;.setTargetId(J)V"
at io.objectbox.query.Query.nativeFindFirst(Native Method)
Fixed by adding:
-keep class io.objectbox.relation.ToOne { *; }
-keep class io.objectbox.relation.ToMany { *; }
@devjn Thanks for reporting! Can you verify that the following rules are enough (native ObjectBox code only accesses setTargetId(long)):
-keep class io.objectbox.relation.ToOne {
void setTargetId(long);
}
-keep class io.objectbox.relation.ToMany
Yep, it seem to work
Alright, the updated rules should be available with the next release. Until then you can add the ToOne rule above to your ProGuard files. -ut
Most helpful comment
Yep, it seem to work