Error in gradle Android studio: "Cannot invoke method get() on null object"

10,115

if you use difference modules you must add firebase config to all android modules

therefor

  • add implementation 'com.google.firebase:firebase-core:x' to dependencies

  • add apply plugin: 'com.google.gms.google-services'

  • get firebase config ("google-service.json") for each android module and add to each module

Share:
10,115
Eran Shay
Author by

Eran Shay

Updated on June 29, 2022

Comments

  • Eran Shay
    Eran Shay almost 2 years

    I have a projects with number of modules, each one is a separate app. I added Firebase to one module, and since then another one's gradle cannot be sync. I keep getting: "Cannot invoke method get() on null object", which disappear when I comment out the play-service" dependencies. My gradle is:

    buildscript {
        repositories {
            google()
            maven { url 'https://maven.fabric.io/public' }
        }
    
        dependencies {
            classpath 'io.fabric.tools:gradle:1.+'
        }
    }
    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'
    
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    
    
    android {
        compileSdkVersion 27
        buildToolsVersion "27.0.3"
    
        defaultConfig {
            applicationId "xxxx"
            minSdkVersion 14
            targetSdkVersion 27
    
    //        multiDexEnabled true
    
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_7
                targetCompatibility JavaVersion.VERSION_1_7
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    
    }
    
    dependencies {
        implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
            transitive = true;
        }
        implementation 'com.android.support:support-v4:27.1.1'
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support:cardview-v7:27.1.1'
        implementation 'com.google.android.gms:play-services-location:15.0.1'
        implementation 'com.google.android.gms:play-services-maps:15.0.1'
    }
    

    The top-level project gradle:

    buildscript {
        repositories {
            jcenter()
            google()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.3'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            google()
        }
    }
    

    I use gradle version 4.4 and Android plugin version 3.1.3 The error's stack

    java.lang.NullPointerException: Cannot invoke method get() on null object
        at
     org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
        at
     org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
        at
     org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at
     org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at
     org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:57)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at com.google.gms.googleservices.GoogleServicesPlugin$1$_afterResolve_closure1.doCall(GoogleServicesPlugin.groovy:316)
        at
     sun.reflect.GeneratedMethodAccessor357.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at
     java.lang.reflect.Method.invoke(Method.java:498)
        at
     org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at
     groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at
     org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
        at
     groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
        at
     groovy.lang.Closure.call(Closure.java:414)
        at
     groovy.lang.Closure.call(Closure.java:430)
    
    (another 150 lines)
    

    Any suggestion will help, Cheers.

  • Eran Shay
    Eran Shay almost 6 years
    Thanks, my problem is that other modules don't use Firebase, and I don't want to add them. It seems to me very aggressive of Firebase to say if you use one module in your project you must use Firebase in all of your modules. By the way, my problem is with a Module that use "com.google.android.gms:play-services-location" but not Firebase. As soon as I comment out the "implementation 'com.google.android.gms:play-services-location:15.0.1'" from the other module it all works.
  • Onivas
    Onivas almost 6 years
    I was having the same issue after I added a new Wear module.... I did exactly what you said and it worked. I just need to edit the package_name inside the google-service.json based on the package name of the wear app. Thanks