Building Android library with Eclipse vs Ant - android

I have a simple Android library project, which contains network calls functionality only. It doesn't need to inherit any XML/resources etc… to calling application. When I build this project using Eclipse IDE (right click on project and click on Build Project), it generates a JAR under bin/libproject.jar. I can simply drop this file to any project's build path and it works fine.
Now, I want to implement continuous integration for my library. That means, I need some command-line way to achieve the same (building jar, when I build the project using ant). Ant builds the project differently. It creates classes.jar in bin/ folder, which is not the same as as libproject.jar.
I believe Eclipse's Build Project (ADT rev-21) is doing something magical to build this complete JAR for my library project (this is more like a java project like JAR).
Do you know how can I achieve the same using command line?

im using eclipse 4.3 w/ ant and with sdk tools 21.1
IMO - they recently changed the sdk regarding lib dependencies.
http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
see the above link.
my example lib dependency(transitive) as follows:
ABS <== SlidingMenu <== MyProject
so, in SM.project.properties...
android.library.reference.1=../../src/ABS/library
and, in MP.project.properties ...
android.library.reference.1=../../src/SlidingMenu/library
The ant build, run on proj=SM using either eclipse internal, ant build tools OR ant on CLI in a terminal session outside eclipse
does following:
[javac] Note: Recompile with -Xlint:deprecation for details.
[echo] Creating library output jar file...
[jar] Building jar: /home/rob/src/tools/ActionBarSherlock-4.2.0/library/bin/classes.jar
and ant build, run on proj=MP does the following:
Setting project property: out.library.jar.file -> /home/rob/src/SlidingMenu/library/bin/classes.jar
..
[jar] Building jar: /home/rob/src/SlidingMenu/library/bin/classes.jar
IMO - you should focus on 2 things :
get the lib reference correct in the file=project.properties of the dependent.
double check in eclipse the project /properties/ java build path / order and export
read the link as it contains multiple , specific NOTEs relating to 'order export'...
When you build the dependent project in ant , it should trigger internally, builds of the other projects. I think that is controlled in the build path of the dependent project.
An additional NOTE on debugging - i could not debug directly from the project explorer because the launcher did not like the "classes.jar" for the 2 , dependent projects. Debug insisted on there being jars with names "${project-name}.jar" in the ./bin directory of each library. But ant assigns "classes.jar" as shown above.
So, to debug, i used the alternate method of DDMS tab in eclipse w/ the app already running. then u find the process and attach the debugger.

Related

Android test project fails to build under Ant, OK in Eclipse

I am trying to set up a CI build server for an Android app project and its JUnit test project. I've generated the appropriate build.xml files for both app and test projects.
Both projects build and execute fine under Eclipse.
However, when I try to do ant debug on the test project without having first built both projects with Eclipse, I get several [javac] errors saying error: cannot find symbol which refer to classes in the app project.
I think this is a difference between the classpath that Eclipse sees and the classpath that Ant is using...
How do I tell Ant where to find the app classes?
It seems the Android default build.xml defines all the targets, so I was wondering how to insert the appropriate reference in the app project's build.xml... rather than having to start the build file from scratch.
Tried exporting the Ant build file from Eclipse; that fails to build either project.
What seems to work is to re-run:
android update test-project -m ../../app_project/ -p .
within the test project.
The only change it made was to create an ant.properties file with one non-comment line:
tested.project.dir=../../app_project/
And now it all works swimmingly outside of Eclipse!

Android Project Dependencies

I need help with a project dependency issue in the Android SDK.
I have the following projects:
"Sync" (a Java only project)
"Agonis" (an Android Library project)
"Dragonis" (an Android application project)
Agonis has a project dependency on Sync, and Dragonis has a project dependency on Agonis, like so:
Dragonis->Agonis->Sync
I have correctly used the Properties->Android->Library dialog to add Agonis as an Android Library. I have exported the Sync project in the Agonis build path setup:
http://i.stack.imgur.com/GsQIR.png
In the Dragonis project, I see both Agonis and Sync in the "Android Dependencies" virtual folder:
http://i.stack.imgur.com/EhH8L.png
From here, I would expect everything to just work, but it doesn't.
I can start the app, but as soon as I instantiate a type from the Agonis project (com.mob.agonis.AgonisServer), I get a NoClassDefFoundError. If it matters, the Agonis type I am trying to instantiate extends a type that is in the Sync project (com.mob.sync.Server).
I tried cleaning all the projects, and using Android Tools->Fix Project Properties on both Agonis and Dragonis to no avail.
Am I overlooking something?
Put the jar library in the folder libs and add it to the java built path.
Right click on the file -> Build Path -> Add to Build Path
Hope it helps.
I figured it out.
The Sync Java-only library was compiling to 1.7 Java byte code. Android only supports converting Java 1.6 byte code to DEX.
Scroll to the top of the console, kids.
You can solve this through the command line tool. You could create/update the Dragonis and Agonis android projects - one as an application and other as library. See http://developer.android.com/tools/projects/projects-cmdline.html
You can easily set up the dependency from D -> A using the command line tools as shown in section called referencing a library project. At this stage, a build of D automatically triggers a build of A.
Now modify the build.xml of A - to include a dependency on : build and copy Sync.jar at the pre-compile stage (look inside build.xml to find the placeholder). The build and copy tasks can be done by starting a sub ant process using subant. See http://ant.apache.org/manual/Tasks/subant.html
At this stage all your dependency work properly on command line if you trigger ant build. Now you can also use eclipse by importing the android project Dragonis from source.
From the project properties of "Sync" set the "Java Compiler"-> "JDK Complience" to 1.6. Rebuild and enjoy!

