Unity 的 GCM 插件,找不到方法 GCMRegistrar.checkDevice
Posted
技术标签:
【中文标题】Unity 的 GCM 插件,找不到方法 GCMRegistrar.checkDevice【英文标题】:GCM Plugin for Unity, Could not find method GCMRegistrar.checkDevice 【发布时间】:2013-05-14 17:01:26 【问题描述】:我以前看过有关此的帖子,但似乎没有一个解决方案对我有用。
我下载/安装了 Eclipse for Mobile(http://www.eclipse.org/downloads/packages/eclipse-mobile-developers/junosr2),并创建了一个 android 应用程序,然后使用从这个人的视频教程 (http://www.youtube.com/watch?v=TazEIIoNVA0) 中找到的说明将其设为库,以便我可以将其放入 Unity。
当然,他的教程是针对 Mobclix 的,我正在尝试构建一个 GCM 插件,但过程类似。我想我已经包含了我需要的所有依赖项。在 Eclipse 中,我在项目中看到“Android Dependencies”,其中包括:classes.jar(包含 Unity 播放器挂钩)、gcm.jar 和 android-support-v4.jar。在“Android 4.2.2”下,我看到“android.jar”。在“引用库”下,我看到了 classes.jar 和 gcm.jar。在关于此错误的其他论坛帖子(例如:GCM : java.lang.noclassdeffounderror: com.google.android.gcm.GCMRegistrar)之后,我手动将 gcm.jar 和 classes.jar 复制到“libs”文件夹(我使用的是 ADT 17),然后选择添加到构建路径。
在这一切之后,当我部署到设备上进行测试时,我仍然看到 2 个错误,第一个是标题错误,然后是 NoClassDefFoundError。我已经通过 3 台设备进行兼容性测试,我目前使用的是带有 2.3.3 的摩托罗拉(所以它应该支持 GCM)。如果有人知道还有什么可能导致这种情况,请告诉我。我对 Eclipse(或者甚至 Java)不是很熟悉,所以我打赌它以某种方式存在于 Java 项目中。
我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.my.package.testapp" android:versionName="1.0" android:versionCode="1">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
/>
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="my_test_scheme" android:host="*" />
</intent-filter>
</activity>
<activity
android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:screenOrientation="portrait"
/>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity
android:name="com.unity3d.player.VideoPlayer"
android:label="@string/app_name"
android:screenOrientation="behind"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
/>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.my.package.testapp" />
</intent-filter>
</receiver>
<service android:name="com.my_provider.gcm.GCMIntentService" />
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="com.my.package.testapp.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.my.package.testapp.permission.C2D_MESSAGE"
android:protectionLevel="signature"
/>
</manifest>
我的 Logcat:
I/Unity (16312): MyApplication::MyApplication()
I/Unity (16312):
I/Unity (16312):
I/Unity (16312): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
I/Unity (16312):
D/dalvikvm(16344): GC_CONCURRENT freed 1309K, 55% free 3034K/6727K, external 1625K/2137K, paused 2ms+3ms
D/dalvikvm(16344): GC_CONCURRENT freed 539K, 54% free 3137K/6727K, external 1625K/2137K, paused 2ms+2ms
D/DeviceProfile(16344): ************************** Properties ****************************
D/DeviceProfile(16344): * android.vm.dexfile: true
D/DeviceProfile(16344): * file.encoding: UTF-8
D/DeviceProfile(16344): * file.separator: /
D/DeviceProfile(16344): * http.agent: Dalvik/1.4.0 (Linux; U; Android 2.3.4; LG-P999 Build/GRJ22)
D/DeviceProfile(16344): * java.boot.class.path: /system/framework/core.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar
D/DeviceProfile(16344): * java.class.path: .
D/DeviceProfile(16344): * java.class.version: 46.0
D/DeviceProfile(16344): * java.compiler:
D/DeviceProfile(16344): * java.ext.dirs:
D/DeviceProfile(16344): * java.home: /system
D/DeviceProfile(16344): * java.io.tmpdir: /sdcard
D/DeviceProfile(16344): * java.library.path: /vendor/lib:/system/lib
D/DeviceProfile(16344): * java.net.preferIPv6Addresses: true
D/DeviceProfile(16344): * java.runtime.name: Android Runtime
D/DeviceProfile(16344): * java.runtime.version: 0.9
D/DeviceProfile(16344): * java.specification.name: Dalvik Core Library
D/DeviceProfile(16344): * java.specification.vendor: The Android Project
D/DeviceProfile(16344): * java.specification.version: 0.9
D/DeviceProfile(16344): * java.vendor: The Android Project
D/DeviceProfile(16344): * java.vendor.url: http://www.android.com/
D/DeviceProfile(16344): * java.version: 0
D/DeviceProfile(16344): * java.vm.name: Dalvik
D/DeviceProfile(16344): * java.vm.specification.name: Dalvik Virtual Machine Specification
D/DeviceProfile(16344): * java.vm.specification.vendor: The Android Project
D/DeviceProfile(16344): * java.vm.specification.version: 0.9
D/DeviceProfile(16344): * java.vm.vendor: The Android Project
D/DeviceProfile(16344): * java.vm.vendor.url: http://www.android.com/
D/DeviceProfile(16344): * java.vm.version: 1.4.0
D/DeviceProfile(16344): * javax.net.ssl.trustStore: /system/etc/security/cacerts.bks
D/DeviceProfile(16344): * line.separator:
D/DeviceProfile(16344): * mngspkphone: true
D/DeviceProfile(16344): * os.arch: armv7l
D/DeviceProfile(16344): * os.name: Linux
D/DeviceProfile(16344): * os.version: 2.6.32.9
D/DeviceProfile(16344): * path.separator: :
D/DeviceProfile(16344): * recording.auto-focus: true
D/DeviceProfile(16344): * recording.ffc.camera-sensor: 1
D/DeviceProfile(16344): * recording.ffc.video: 320x240 15 low
D/DeviceProfile(16344): * recording.hd.video: 1280x720 24 hd
D/DeviceProfile(16344): * recording.live.audio-aux: EhAAAA==
D/DeviceProfile(16344): * recording.live.video: 176x144 15 low
D/DeviceProfile(16344): * recording.live.video-aux: AUKAHv/hAAhnQoAelaCxMQEABGjOPIA=
D/DeviceProfile(16344): * recording.live.video-aux-rotation-90: AUKAHv/hAAhnQoAelaCRcQEABGjOPIA=
D/DeviceProfile(16344): * recording.liveffc.audio-aux: EhAAAA==
D/DeviceProfile(16344): * recording.liveffc.camera-sensor: 1
D/DeviceProfile(16344): * recording.liveffc.video: 176x144 15 low
D/DeviceProfile(16344): * recording.liveffc.video-aux: AUKAHv/hAAhnQoAelaCxMQEABGjOPIA=
D/DeviceProfile(16344): * recording.liveffc.video-aux-rotation-90: AUKAHv/hAAhnQoAelaCRcQEABGjOPIA=
D/DeviceProfile(16344): * recording.normal.video: 640x480 15 medium
D/DeviceProfile(16344): * user.dir: /
D/DeviceProfile(16344): * user.home:
D/DeviceProfile(16344): * user.language: en
D/DeviceProfile(16344): * user.name:
D/DeviceProfile(16344): * user.region: US
D/DeviceProfile(16344): ******************************************************************
I/ActivityManager( 1111): No longer want com.google.android.gsf.login (pid 15960): hidden #16
I/dalvikvm(16312): Could not find method com.google.android.gcm.GCMRegistrar.checkDevice, referenced from method com.my_provider.gcm.RegisterActivity.RegisterDevice
W/dalvikvm(16312): VFY: unable to resolve static method 382: Lcom/google/android/gcm/GCMRegistrar;.checkDevice (Landroid/content/Context;)V
D/dalvikvm(16312): VFY: replacing opcode 0x71 at 0x0000
D/dalvikvm(16312): VFY: dead code 0x0003-001c in Lcom/my_provider/gcm/RegisterActivity;.RegisterDevice (Landroid/app/Activity;)V
W/Unity (16312): Font size and style overrides are only supported for dynamic fonts.
W/Unity (16312):
W/Unity (16312): (Filename: ./Runtime/GUI/TextMeshGenerator2.cpp Line: 65)
W/Unity (16312):
W/Unity (16312): Font size and style overrides are only supported for dynamic fonts.
W/Unity (16312):
W/Unity (16312): (Filename: ./Runtime/GUI/TextMeshGenerator2.cpp Line: 65)
W/Unity (16312):
W/dalvikvm(16312): threadid=9: thread exiting with uncaught exception (group=0x4001d560)
E/AndroidRuntime(16312): FATAL EXCEPTION: GLThread 10
E/AndroidRuntime(16312): java.lang.NoClassDefFoundError: com.google.android.gcm.GCMRegistrar
E/AndroidRuntime(16312): at com.my_provider.gcm.RegisterActivity.RegisterDevice(RegisterActivity.java:23)
E/AndroidRuntime(16312): at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
E/AndroidRuntime(16312): at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
E/AndroidRuntime(16312): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
E/AndroidRuntime(16312): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
W/ActivityManager( 1111): Force finishing activity com.my.package.testapp/com.unity3d.player.UnityPlayerNativeActivity
代码很简单,只有两个文件
RegisterActivity.java:
package com.my_provider.gcm;
import android.os.Bundle;
import android.app.Activity;
import com.google.android.gcm.GCMRegistrar;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class RegisterActivity extends UnityPlayerActivity
public static String ProjectID;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
public static void RegisterDevice(final Activity activity)
try
GCMRegistrar.checkDevice(activity);
GCMRegistrar.checkManifest(activity);
final String regId = GCMRegistrar.getRegistrationId(activity);
if (regId.equals(""))
GCMRegistrar.register(activity, ProjectID);
catch(Exception e)
UnityPlayer.UnitySendMessage("ErrorHandlerGameObject", "OnError", e.getMessage());
最后是服务 GCMBroadcastReceiver.java:
package com.my_provider.gcm;
import android.content.Context;
import android.content.Intent;
import com.google.android.gcm.GCMBaseIntentService;
import com.unity3d.player.UnityPlayer;
public class GCMBroadcastReceiver extends GCMBaseIntentService
public GCMBroadcastReceiver()
super(RegisterActivity.ProjectID);
@Override
protected void onRegistered(Context context, String deviceID)
UnityPlayer.UnitySendMessage("DeviceRegisteringGameObject", "OnRegisteredAndroidDevice", deviceID);
@Override
protected void onUnregistered(Context context, String deviceID)
UnityPlayer.UnitySendMessage("DeviceRegisteringGameObject", "OnUnregisteredAndroidDevice", deviceID);
@Override
public void onError(Context context, String errorID)
UnityPlayer.UnitySendMessage("DeviceRegisteringGameObject", "OnError", errorID);
@Override
protected void onMessage(Context context, Intent intent)
UnityPlayer.UnitySendMessage("DeviceRegisteringGameObject", "OnMessageAndroid", intent.getStringExtra("message"));
【问题讨论】:
【参考方案1】:我已手动将 gcm.jar 和 classes.jar 复制到“libs”文件夹(我使用的是 ADT 17),然后选择添加到构建路径。
我不知道classes.jar
是什么。
对于普通的 Android 应用来说,在 libs/
文件夹中只包含 gcm.jar
就足够了——使用“添加到构建路径”手动更改构建路径是错误的。
Unity插件的流程是否不同,我不能说。
错误表明您在编译时构建路径中有gcm.jar
(例如,通过添加到构建路径)但它不在APK中(例如,您在libs/
中没有gcm.jar
,或手动将其添加到您的构建路径以防止它被添加到您的 APK)。
【讨论】:
Classes.jar 很难找到,它包含 Unity 特定的钩子,更多信息在这里:answers.unity3d.com/questions/182912/…。我将尝试在不将 gcm.jar 添加到我的构建路径的情况下进行构建,看看是否有任何作用 看起来像 Unity,您需要确保它们不在构建路径中,而是放在您制作的插件 jar 旁边,所以是的,这似乎解决了问题。谢谢以上是关于Unity 的 GCM 插件,找不到方法 GCMRegistrar.checkDevice的主要内容,如果未能解决你的问题,请参考以下文章
AirConsole Unity 插件给出编译器错误 - 找不到“DataContractAttribute”
Phonegap Build:找不到任何与 com.google.android.gms:play-services-gcm:12+ 匹配的版本