我有一个分为两个项目:基本代码模块和应用程序特定的代码模块。

我正在尝试使用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类。

所以这是我的应用程序清单:

<?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类,当我在getDefaultIntentServiceClassName类的GCMBroadcastReceiver方法中放置一个断点时,该类的路径字符串似乎正确。我仍然收到此错误。

我已经在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')
    }
}


我的GCMIntentService软件包在src/main/java内部。
我删除了文件夹main/java,并将包放在src中。

然后,如上所述,更改了Line 1Line 2,如下所示:

java.srcDirs = ['src'] // Line 1
resources.srcDirs = ['src'] // Line 2


它开始工作。

10-08 18:22