简体   繁体   中英

What are the necessary gradle dependencies for an App Engine Backend with Google Cloud Messaging?

What are the necessary gradle dependencies for an App Engine Backend with Google Cloud Messaging?

Currently, when you add a module like that to your Android Studio project it adds this dependency:

'compile 'com.google.android.gms:play-services:8.4.0'

However, when you run the project you get this error:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

Someone suggested using this:

defaultConfig {
        multiDexEnabled true
}

But that actually didn't work for me.

So it appears that I have to specify only the required libraries for GAE + GCM. So far I have:

compile 'com.google.android.gms:play-services-auth:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.google.android.gms:play-services-base:8.4.0'

full list here . But that didn't work. I got this error:

E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.

So I am at a loss.

Is there some other way around this issue? What is weird is my old GAE + GCM projects work fine importing the whole google play services. Importing those older versions of google play services in my new project does not work however. So I am not sure what is going on.

EDIT: Some more information:

I did some tests.

1) Started new Android Studio project, added new google cloud module 'App Engine Java Endpoints Module'. The auto-generated build.grade (Module: app) looks like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile project(path: ':backend', configuration: 'android-endpoints')
}

Result? Compiles and runs perfectly - no problems!

2) Started new Android Studio project, added new google cloud module 'App Engine Backend with Google Cloud Messaging'. the auto-generated build.grade (Module: app) looks like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile project(path: ':backend', configuration: 'android-endpoints')
}

Result? Same crappy error I have been getting!

So it looks like the line 'compile 'com.google.android.gms:play-services:8.4.0'' is the problem. I replaced it with

'compile 'com.google.android.gms:play-services-gcm:8.4.0''

since in theory that is all I need for google cloud messaging. When I run it I get this:

12-30 14:14:16.482 10573-10573/com.myapp.myapp E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 12-30 14:14:16.482 10573-10573/com.myapp.myapp E/GMPM: Scheduler not set. Not logging error/warn. 12-30 14:14:16.502 10573-10623/com.myapp.myapp E/GMPM: Uploading is not possible. App measurement disabled

So it looks like I am missing this google-services.json file or something. I don't get what happened with Android Studio because several months ago I made a GCM enabled app the same way and that one compiles no problem. The gradle.build file of that app looks like this:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(path: ':gcm-backend', configuration: 'android-endpoints')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.ganyo:gcm-server:1.0.2'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:support-v4:22.2.0'
}

So it looks like Android Studio stopped adding the 'compile 'com.ganyo:gcm-server:1.0.2'' dependency.

So I ran a project with

'compile 'com.ganyo:gcm-server:1.0.2'
'compile 'com.google.android.gms:play-services:8.4.0'

Result? Same Execution failed error.

Ok so let's try the old play-services library in my new project:

'compile 'com.ganyo:gcm-server:1.0.2'
'compile 'com.google.android.gms:play-services:7.5.0'

Result? Same Execution failed error.

I just don't get why this isn't working out of the box like it used to...

So, this should be considered an issue with Android Studio since simply adding a module 'App Engine Backend with Google Cloud Messaging' will break the build every time on even the simplest of apps, due to the full Google Play Services dependency 'com.google.android.gms:play-services:8.4.0' being large enough to exceed the 65K DEX method limit on its own. This problem is actually documented in ' Setting Up Google Play Services '.

The solution is, as you had discovered, to manually edit your build.gradle and add only the import for GCM 'com.google.android.gms:play-services-gcm:8.4.0'. The requirement for 'google-services.json' to be added manually is normal though, since you need to generate it for your project on developers.google.com. Adding 'multiDexEnabled true' isn't a good solution, as it increases your APK size needlessly, and you still end up with duplicate dependencies.

I've created an entry on the Android issue tracker to fix the dependencies for the App Engine GCM backend module in Android Studio. Feel free to star this issue for visibility and updates.

Preliminary tests show that this works so far.

I took at a look at the google GCM sample project here: https://github.com/googlesamples/google-services/tree/master/android/gcm

And edited my gradle files. Here is what they look like now:

APP:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.me.myapp"
        minSdkVersion 13
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':backend', configuration: 'android-endpoints')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

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

PROJECT:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.google.gms:google-services:2.0.0-alpha3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And also I followed this guide (see step 2) to generate a google-services.json file which I dropped into the app/ directory.

Now the app compiles and works (so far at least).

I'm really angry this doesn't work out of the box with the auto generated gradle files and that I have to hunt them down in some sample app. It didn't used to be this way just a few months ago. It worked as soon as you added the GCM module.

If anyone else has any tips/suggestions for future projects please let me know. =D

If you do get to the stage where you need to run with MultiDex enabled, there are three steps you need to do:

  1. Enable it in your defaultConfig in Gradle as you already tried:

     defaultConfig { ... minSdkVersion 14 targetSdkVersion 21 ... // Enabling multidex support. multiDexEnabled true } 
  2. Include it in your dependencies:

     dependencies { compile 'com.android.support:multidex:1.0.0' } 
  3. Add it to your manifest:

     <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.multidex.myapplication"> <application ... android:name="android.support.multidex.MultiDexApplication"> ... </application> </manifest> 

https://developer.android.com/intl/es/tools/building/multidex.html#about

But always have a good look at the dependencies you already have as it's usually not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM