Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Android project with ant having a library project dependency on another library project

Tags:

android

ant

I have maybe this not so common setup:

(> = dependency)
Android project > Android library project 1 > Android library project 2
So I have an Android library project which has a dependency to another library project.

When I'm building project in Eclipse everythings works fine but I cannot get my build working with Ant.

First Ant compiles Android library project 2 which generates a classes.jar and puts this file in the bin folder.
Then Ant tries to compile the Android library project 1 but then I'm getting errors becouse it is missing classes from Android library project 2.

Well this is not so weird becouse the jar file is not included in the libs folders. But in project.properties i've made a dependency to the library project 2 so why does Ant not copy the classes.jar to the libs folders of library project 1?

Well I can think of a solution to use an Ant task to copy the file to the libs folder, but then I have to modify the build.xml which I do not prefer.

** EDIT

The problem is that the R class is missing, when I look in classes.jar this java file does not contain the R class. So my solution would proberly not work.

like image 694
user1051892 Avatar asked May 14 '12 09:05

user1051892


4 Answers

This behaviour was caused by a change in R17 of the build tools: http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

In a nutshell: R files for libraries are no longer packaged in the classes.jar for that library. However, since the pareent.R for the parent-library (project1 in your example) also contains the resource-references for the 'child' library (project2 in your example), you don't have to refer to the child-R anyway.

Replace all project2.R-import statements in project1 with project1.R import statements and you should be fine.

like image 71
edovino Avatar answered Nov 13 '22 14:11

edovino


For ant to compile add dependency in ant.properties. eg:

android.library.reference.1=../path/to/library
like image 25
Tarun Avatar answered Nov 13 '22 12:11

Tarun


This sounds like a very brittle setup - you may have good reason for this, but could you instead decouple the libraries dependence on each other?

For example; implement a bridge pattern to translate the calls between both libraries, and make the calling Android project attach them. This way, you have no code in either library that depends on the other, and the only project that needs to handle dependency setup is your main project.

One of the main reasons for using a library is to make the code re-usable, this approach ensures someone (you, a colleague, your successor...) could plug in just one library and create their own implementation of the other one.

Here is another good article on applying the bridge pattern in Java: http://java.dzone.com/articles/design-patterns-bridge

like image 1
seanhodges Avatar answered Nov 13 '22 13:11

seanhodges


Well the problem was that the R class was missing.
So i removed the R class dependency between the two library projects.
I don't know if this is fixable but i think it is bad practice any way.

Without this dependency Ant builds fine.

like image 1
user1051892 Avatar answered Nov 13 '22 14:11

user1051892