Saturday, August 27, 2011

fix "bad class file error" android build error

I got the error below while building android source code:
packages/apps/Calculator/src/com/android/calculator2/CalculatorDisplay.java:19: cannot access android.content.Context
bad class file: android/content/Context.class(android/content:Context.class)

The error is caused by using an incorrect version of jdk. Google recommend using sun-java6-jdkto build android source code. On my pc, besides sun jdk that installed, openjdk and fastjar are installed by default. And they're used to build android, rather than sun-jdk. To make sure that sun jdk is used, I used commands below to select sun jdk.

sudo update-alternatives --config java
sudo update-alternatives --config jar

Typical output is shown below, and android build successfully after I select sun jdk.
There are 2 choices for the alternative jar (providing /usr/bin/jar).


  Selection    Path                             Priority   Status
------------------------------------------------------------
  0            /usr/bin/fastjar                  100       auto mode
  1            /usr/bin/fastjar                  100       manual mode
* 2            /usr/lib/jvm/java-6-sun/bin/jar   63        manual mode

3 comments:

Anonymous said...

I tried this, my default was "/usr/bin/fastjar". I selected "/usr/lib/jvm/java-6-sun/bin/jar" and still the build is failing. Plz suggest.
Thanks
ARVR.

rxwen said...

What error did you get after configuration?

Mike said...

I got the same error. Changing "jar" to sun java 6 doesn't make any difference.
Mike