简体   繁体   English

使用Google Cloud Messaging的App Engine后端有哪些必要的依赖关系?

[英]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? 使用Google Cloud Messaging的App Engine后端有哪些必要的依赖关系?

Currently, when you add a module like that to your Android Studio project it adds this dependency: 目前,当您向Android Studio项目添加类似模块时,它会添加此依赖项:

'compile 'com.google.android.gms:play-services:8.4.0' 'compile'c​​om.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'. 错误:任务':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 com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:处理'命令'/Library/Java/JavaVirtualMachines/jdk1.7.0_79。 jdk / Contents / Home / bin / java''以非零退出值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. 所以我似乎只需要为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. E / GMPM:GoogleService初始化失败,状态:10,缺少预期资源:'R.string.google_app_id'用于初始化Google服务。 Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。

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. 奇怪的是,我的旧GAE + GCM项目可以很好地导入整个Google Play服务。 Importing those older versions of google play services in my new project does not work however. 但是,在我的新项目中导入旧版本的Google Play服务并不起作用。 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'. 1)开始新的Android Studio项目,添加了新的谷歌云模块“App Engine Java端点模块”。 The auto-generated build.grade (Module: app) looks like this: 自动生成的build.grade(Module:app)如下所示:

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'. 2)开始新的Android Studio项目,添加了新的谷歌云模块'App Engine后端与谷歌云消息'。 the auto-generated build.grade (Module: app) looks like this: 自动生成的build.grade(Module:app)如下所示:

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. 所以它看起来像'compile'c​​om.google.android.gms:play-services:8.4.0''是问题所在。 I replaced it with 我换了它

'compile 'com.google.android.gms:play-services-gcm:8.4.0'' 'compile'c​​om.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. 12-30 14:14:16.482 10573-10573 / com.myapp.myapp E / GMPM:GoogleService初始化失败,状态:10,缺少预期资源:'R.string.google_app_id'用于初始化Google服务。 Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。 12-30 14:14:16.482 10573-10573/com.myapp.myapp E/GMPM: Scheduler not set. 12-30 14:14:16.482 10573-10573 / com.myapp.myapp E / GMPM:调度程序未设置。 Not logging error/warn. 不记录错误/警告。 12-30 14:14:16.502 10573-10623/com.myapp.myapp E/GMPM: Uploading is not possible. 12-30 14:14:16.502 10573-10623 / com.myapp.myapp E / GMPM:无法上传。 App measurement disabled 应用测量禁用

So it looks like I am missing this google-services.json file or something. 所以看起来我错过了这个google-services.json文件或其他东西。 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. 我不知道Android Studio发生了什么,因为几个月前我以同样的方式创建了一个支持GCM的应用程序,并且编译没有问题。 The gradle.build file of that app looks like this: 该应用程序的gradle.build文件如下所示:

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. 所以看起来Android Studio停止添加'compile'c​​om.ganyo:gcm-server:1.0.2''依赖。

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. 因此,这应该被视为Android Studio的一个问题,因为只需添加一个模块“App Engine后端与Google云消息传递”即使最简单的应用也会每次都破坏构建,因为完整的Google Play服务依赖项'com.google .android.gms:play-services:8.4.0'足够大,可以自己超过65K DEX方法限制。 This problem is actually documented in ' Setting Up Google Play Services '. 实际上,“ 设置Google Play服务 ”中记录了此问题。

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'. 正如您所发现的那样,解决方案是手动编辑build.gradle并仅为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. 手动添加“google-services.json”的要求是正常的,因为您需要在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. 添加'multiDexEnabled true'不是一个好的解决方案,因为它会不必要地增加你的APK大小,你仍然会有重复的依赖关系。

I've created an entry on the Android issue tracker to fix the dependencies for the App Engine GCM backend module in Android Studio. 在Android问题跟踪器上创建了一个条目来修复Android Studio中App Engine GCM后端模块的依赖关系。 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 我在这里看了一下google GCM示例项目: https//github.com/googlesamples/google-services/tree/master/android/gcm

And edited my gradle files. 并编辑了我的gradle文件。 Here is what they look like now: 这是他们现在的样子:

APP: 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. 我也按照指南(参见步骤2)生成了google-services.json文件,我将其放入app /目录中。

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. 我真的很生气,这与使用自动生成的gradle文件开箱即用,我必须在一些示例应用程序中搜索它们。 It didn't used to be this way just a few months ago. 几个月前它就不是这种方式了。 It worked as soon as you added the GCM module. 一旦添加了GCM模块,它就会起作用。

If anyone else has any tips/suggestions for future projects please let me know. 如果其他人对未来项目有任何提示/建议,请告诉我。 =D = d

If you do get to the stage where you need to run with MultiDex enabled, there are three steps you need to do: 如果您确实进入需要在启用MultiDex的情况下运行的阶段,则需要执行以下三个步骤:

  1. Enable it in your defaultConfig in Gradle as you already tried: 您已尝试在Gradle中的defaultConfig中启用它:

     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 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. 但是总是要好好看看你已经拥有的依赖关系,因为它通常是不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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