Search code examples
javaandroidandroid-serviceclassnotfoundexception

ClassNotFoundException when trying to start a Service


I'm trying to start a Android service:

startService(new Intent(this, MetawatchServiceStatic.class));

And application crashes with ClassNotFoundException. But it's working perfectly on second try when application started again. And everything is fine until system reboot. After reboot I need to try start service twice, again.

I tried to start the service in another process using definition in manifest:

<activity
    android:name=".MetawatchActivity"
    android:label="@string/app_name"
    android:process="com.Clusterrr.metawatch.gui" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<service android:name="com.Clusterrr.metawatch.MetawatchServiceStatic" >

And starting service on button click:

public void onClick(View v)
{
    switch (v.getId())
    {
    case R.id.buttonStart:
        try
        {
            startService(new Intent(this, MetawatchServiceStatic.class));
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        break;
    }
}

I can't catch this Exception because it's occurred in service process and it crashes immediately. But service starting perfectly on second click. On third and other tries everything if fine too. Until reboot or APK reinstall. But sometimes it's working on first try without any problems. I can't understand why.

Android version: 4.4.2

Logcat:

1995-1995/com.Clusterrr.metawatch.services E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.Clusterrr.metawatch.services, PID: 1995
    java.lang.RuntimeException: Unable to instantiate service com.Clusterrr.metawatch.MetawatchServiceStatic: java.lang.ClassNotFoundException: Didn't find class "com.Clusterrr.metawatch.MetawatchServiceStatic" on path: DexPathList[[zip file "/data/app/com.Clusterrr.metawatch-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.Clusterrr.metawatch-1, /vendor/lib, /system/lib]]
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2651)
            at android.app.ActivityThread.access$1800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.Clusterrr.metawatch.MetawatchServiceStatic" on path: DexPathList[[zip file "/data/app/com.Clusterrr.metawatch-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.Clusterrr.metawatch-1, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2648)
            at android.app.ActivityThread.access$1800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Solution

  • You have a ClassNotFoundException here because of the difference between the Service declared in the manifest file, which is ".MetawatchService", and the one you're actually trying to start, which is MetawatchServiceStatic.class.

    Make the two to be of the same name.