Thursday, March 18, 2010

Android Error: Conversion to Dalvik format failed with error 1

I wanted to document something that I learnt about Android development, particularly, this error:

Conversion to Dalvik format failed with error 1

It can happen typically when someone starts their Android project in Eclipse. The project can be as simple as a "Hello World" project, this error may still come up. Apparently Eclipse does some internal caching for ADT (version doesn't seem to matter, I have seen it from 0.8.0 version to 0.9.6 version) and when we update Android Tools in Eclipse, it gets corrupted. Here is what I tried and worked for me. My installation is Eclipse 3.5.1 and ADT 0.9.6.

Option One:
In Eclipse, delete your project (back up the source before, of course). Restart Eclipse, recreate the project and the error will go away. This worked for some, but it didn't for me.

Option Two:

Next is to delete your project, start Eclipse on command line with clean option:

eclipse -clean

This cleans up any cached data that Eclipse stores. Now recreate your project and hopefully the error is gone. Well, sometimes it doesn't work. On to the next option.

Option Three:
Delete your project. Uninstall all Android plug-ins in Eclipse and restart it with clean option. Now install your required Android plug-ins.
    Create your project, hopefully the error is gone, if not continue reading. In your project home, eclipse will create a .classpath file. Open it in an editor, and ensure that each of the following lines exist in it.

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
        <classpathentry kind="src" path="gen"/>
        <classpathentry kind="output" path="bin"/>
    </classpath>


    Note that there will be other lines relevant to your project like


    <classpathentry combineaccessrules="false" kind="src" path="/project-name"/>

    that is okay, the key thing is this line is:

    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>


    It needs to be in there. Make this change and save the file, and restart Eclipse. In main menu, do Project -> Clean. Now in project explorer, on the new project you just created, Right Click -> Properties -> Android -> Check Android 1.5 -> Click Apply Button.

    Next project, Right Click -> Properties. In the menu on left, click on Java Build Path, on right panel, click on Libraries tab. Ensure that you see a folder called "Android 1.5". Now hit OK and your project should compile.

    In the Library tab, if you see Android.jar then it must be deleted manually (highlight and click "Remove" button) and then this error will be gone.

    Apart from this I have heard one other option to try to remove this error. I didn't try it but it is supposed to work. First delete Android.jar from right click on Project -> Properties ->Java Build Path -> Libraries tab. Then Right Click the Project -> Android Tools -> Fix Project Properties. This will add Android 1.5 folder and this error will be gone.

    I haven't tested this with other versions of Andriod SDKs but I don't see why they wont work.

    No comments: