![](/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.