0

我尝试使用带有 gradle-experimental 的 android 模型插件将 NDK 应用程序与 Firebase Cloud Messaging 组装在一起。当我打电话

FirebaseInstanceId.getInstance().getToken()

应用程序调用

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

如果我从 com.android.model.application 插件切换到 com.android.application 插件,那么一切正常,但不幸的是我需要使用 Cloud Messaging 构建 ndk 项目。你知道如何解决这个问题吗?摇篮文件:

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'
}

和 proguard-rules.pro 文件:

-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.**
4

1 回答 1

2

在 gradle-experimental 产生 IllegalStateException (名称为 [DEFAULT] 的 FirebaseApp 不存在)时构建此类应用程序的解决方案是将实验性本机模块构建为库,并通过此示例在稳定的 gradle 模块中使用 Firebase:

Android 原生库模块(和调试)

于 2016-08-31T07:45:06.827 回答