我的项目分为两部分:基本代码模块和应用程序特定代码模块。
我尝试使用 GCMIntentService,但在应用程序开始运行时出现此错误:
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint
E/AndroidRuntime: FATAL EXCEPTION: main
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: Process: br.com.brainweb.brainfeed.milkpoint, PID: 19281
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service br.com.brainweb.brainfeed.milkpoint.GCMIntentService: java.lang.ClassNotFoundException: Didn't find class "br.com.brainweb.brainfeed.milkpoint.GCMIntentService" on path: DexPathList[[zip file "/data/app/br.com.brainweb.brainfeed.milkpoint-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:2746)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.access$1800(ActivityThread.java:148)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5310)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "br.com.brainweb.brainfeed.milkpoint.GCMIntentService" on path: DexPathList[[zip file "/data/app/br.com.brainweb.brainfeed.milkpoint-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:2743)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.access$1800(ActivityThread.java:148)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5310)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: Suppressed: java.lang.ClassNotFoundException: br.com.brainweb.brainfeed.milkpoint.GCMIntentService
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.Class.classForName(Native Method)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: ... 11 more
03-08 11:23:47.454 19281-19281/br.com.brainweb.brainfeed.milkpoint E/AndroidRuntime: Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
对于错误的描述,找不到类。我读到,与应用程序在同一个包中必须有一个 GCMIntentService 类。
这是我的应用程序的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.brainweb.brainfeed.milkpoint"
android:versionCode="5"
android:versionName="2.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<permission
android:name="br.com.brainweb.brainfeed.milkpoint.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="br.com.brainweb.brainfeed.milkpoint.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name="br.com.brainweb.brainfeed.Aplicacao"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<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="br.com.brainweb.brainfeed.milkpoint" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<activity
android:name="br.com.brainweb.brainfeed.SplashScreen"
android:configChanges="orientation"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="br.com.brainweb.brainfeed.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name="br.com.brainweb.brainfeed.ItemViewActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" />
<activity
android:name="br.com.brainweb.brainfeed.InfoActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<service android:name="org.OpenUDID.OpenUDID_service" >
<intent-filter>
<action android:name="org.OpenUDID.GETUDID" />
</intent-filter>
</service>
</application>
</manifest>
包中实际上有一个 GCMIntentService 类,当我在 GCMBroadcastReceiver
类的 getDefaultIntentServiceClassName
方法中放置断点时,该类的路径字符串似乎是正确的。我仍然收到此错误。
我已经在 Stack Overflow 上尝试了很多解决方案,但似乎仍然没有一个能解决这个问题。
折腾了好久,终于找到了解决办法。
这就是我的 build.gradle
的一部分:
android {
compileSdkVersion 18
buildToolsVersion "21.1.2"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src\\main\\java','src\\test\\java'] // Line 1
resources.srcDirs = ['src\\main\\java','src\\test\\java'] // Line 2
aidl.srcDirs = ['src\\main\\java','src\\test\\java']
renderscript.srcDirs = ['src\\main\\java','src\\test\\java']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
我的GCMItentService
包位于src/main/java
内。我删除了文件夹 main/java
并将包放入 src
中。
然后我更改了 Line 1
和 Line 2
如上所示,如下所示:
java.srcDirs = ['src'] // Line 1
resources.srcDirs = ['src'] // Line 2
它开始工作了。
我是一名优秀的程序员,十分优秀!