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

I collect Android APK using Ant. The APK will be installed in the environment with the jar file (name it foo.jar). Therefore, the APK will need to learn about foo.jar at compile time, but I do not want it to be included in classes.dex (since it will already be available).

Please note that this is not just about putting the jar in the "libs" directory. Although this fixes the "compilation" of the problem part, it does not solve the problem of saving jar from classes.dex.

Thanks in advance for your help.

+6
source share
3 answers

Two paths are used for compilation, used as -compile as classpathref, 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 own classpath and the tested project 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 "test.project.classpath", but the path "test.project.classpath" is not used in dexing, so you can change it in a pre-compile target, for example 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 add the path to the exported jars to "test.project.classpath" before starting the compilation. You can put "exported.jars.dir" in your ant.properties file.

+6
source

How to place the jar in some other directory (say foo/ ) and add this to the compilation path? Thus, the JAR is not "exported" and, therefore, the dex tool does not act on it.

+1
source

What works for me is to copy the exclude flags to my "libs" directory before compilation, to the target "-pre-compile", and then delete these files again from "libs" after compiling to the tag "-post-compile".

Note:

  • this did not work, if I copied the files to the target "-pre-build", it should have been "precompiled"
  • After copying, I had to set the path "project.all.jars.path" to avoid errors.

In my pasted code example, I need banks in the directory referenced by the "libs.ads.dir" property for compilation, but don’t want them to be 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> 
+1
source

Source: https://habr.com/ru/post/917081/


All Articles