Reputation: 61
I am compiling an Android APK using Ant. The APK will be installed in an environment that has a jar file (call it foo.jar). So the APK will need to know about foo.jar during compilation, but I don't want it included in classes.dex (since it will already be available).
Please note that this is not just a matter of putting the jar in the 'libs' directory. While that fixes the 'compile' part of the problem, it does not solve the problem of keeping the jar out of classes.dex.
Thanks in advance for your help.
Upvotes: 6
Views: 2998
Reputation: 61
There are two paths used by -compile as a classpathref for compiling but one of them is not used in dexing.
<target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
<!-- merge the project's own classpath and the tested project's classpath -->
<path id="project.javac.classpath">
<path refid="project.all.jars.path" />
<path refid="tested.project.classpath" />
</path>
<javac encoding="${java.encoding}"
source="${java.source}" target="${java.target}"
debug="true" extdirs="" includeantruntime="false"
destdir="${out.classes.absolute.dir}"
bootclasspathref="project.target.class.path"
verbose="${verbose}"
classpathref="project.javac.classpath"
fork="${need.javac.fork}">
<src path="${source.absolute.dir}" />
<src path="${gen.absolute.dir}" />
<compilerarg line="${java.compilerargs}" />
</javac>
…
</target>
These paths are “project.all.jars.path" and ”tested.project.classpath" but the path ”tested.project.classpath" is not use in dexing so you can modify it in the pre-compile target like this:
<target name="-pre-compile" >
<path id="tmp">
<pathelement path="${toString:tested.project.classpath}"/>
<fileset dir=“${exported.jars.dir}” >
<include name="*.jar" />
</fileset>
</path>
<path id="tested.project.classpath"><pathelement path="${toString:tmp}"/></path>
<path id="tmp"/>
</target>
Here, you append the path of your exported jars to “tested.project.classpath” before the compilation begins. You can put “exported.jars.dir” in your ant.properties file.
Upvotes: 6
Reputation: 3246
What works for me is to copy the exclude jars to my "libs" directory before compilation, in target "-pre-compile", and then delete these files again from "libs" after compiling, in taget "-post-compile".
Note:
In my pasted code example I need the jars in the directory referenced by property "libs.ads.dir" for compilation, but do not want them included in my jar:
<target name="-pre-compile">
<copy todir="${jar.libs.dir}">
<fileset dir="${libs.ads.dir}"/>
</copy>
<path id="project.all.jars.path">
<fileset dir="${jar.libs.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
</target>
<target name="-post-compile" >
<delete>
<fileset dir="${jar.libs.dir}" casesensitive="yes">
<present present="both" targetdir="${libs.ads.dir}"/>
</fileset>
</delete>
</target>
Upvotes: 1
Reputation: 10672
How about putting the jar in some other directory (say, foo/
) and adding that to the compile classpath? That way the JAR is not "exported" and hence is not acted upon by dex tool.
Upvotes: 1