Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Ant build -- How to compile against a JAR but exclude it from classes.dex?

Tags:

android

ant

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.

like image 581
Chillon Avatar asked May 31 '12 08:05

Chillon


2 Answers

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.

like image 61
user3647877 Avatar answered Nov 15 '22 09:11

user3647877


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.

like image 27
curioustechizen Avatar answered Nov 15 '22 08:11

curioustechizen