2
votes

I'm trying to swith from gcm to fcm. I've followed the steps detailed in the gcm to fcm migration guide, although the FirebaseApp doesn't initializes.

I have identified couple possbile issues, however codenameone doesn't help solving them.

Apparently, google-services needs to be added to the buildscript dependencies, like this:

buildscript {
    repositories {
        ...
    }
    dependencies {

        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

However, I can't find any codenameone build hint to help me do that.

Also, the google-services plugin needs to be added, like this:

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

I have accomplished this by using android.gradlePlugin build hint:

codename1.arg.android.gradlePlugin=apply plugin\:'com.google.gms.google-services'

But the build fails complaining that it can't find the plugin, as it probably needs that defined as described above.

Also, I have seen that in most cases this line should apper after the "dependencies" section, not before, as the current build hint does.

Not really sure if this is an issue at this point.

I have also tried manually starting the FirebaseApp, via some native code, like this:

Context context = AndroidNativeUtil.getContext();
FirebaseApp.initializeApp(context);

but it doesn't help. No error is thrown, but when trying to retrieve the token via

String token = FirebaseInstanceId.getInstance().getToken();

I get an error like this:

IllegalStateException: Default FirebaseApp is not initialized in this process

My build.gradle file, generated by codenameone build servers looks like this:

apply plugin: 'com.android.application'

buildscript {
    repositories {
     jcenter()
     mavenLocal()
      mavenCentral()
      google()
    maven { url "https://maven.google.com" }
     mavenLocal()
      mavenCentral()
      google()
    }
    dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

android {
    compileSdkVersion 27
    buildToolsVersion '27'

    dexOptions {
        preDexLibraries = false
        incremental false
        jumboMode = true
        javaMaxHeapSize "3g"
    }
    defaultConfig {
        applicationId "<appid>"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 85
        versionName "0.85"
        multiDexEnabled true
    }
    sourceSets {
        main {
            aidl.srcDirs = ['src/main/java']
        }
    }

    lintOptions {
        lintOptions {
        checkReleaseBuilds false
        abortOnError false
        }
    }
    signingConfigs {
        release {
            storeFile file("keyStore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }
buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
            signingConfig signingConfigs.release
        }
        debug {
            signingConfig signingConfigs.release
        }
    }
}

repositories {
    jcenter()
    maven { url "https://maven.google.com" }
     mavenLocal()
      mavenCentral()
      google()
    flatDir{
              dirs 'libs'
       }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:27.+'
     implementation 'com.android.support:appcompat-v7:27.+'

 compile 'com.facebook.android:facebook-android-sdk:4.7.0'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.gms:google-services:4.1.0'
compile 'com.google.firebase:firebase-core:16.0.3'
compile 'com.google.firebase:firebase-messaging:17.3.0'}

Please help.

Update: So it seems that there's a need for two new build hints that can allow:

  1. Inject an apply plugin statement after the dependencies section
  2. Inject a clsspath statement in the buildscript dependencies section
1
Usually the apply plugin: 'com.google.gms.google-services' should go at the bottom of the app build.gradle file. Are you sure you are adding it at the end? - MatPag
I can't add it at the end, as I don't have direct access to the build.gradle. This is controlled by the codename one build server via build hints and currently the only option is to add it at the begining. - Adrian Ionescu
You need to find a solution for this to work, read here in the documentation: firebase.google.com/docs/android/setup#add_the_sdk in the part where is shown the app/build.gradle code there is a red comment where they explicitly state to add the line at the bottom of the file - MatPag
Yes... I'm going to need the help of the codenameone team for that. Maybe they can help me with a new build hint to achieve this. - Adrian Ionescu
Did you read this? github.com/codenameone/CodenameOne/wiki/Push-Notifications The new push implementation supports FCM. Play services is included by default and everything will work with FCM as expected if you use the current push instructions. You don't need to go into the native Android approach of doing it. - Shai Almog

1 Answers

0
votes

I have found a workaround to initialize the FirebaseApp without the google-services plugin.

I had to manually initialize it as described here: Can I initialize Firebase without using google-services.json?