Pergunta

I have an Android Library project, AndroidLib, that depends on a Java project in my workspace, JavaLib.

In Eclipse this is done by adding the JavaLib project to Java Build Path > Projects for AndroidLib and setting it to be exported in the Order and Export tab, and it builds fine.

However Ant doesn't seem to pick up the dependency on the JavaLib project when building AndroidLib (I have run android update lib-project). What is the best way to add this dependency to my build.xml?

Foi útil?

Solução 2

I'll answer my own question: the only way to do this properly is to not use Ant.

  1. You need a proper dependency management system. Maven is your only real choice here.
  2. You need a build system that supports Maven. I currently use Maven again for this, because at the time it was the only mature build tool that also supported Maven dependency management -- now (albeit for Android Library Projects, since the format for these is not yet final) Gradle should be your build tool of choice.

Outras dicas

I had the same problem. I ended solving it in a very hackish way.

In AndroidLib/build.xml (or better in AndroidLib/custom_rules.xml), I defined a -pre-build target that builds the JavaLib and copies the resulting jar in libs/. I also defined a -post-package target to remove the copied jar, otherwise Eclipse will get confused.

<property name="lib.javalib.project.dir" location="${basedir}/../JavaLib" />
<target name="-pre-build">
  <subant buildpath="${lib.javalib.project.dir}" target="package" failonerror="true" />
  <copy todir="${basedir}/libs" failonerror="true" verbose="true">
      <fileset dir="${lib.javalib.project.dir}/target">
          <filename name="javalib*.jar"/>
      </fileset>
  </copy>
</target>
<target name="-post-package">
    <delete verbose="true">
        <fileset dir="${basedir}/libs" includes="javalib*.jar" />
    </delete>
</target>

This solution is far from satisfying, but it gets the job done.

You will find a similar question and answer there : Android Ant Include Java Library projects

I changed my build.xml to run the java project's ant file and then copy the jar to the libs folder:

    <target name="-pre-compile">
        <ant antfile="build.xml" dir="dependencies/JavaUtils" target="clean"/>
        <ant antfile="build.xml" dir="dependencies/JavaUtils"/>
        <copy todir="${jar.libs.dir}" failonerror="true" file="dependencies/JavaUtils/dist/Java-utils.jar"/>
        <sleep seconds="3"/> <!-- Delay for the file to be recognized after the copy -->
    </target>

How this helped someone.

BTW, I started reading about Gradle's dependency/build/whatever system and got the creeps from it. Ahhh, what a relief to get back to ant...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top