Java – Unable to instantiate service: ClassNotFoundException

Unable to instantiate service: ClassNotFoundException… here is a solution to the problem.

Unable to instantiate service: ClassNotFoundException

I’ve read about a hundred questions of the same category, but none of them help. I have a working application, then refactor the package name, and then compile and run. I came back a week later to work on it and got ClassNotFoundException on startup. To make matters worse, if I check the previous commit from repo (before any refactoring, everything works fine), the project throws the same error on startup (except for a different package name).

I’ve tried everything I can think of, but I don’t know how to find the problem. I did the obvious: clean up the project, completely remove and re-add, run on a new device….

I’m not sure which code helps with debugging, but here is the declaration and logcat output in my list. Please let me know if something else might be useful. I was really lost.

Crash:

E/AndroidRuntime(  515): FATAL EXCEPTION: main
E/AndroidRuntime(  515): java.lang.RuntimeException: Unable to instantiate service
co.mosic.mosic.CommunicationService: java.lang.ClassNotFoundException: co.mosic.mosic.CommunicationService
E/AndroidRuntime(  515):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2237)
E/AndroidRuntime(  515):    at android.app.ActivityThread.access$1600(ActivityThread.java:123)
E/AndroidRuntime(  515):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
E/AndroidRuntime(  515):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  515):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  515):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(  515):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  515):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  515):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(  515):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(  515):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  515): Caused by: java.lang.ClassNotFoundException: co.mosic.mosic.CommunicationService
E/AndroidRuntime(  515):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(  515):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(  515):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(  515):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2234)
E/AndroidRuntime(  515):    ... 10 more

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="co.mosic.mosic"
    android:versionCode="1"
    android:versionName="1.0" >

...

<service 
    android:name=". CommunicationService" >
    <intent-filter>
        <action android:name="co.mosic.mosic.MStcServiceInet" /> 
    </intent-filter>
</service>

Solution

Due to the recent update of the ADT plugin, the library folder must be changed to “libs” instead of lib. I’ve come across it a few times recently.

Related Problems and Solutions