Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException using AndroidAnnotations

I trying to use AndroidAnnotaions, but getting ClassNotFoundException error in my project.

For testing purposes, I created a new project with one Activity and I still getting the error. My files:

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rodrigora.testannotations.MainActivity_"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.rodrigora.testannotations;

import android.app.Activity;
import android.view.Menu;

import com.googlecode.androidannotations.annotations.EActivity;

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

These are the two files I changed from sample project created by Eclipse and followed these steps to add AndroidAnnotations to project:

  1. Added androidannotations-api-2.7.jar to libs folder and to build path
  2. Added androidannotations-2.7.jar to compiled-libs folder and to Factory Path
  3. Marked androidannotations-2.7.jar in Order and Export tab.

Here the stack trace when I launch the app:

12-28 20:29:21.296: E/AndroidRuntime(30600): FATAL EXCEPTION: main
12-28 20:29:21.296: E/AndroidRuntime(30600): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rodrigora.testannotations/com.rodrigora.testannotations.MainActivity_}: java.lang.ClassNotFoundException: com.rodrigora.testannotations.MainActivity_ in loader dalvik.system.PathClassLoader[/data/app/com.rodrigora.testannotations-2.apk]
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.os.Looper.loop(Looper.java:130)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at java.lang.reflect.Method.invokeNative(Native Method)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at java.lang.reflect.Method.invoke(Method.java:507)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at dalvik.system.NativeStart.main(Native Method)
12-28 20:29:21.296: E/AndroidRuntime(30600): Caused by: java.lang.ClassNotFoundException: com.rodrigora.testannotations.MainActivity_ in loader dalvik.system.PathClassLoader[/data/app/com.rodrigora.testannotations-2.apk]
12-28 20:29:21.296: E/AndroidRuntime(30600):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 20:29:21.296: E/AndroidRuntime(30600):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
12-28 20:29:21.296: E/AndroidRuntime(30600):    ... 11 more

Anyone knows what I forgot? Eclipse should show generated classes by AndroidAnnotations?

Thank you!

EDIT

Full code - bitbucket

like image 686
Rodrigo Avatar asked Dec 16 '22 15:12

Rodrigo


2 Answers

It looks like your AndroindAnnotation generated classes are not getting included in your build. Here are the lines your .classpath seems to be missing --

   <classpathentry kind="src" path=".apt_generated">
               <attributes>
                       <attribute name="optional" value="true"/>
               </attributes>
   </classpathentry>

These should have been generated for you as part of following the AndroidAnnotations install. Things to check --

  • Your ADT tools are up to date.

  • You ran Project->Clean after enabling annotations

I was able to clone your repo, import into eclipse, and get a clean build and run the app without a crash using the following steps --

  1. Refactor/rename project to TestAnnotations (eclipse imported as "MainActivity_")
  2. Enable annotation processing (needed to turn on Enable project specific settings and Enable annonation processing in `Java Compiler->Annontation Processing)
  3. Project->Clean
  4. Run it

Note: If I did an import and just refactored to get a clean build, but did not enable annotation processing, I got the exact same behavior you describe. Check to make sure you have project specific settings on, annotations on, and then do a clean build.

like image 200
iagreen Avatar answered Jan 03 '23 05:01

iagreen


With maven and eclipse updating project configuration causes android annotation output folder get changed. To fix it go to java build path>Source and set the android annotation folder's output to default output folder.

like image 28
Hadi Tok Avatar answered Jan 03 '23 04:01

Hadi Tok