Adding android library project to an android project prevents Ant from building

I'm adding an android library project to my android app (in this case, Beintoo). It's not mine; It's an external library. However, it doesn't come with a build.xml. Building in Eclipse works fine, but when I attempt to build my app with ant, I get this:
BUILD FAILED
C:\Sandbox\MyProject\build.xml:110: The following error occurred while executing this line:
C:\Sandbox\MyProject\build.xml:41: The following error occurred while executing this line:
C:\Program Files (x86)\Android\android-sdk\tools\ant\build.xml:515: Invalid file: C:\Git\Beintoo-Android\beintoo-android-sdk\BeintooSDK\build.xml
It's perfectly true. The file is invalid, because it doesn't exist. I wouldn't know what to do to add it safely, nor if that's even a good idea.
I have learnt that I can't simply build the library project into a jar. How can I get my project to build in Ant with this library project?
Ok, so, Error 454's answer was close, but not quite right. Since r14 of Android Tools, every library project must have it's own build.xml if it is to be built by Ant, as noted here:
https://groups.google.com/forum/?fromgroups#!topic/adt-dev/Z2e3dY-3Ma0
Running android update lib-project (which, as Error 454 notes, is in the android-sdk/tools folder which should be in PATH) on the library project will add a generic build.xml, and allow the main project to build.
For library projects, you need to browse to the library project root and run:
android update lib-project -p .
The android executable is in the android sdk/tools folder which should be added to your path variable in your OS. Once you run this, the necessary build files will be generated and your ant build should succeed.
Similarly, if your root project doesn't have the necessary build files, you will need to browse to the main project root and run:
android update project -p .
Additionally to the two previous correct answers I had to add --target android-16 because I was getting an "Error: The project either has no target set or the target is invalid."
So in my case
android update lib-project -p . --target android-16
Did it. (replace the 16 as you need)

How do I build my project with ANT when it depends on a non-library project with an Android library project dependency?

I'm trying to build my project using ANT. My project uses ActionbarSherlock, and Roboguice-Sherlock. ActionbarSherlock is an Android library project, but Roboguice-Sherlock is not. It's not even an Android project at all. See here:
https://github.com/rtyley/roboguice-sherlock
I can get ActionbarSherlock to build using ANT no problem, the problem lies with Roboguice-Sherlock. It's not an Android project, yet it has dependencies on Android and ActionbarSherlock, as well as Roboguice. When I try to build I get a ton of errors saying it can't find the Roboguice-Sherlock stuff.
If your project has the Roboguice .jar and the library project reference to ActionBarSherlock already configured you should be able to just drop it into your libs/ folder to have it automatically picked up.
I am working with Roberto to ensure that the .jar of his project gets uploaded to GitHub for non-Maven users to use.
For now, you can clone the project simply run mvn clean package to get a standalone .jar in the target/ folder which will enable you to do as I described above.
You need to add robiguice-sherlock as a .jar to your Android project. You can download it here (click Download (JAR)). Trying to build the source in Android will fail because it is not an Android project. Once it is in your directory, you need to open project properties and add the .jar to your build path.

Build Android Library Project With Ant

I am having trouble building my Android library project with ant. When I try to run ant release, it says Target "release" does not exist in the project "MyProject".
I then assumed that perhaps libraries do not get build with release/debug, so I started using ant compile, which seems to work. I then zip the folder manually using java's zip utility and rename it to .apk. I am guessing this is the wrong way to do this.
Can anyone show me how to build an Android library project using ant?
I then assumed that perhaps libraries do not get build with release/debug
Correct.
I am guessing this is the wrong way to do this.
Also correct.
Can anyone show me how to build an Android library project using ant?
You typically do not build an Android library project. You build other projects that reference the Android library project. Creating projects that reference the library project, for use with Ant, is covered in the Android documentation. More information about the role of Android library projects can also be found in the Android documentation.
I had the same trouble with a target "nodeps" that was not known by Ant when building a project that was referencing a library. I added a fake target "nodeps", then I get the target "release" unknown. Wrong way.
Solution : in my referenced library directory, I run the command android update project -p . that created the build.xml compatible with Ant build.

Categories

Resources