2

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
Adrian Ionescu
  • 281
  • 1
  • 8
  • 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 Sep 11 '18 at 14:34
  • 1
    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 Sep 11 '18 at 15:10
  • 1
    You need to find a solution for this to work, read here in the documentation: https://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 Sep 11 '18 at 16:25
  • 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 Sep 11 '18 at 17:51
  • Did you read this? https://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 Sep 12 '18 at 03:24
  • Hi, Shai, actually I did read that, but I only have a basic account, and I can't afford a pro account at this time, which is why I opted for a native implementation. However, I tried having my main app class implement the PushCallback interface and that solved the dependencies issues, but caused other issues, described here : https://stackoverflow.com/questions/52295256/codenameone-android-push-notifications-support-without-a-pro-account, so right now I'm stuck. Please help. – Adrian Ionescu Sep 12 '18 at 12:33
  • @Shai, although I have found a workaround for this issue, I think thre's still a need for a build hint, or some other solution, that can allow applying the google-services plugin at the bottom of the build.gradle file, for those that, for various reasons, want to implement things natively. Thanks. – Adrian Ionescu Sep 13 '18 at 06:40
  • There are build hints that apply specific Google services selectively. – Shai Almog Sep 14 '18 at 03:01
  • No, I'm talking about applying a plugin, with a line like this: apply plugin: 'com.google.gms.google-services', at the bottom of the gradle file, not at the begining. – Adrian Ionescu Sep 14 '18 at 08:29

1 Answers1

0

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?

Adrian Ionescu
  • 281
  • 1
  • 8