Closed lupsyn closed 8 years ago
Hi!
I strong recommend you not use the method SugarContext.init().
The best way is to override the aplication declaration over the AndroidManifest.xml file.
Check the sugar documentation! And let me know if this help you!
Yes sure, in the end i've used android:name not to Sugar.init, anyway, to replicate error try to use sugar in a module, and using the Application context of the app. You will see getDomainClass can't found sugar extensions objects and it's really strange... it seems this if is always false :
if ((discoveredClass != null) &&
((SugarRecord.class.isAssignableFrom(discoveredClass) &&
!SugarRecord.class.equals(discoveredClass)) ||
discoveredClass.isAnnotationPresent(Table.class)) &&
!Modifier.isAbstract(discoveredClass.getModifiers()))
Would you like to show me the metadata configuration that you have defined in the AndroidManifest?
Metadata obiuvsly is in the module not in the app that is using library :
<application
android:allowBackup="true" >
<meta-data
android:name="QUERY_LOG"
android:value="true"
<meta-data
android:name="DATABASE"
android:value="mobilesdkinternal.db"/>
<meta-data
android:name="VERSION"
android:value="1"/>
<meta-data
android:name="DOMAIN_PACKAGE_NAME"
android:value="com.mylibrary.sugarobjecs"/>
</application>
It seems to be ok! Did you use multiDex in your project?
I've disabled multidex, and proguard. I'm using dagger2 but i don't think classes generated by this tool can cause problems, as i said problem is when you try to init Sugar context by Application one.
Bug is replicated here : https://github.com/lupsyn/sugartest as you see, when you try to save the testObject, classes by getDomainClasses are not found.
Ok, I will try to help you!
Change line 278 in getDomainClass in : discoveredClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
@lupsyn if this is the solution please submit a pull-request!
This is solved, thanks @lupsyn!
Hi,
I have the same issue on SugarOrm 1.5 using the line from @lupsyn's comment. getDomainClass is called with the correct class name, but Class.forName(className, true, Thread.currentThread().getContextClassLoader()); throws an exception.
It is the first time I try to create a new table since I migrated to 1.5, I was using 1.3 with no issues before.
Reverting to the previous line made it work again.
discoveredClass = Class.forName(className, true, ContextUtil.getContext().getClass().getClassLoader());
Hi All, when i try to sugar.init with the activity context all is good but when i try to init with the Application context (MyActivity.getApplication()) something in getDomainClasses goes wrong. Reflection can't find sugar extensions classes. Is it normal ? I have to specify i'm using sugar in a module, and the application context is from the app where i'm using the library. (it makes no difference i think)... obiuvsly i've specified the domain_package_name. Has someone the same problem ? Thank you