5

I try to include in my application compilation a .jar compiled.

I use ant to compile my Android application.

When I add this task to build.xml :

 <javac srcdir="${src}"
     classpath="xyz.jar" />

compilation failed on android package importation :

[javac] Compiling 1 source file
[javac] C:\HelloWorld\src\com\example\hellowolrd\HelloWorld.java:3: package android.app does not exist
[javac] import android.app.Activity;
[javac]                   ^

Whereas when my task was not in build.xml, compilation succeeded.

TheFrancisOne
  • 2,667
  • 9
  • 38
  • 58

4 Answers4

11

If you're using the standard ant script from android create project then you should be able to dump any jars in a libs directory and have them included automatically (documentation).

Dominic Mitchell
  • 11,861
  • 4
  • 29
  • 30
  • 1
    Yes, I create project from command line `android create project` command line. I tried to include .jar in `libs/` folder, but import error still occurs – TheFrancisOne Feb 25 '11 at 09:01
  • 2
    Did you get it to work? You checked this answer as accepted, but your comment says your error still occurs. – Erik B Aug 16 '11 at 23:08
  • @Erik B , thanks for that. To post owner, would you update for the corrected script? – eros Aug 29 '11 at 02:54
  • I also encounter this kind of problem, and move the .jar file to libs works for me! – wenchiching Jun 30 '14 at 03:15
3

I just added lib to javac and it did the trick:

ant -lib lib debug
Nifle
  • 11,745
  • 10
  • 75
  • 100
Cougar
  • 61
  • 1
1

If you have a build.xml you have already edited (altered variables, custom imports, etc) and are a little nervous about running generic commands on it, you can also add:

<javac srcdir="libs"
     classpath="xyz.jar" />

to the build.xml file and it will work. It is the manual equivalent of the command

ant -lib lib debug
Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
-1

If you're not familiar with how Ant works, it's easier to use Eclipse for your Android projects. Then you can add jars using a GUI. http://developer.android.com/guide/appendix/faq/commontasks.html#addexternallibrary

Also, based on your other question, this appears to be what you are attempting to do: http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject

Richard
  • 899
  • 1
  • 8
  • 12
  • I'm expected to use And (and not Eclipse ADT plugin) to build my android application from command line. Thanks fos your link, but it's about library project. Here my project is about application and I woudl like to include an external .jar – TheFrancisOne Feb 25 '11 at 08:58