Why Active Android is not working with gradle 2.0?
Asked Answered
I

6

16

I am using Active Android in my app. It was working fine till I upgraded my Android Studio to 2.0 from 1.3. With this upgrade my gradle also got upgraded to 2.0 which is causing some issue with the Active Android.

I am getting this error when building with gradle 2.0.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.activeandroid.TableInfo.getTableName()' on a null object reference
                                                                       at com.activeandroid.Cache.getTableName(Cache.java:156)
                                                                       at com.activeandroid.query.From.addFrom(From.java:169)
                                                                       at com.activeandroid.query.From.toSql(From.java:250)
                                                                       at com.activeandroid.query.From.execute(From.java:298)

I tried building my old studio with gradle 1.3 it is still working fine. Any help please?

build.gradle file when app is giving the above error

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.google.gms:google-services:2.0.0-beta2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build.gradle when app is working fine

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:2.0.0-beta2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

gradle wrapper properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
Isonomy answered 8/4, 2016 at 2:35 Comment(2)
Post gradle wrapper version. Post build.gradle files.Stoneblind
If you are using Android studio 2.0, you plugin should be 2.0+. Can you run your app via gradlew via command line?Stoneblind
D
29

I ran into this problem too, I found it's only an issue on Android 23+ devices, and it can be avoided if you turn Instant Run off (File->Settings->Build, Execution, Deployment->Instant Run).

If you want to keep Instant Run you can try UnChecking "Restart Activity on Code Changes"

On Mac:

Preferences > Build, Execution, Deployment > Instant Run > Uncheck "Restart Activity on Code Changes"

Displeasure answered 8/4, 2016 at 21:55 Comment(4)
That worked, seems like a problem with Instant Run. Its sad because I wanted to use it so badly.Isonomy
I faced with it on Android 22Argal
Thanks man, I've had this issue as well... This is appalling.Arturoartus
OMG I just lost sooo much time with this error. But now it's working thanks to your post.Frowst
O
9

There is a problem with ActiveAndroid which is not being able to retrieve the Model classes searching in the DexFile when Instant run is activated Some info about DexFile and Instant run here

There are three possible workarounds:

  1. Disable Intant run Android Studio -> Preferences -> Intant run
  2. Add the already suggested code in AndroidManifest:
<meta-data
    android:name="AA_MODELS"
    android:value="com.myapp.model.Item, com.myapp.model.Category" />
  1. Add the following code in the ActiveAndroid intialization:
Configuration.Builder config = new Configuration.Builder(this);
config.addModelClasses(Model1.class, Model2.class);
ActiveAndroid.initialize(config.create());

Hope it helps

Odelet answered 27/4, 2016 at 10:38 Comment(1)
I was facing same problem but this solution is working, i was making mistake not adding the models in manifest or in configuration when initializing dynamically.Pyrrha
B
7

I met the same problem,but I don't know why.I have specified my Model classes explicitely in my AndroidManifest:

<meta-data
    android:name="AA_MODELS"
    android:value="com.myapp.model.Item, com.myapp.model.Category" />

It's resolved;

Bombard answered 8/4, 2016 at 3:17 Comment(1)
It resolved the issue but doing this makes my app run really slow when it runs for the first time. Also it increases my data size and decreases app size. Could you confirm if this is also happening in your case?Isonomy
T
1

i have same issue on gradle 2.0, but i dont why this error present ,, finally i solved it by downgrade the gradle version to gradle 1.5.0

Turkmen answered 11/4, 2016 at 18:27 Comment(0)
H
0

I had same problem with active android ORM. Not working only on the emulator. Switching off the instant run. And a complete reinstall of the app, Cleared the error.

Himyarite answered 27/4, 2016 at 19:9 Comment(0)
G
0

To add to the answer provided by VictorG you could avoid disabling instant run. Since the basic problem is that Cache doesn't retain the ModelInfo values due to DexFile changes caused by instant run you could check if the ModelInfo contains any table data and reinitialize ActiveAndroid if needed.

if (Cache.isInitialized() && Cache.getTableInfos().isEmpty()) {
    ActiveAndroid.dispose();
}
ActiveAndroid.initialize(HOWEVER_YOU_BUILD_YOUR_CONFIG);
Galvanoscope answered 23/7, 2016 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.