Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem initializing ActiveAndroid in API 21 #283

Open
JesusM opened this issue Oct 19, 2014 · 7 comments
Open

Problem initializing ActiveAndroid in API 21 #283

JesusM opened this issue Oct 19, 2014 · 7 comments

Comments

@JesusM
Copy link

JesusM commented Oct 19, 2014

When I'm trying to initialize ActiveAndroid in Android 5.0, it crashes with this error: java.lang.NoClassDefFoundError: org.mockito.internal.runners.RunnerImpl . I read about this and it seems to be related with ART.

related links:
mockito/mockito#71
https://code.google.com/p/dexmaker/issues/detail?id=39

@mpfeiffermway
Copy link
Contributor

I can confirm this issue.

@roms
Copy link

roms commented Oct 22, 2014

I get a very similar, but not exactly the same, error on initialization:

java.lang.NoClassDefFoundError: com.google.android.gms.internal.ir
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:308)
at com.activeandroid.ModelInfo.scanForModelClasses(ModelInfo.java:185)
at com.activeandroid.ModelInfo.scanForModel(ModelInfo.java:150)
at com.activeandroid.ModelInfo.(ModelInfo.java:61)
at com.activeandroid.Cache.initialize(Cache.java:66)
at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:44)
at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:34)

I'm not so sure its an ART issue as it worked on my Nexus 5 running 4.4.2 with ART. The initialization error happens on Android 5.0.

@mpfeiffermway
Copy link
Contributor

After some debugging it seems that in my case ActiveAndroid chokes on a class that is included in another lib (android-logging-log4j-1.0.3.jar). It dies with a NoClassDefFoundError. As @roms mentions this only happens on Android 5.0 (emulator), not on older versions.

I've added a catch for this particular error and now it seems to work again:

    private void scanForModelClasses(File path, String packageName, ClassLoader classLoader) {

[...]

            try {
                Class<?> discoveredClass = Class.forName(className, false, classLoader);
                if (ReflectionUtils.isModel(discoveredClass)) {
                    @SuppressWarnings("unchecked")
                    Class<? extends Model> modelClass = (Class<? extends Model>) discoveredClass;
                    mTableInfos.put(modelClass, new TableInfo(modelClass));
                }
                else if (ReflectionUtils.isTypeSerializer(discoveredClass)) {
                    TypeSerializer instance = (TypeSerializer) discoveredClass.newInstance();
                    mTypeSerializers.put(instance.getDeserializedType(), instance);
                }
            }
/// ADDED
            catch (NoClassDefFoundError e) {
                Log.e("Couldn't find class definition.", e);
            }
/// ADDED
            catch (ClassNotFoundException e) {
                Log.e("Couldn't create class.", e);
            }
            catch (InstantiationException e) {
                Log.e("Couldn't instantiate TypeSerializer.", e);
            }
            catch (IllegalAccessException e) {
                Log.e("IllegalAccessException", e);
            }
        }
    }

Not sure if this is the correct solution, but it seems to work for now.

@mpfeiffermway
Copy link
Contributor

Seems like there's already a pull request that should fix this: #277. And another pull request that might be of interest in this case: #247.

@mirkomane
Copy link

I get this error in Android 5 on Nexus 4 and Nexus 5:

java.lang.NoClassDefFoundError: org.codehaus.jackson.jaxrs.JacksonJsonProvider
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:308)
at com.activeandroid.ModelInfo.scanForModelClasses(ModelInfo.java:187)
at com.activeandroid.ModelInfo.scanForModel(ModelInfo.java:152)
at com.activeandroid.ModelInfo.(ModelInfo.java:63)
at com.activeandroid.Cache.initialize(Cache.java:66)
at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:44)
at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:34)
at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:30)
at com.activeandroid.app.Application.onCreate(Application.java:25)

The problem seems to be that my application class extends the ActiveAndroid application class.
I solved this problem extending my application class with android.app.Application and calling a method that disables model auto searching and defines models in my Application object. Finally I call ActiveAndroid.initialize() method.

public class MyApplicationClass extends Application {
@OverRide
public void onCreate() {
super.onCreate();
initializeDB();
MyDatabase.init();
}
/* This method disable model auto searching defining models in the Application object. */
protected void initializeDB() {
Configuration.Builder configurationBuilder = new Configuration.Builder(this);
configurationBuilder.addModelClasses(MyClassModel1.class);
configurationBuilder.addModelClasses(MyClassModel2.class);
configurationBuilder.addModelClasses(MyClassModel3.class);

         ActiveAndroid.initialize(configurationBuilder.create());
    }

}

@matbos
Copy link

matbos commented May 13, 2015

Downloading source code and building the jar solves the problem.

@juanmacuevas
Copy link

Can I expect a release in maven repo with this issue fixed anytime soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants