chennaione / sugar

Insanely easy way to work with Android Database.
http://satyan.github.com/sugar/
MIT License
2.62k stars 583 forks source link

Class not found in getDomainClass #519

Closed lupsyn closed 8 years ago

lupsyn commented 8 years ago

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

JonatanSalas commented 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!

lupsyn commented 8 years ago

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()))
JonatanSalas commented 8 years ago

Would you like to show me the metadata configuration that you have defined in the AndroidManifest?

lupsyn commented 8 years ago

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>
JonatanSalas commented 8 years ago

It seems to be ok! Did you use multiDex in your project?

lupsyn commented 8 years ago

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.

lupsyn commented 8 years ago

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.

JonatanSalas commented 8 years ago

Ok, I will try to help you!

lupsyn commented 8 years ago

Change line 278 in getDomainClass in : discoveredClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());

JonatanSalas commented 8 years ago

@lupsyn if this is the solution please submit a pull-request!

JonatanSalas commented 8 years ago

This is solved, thanks @lupsyn!

RossinesP commented 8 years ago

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.

RossinesP commented 8 years ago

Reverting to the previous line made it work again. discoveredClass = Class.forName(className, true, ContextUtil.getContext().getClass().getClassLoader());