![](/img/trans.png)
[英]Android push notification with Google Cloud Messaging and Google App Engine as backend
[英]What are the necessary gradle dependencies for an App Engine Backend with Google Cloud Messaging?
使用Google Cloud Messaging的App Engine后端有哪些必要的依赖关系?
目前,当您向Android Studio项目添加类似模块时,它会添加此依赖项:
'compile'com.google.android.gms:play-services:8.4.0'
但是,当您运行该项目时,您会收到此错误:
错误:任务':app:transformClassesWithDexForDebug'的执行失败。
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结束
有人建议用这个:
defaultConfig {
multiDexEnabled true
}
但这实际上对我不起作用。
所以我似乎只需要为GAE + GCM指定所需的库。 到目前为止,我有:
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'
完整列表在这里 。 但那没用。 我收到了这个错误:
E / GMPM:GoogleService初始化失败,状态:10,缺少预期资源:'R.string.google_app_id'用于初始化Google服务。 可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。
所以我不知所措。
还有其他方法可以解决这个问题吗? 奇怪的是,我的旧GAE + GCM项目可以很好地导入整个Google Play服务。 但是,在我的新项目中导入旧版本的Google Play服务并不起作用。 所以我不确定发生了什么。
编辑:更多信息:
我做了一些测试。
1)开始新的Android Studio项目,添加了新的谷歌云模块“App Engine Java端点模块”。 自动生成的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')
}
结果? 编译并运行完美 - 没问题!
2)开始新的Android Studio项目,添加了新的谷歌云模块'App Engine后端与谷歌云消息'。 自动生成的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')
}
结果? 我得到的同样糟糕的错误!
所以它看起来像'compile'com.google.android.gms:play-services:8.4.0''是问题所在。 我换了它
'compile'com.google.android.gms:play-services-gcm:8.4.0''
从理论上讲,这就是我所需要的谷歌云消息传递。 当我运行它时,我得到了这个:
12-30 14:14:16.482 10573-10573 / com.myapp.myapp E / GMPM:GoogleService初始化失败,状态:10,缺少预期资源:'R.string.google_app_id'用于初始化Google服务。 可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。 12-30 14:14:16.482 10573-10573 / com.myapp.myapp E / GMPM:调度程序未设置。 不记录错误/警告。 12-30 14:14:16.502 10573-10623 / com.myapp.myapp E / GMPM:无法上传。 应用测量禁用
所以看起来我错过了这个google-services.json文件或其他东西。 我不知道Android Studio发生了什么,因为几个月前我以同样的方式创建了一个支持GCM的应用程序,并且编译没有问题。 该应用程序的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'
}
所以看起来Android Studio停止添加'compile'com.ganyo:gcm-server:1.0.2''依赖。
所以我用了一个项目
'compile 'com.ganyo:gcm-server:1.0.2'
'compile 'com.google.android.gms:play-services:8.4.0'
结果? 相同执行失败错误。
好的,让我们在我的新项目中尝试旧的播放服务库:
'compile 'com.ganyo:gcm-server:1.0.2'
'compile 'com.google.android.gms:play-services:7.5.0'
结果? 相同执行失败错误。
我只是不明白为什么这不像过去那样开箱即用......
因此,这应该被视为Android Studio的一个问题,因为只需添加一个模块“App Engine后端与Google云消息传递”即使最简单的应用也会每次都破坏构建,因为完整的Google Play服务依赖项'com.google .android.gms:play-services:8.4.0'足够大,可以自己超过65K DEX方法限制。 实际上,“ 设置Google Play服务 ”中记录了此问题。
正如您所发现的那样,解决方案是手动编辑build.gradle并仅为GCM“com.google.android.gms:play-services-gcm:8.4.0”添加导入。 手动添加“google-services.json”的要求是正常的,因为您需要在developers.google.com上为您的项目生成它。 添加'multiDexEnabled true'不是一个好的解决方案,因为它会不必要地增加你的APK大小,你仍然会有重复的依赖关系。
我在Android问题跟踪器上创建了一个条目来修复Android Studio中App Engine GCM后端模块的依赖关系。 请随时为此问题提供可见性和更新。
初步测试表明,这项工作到目前为止。
我在这里看了一下google GCM示例项目: https : //github.com/googlesamples/google-services/tree/master/android/gcm
并编辑了我的gradle文件。 这是他们现在的样子:
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'
项目:
// 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
}
我也按照本指南(参见步骤2)生成了google-services.json文件,我将其放入app /目录中。
现在应用程序编译和工作(至少到目前为止)。
我真的很生气,这与使用自动生成的gradle文件开箱即用,我必须在一些示例应用程序中搜索它们。 几个月前它就不是这种方式了。 一旦添加了GCM模块,它就会起作用。
如果其他人对未来项目有任何提示/建议,请告诉我。 = d
如果您确实进入需要在启用MultiDex的情况下运行的阶段,则需要执行以下三个步骤:
您已尝试在Gradle中的defaultConfig中启用它:
defaultConfig { ... minSdkVersion 14 targetSdkVersion 21 ... // Enabling multidex support. multiDexEnabled true }
将它包含在您的依赖项中:
dependencies { compile 'com.android.support:multidex:1.0.0' }
将它添加到您的清单:
<?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
但是总是要好好看看你已经拥有的依赖关系,因为它通常是不需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.