Search code examples
androidandroid-ndkfirebase-cloud-messagingndk-buildgradle-experimental

Ndk build with gradle experimental android model plugin causes java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.


I try to assemble NDK app with Firebase Cloud Messaging using android model plugin with gradle-experimental. When I call

FirebaseInstanceId.getInstance().getToken()

The Application invokes

java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.

If I switch to com.android.application plugin from com.android.model.application plugin then everything works fine but unfortunately I need to build ndk project with Cloud Messaging. Do you have any idea how to solve this? Gradle file:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 24
        buildToolsVersion = "24.0.2"

      /*  defaultConfig {
        applicationId = "com.myapppackage.myappname"
        minSdkVersion.apiLevel = 14
        targetSdkVersion.apiLevel = 24
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
    }*/
    defaultConfig {
        applicationId = "com.myapppackage.myappname"
    }

    buildTypes {

        release {
            minifyEnabled = true
            useProguard = true
            proguardFiles.add(file('proguard-rules.pro'))
            ndk.with {
                debuggable = false
            }
        }
        debug {
            minifyEnabled = true
            useProguard = true
            debuggable = true
            proguardFiles.add(file('proguard-rules.pro'))
            ndk.with {
                debuggable = false
            }
        }
    }


    compileOptions.with {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    ndk {
        platformVersion = "14"
        moduleName = "testndk"
        toolchain = "clang"
        ldLibs.add("log")
    }


    sources {
        main {
            jni {
                source {
                    srcDirs.removeAll()
                    srcDirs.add('src/main/jni/netguard')
                }
                exportedHeaders {
                }
            }
        }
    }

    productFlavors {
        create("all") {
            versionName = "1.00"
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 24
            versionCode = 100
            archivesBaseName = "MyAppName-v$versionName"
        }
        create("lollipop-and-later") {
            versionName = "1.00"
            minSdkVersion.apiLevel = 20
            targetSdkVersion.apiLevel = 24
            versionCode = 100
            archivesBaseName = "MyAppName-v$versionName"
        }
        create("kitkat-and-earlier") {
            versionName = "1.00"
            minSdkVersion.apiLevel = 14
            maxSdkVersion = 19
            targetSdkVersion.apiLevel = 24
            versionCode = 100
            archivesBaseName = "MyAppName-v$versionName"
        }
    }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-ads:9.4.0'
    testCompile 'junit:junit:4.12'
}

And proguard-rules.pro file:

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-dontobfuscate
-dontoptimize
-keep class android.support.v7.widget.** { *; }
-dontwarn android.support.v4.**
-keep public class com.google.android.gms.** {*;}
-dontwarn com.google.android.gms.**
-keep public class com.google.firebase.** {*;}
-dontwarn com.google.firebase.**

Solution

  • The solution to build such app at the time when gradle-experimental produced IllegalStateException ( FirebaseApp with name [DEFAULT] doesn't exist) was to build experimental native module as library and use Firebase in stable gradle module by this example:

    Android Native Library Module (and Debug)