简体   繁体   中英

How to specify multiple source directory for Android library project

I am using ant tool to build android library project, I need to specify multiple source directory .

I have tried to specify multiple source directory by adding these lines alternatively in ant.properties file

source.dir=src:src2    
source.dir=src;src2

but unable to build in both case, in both the cases .class were generated , but while creating jar file i was getting this error

BUILD FAILED

C:\\Program Files\\Android\\android-sdk\\tools\\ant\\build.xml:681: The following error occurred while executing this line:

C:\\Program Files\\Android\\android-sdk\\tools\\ant\\build.xml:749:

C:\\workarea\\Android\\Packages\\test\\src;src2 does not exist.

can any one tell me How to specify multiple source directory in ant.properties to build Android library projects ?

This did the trick for me without modifying SDK files:

ant.properties:

source.absolute.dir = tmp-src

custom_rules.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="CustomRules">
    <target name="-pre-build" >
        <copy todir="tmp-src" >
            <fileset dir="src" includes="**" />
            <fileset dir="src-1" includes="**" />
            <fileset dir="src-2" includes="**" />
            <fileset dir="src-3" includes="**" />
            <fileset dir="src-4" includes="**" />
        </copy>
    </target>

    <target name="-post-build" >
        <delete dir="tmp-src" />
    </target>
</project>

I have Solved This issue in a tricky way; Here it is

To building library-project in android with more than one source directory, first go to ant.properties file (for linux it is build.properties ) and add source.dir

    source.dir=first_source_dir ;second_source_dir ; third_source_dir

for lib project , ant creates the jar library with compiled .class files from the directory bin/classes of out.dir directory specified in ant.properties or build.properties file;

While creating jar , ant removes all .java source file form the jar , which may be included with the jar if coder keeps any .java source file in out.dir directory, and specify that directory in source.dir ;

Now to removing those .java source ant goes to the source.dir directory with the following command

        <property name="source.absolute.dir" location="${source.dir}" />
        dir="${source.absolute.dir}" 

With this command ant actually trying to go the directory

cd   <your_project_root_dir>/first_source_dir ;second_source_dir ; third_source_dir

which is not present ...

Solution:

step 1. At first make sure that your source directory (source.dir) and build directory ( out.dir ) are different;

step 2. go to C:\\Program Files\\Android\\android-sdk\\tools\\ant open build.xml then go to jar tag

    <jar destfile="${out.library.jar.file}">                        
       <fileset dir="${out.classes.absolute.dir}"
         includes="**/*.class"
         excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/Manifest.class ${project.app.package.path}/Manifest$*.class ${project.app.package.path}/BuildConfig.class"/>
       <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
   </jar>

Now comment or remove the last fileset tag in jar tag

    <jar destfile="${out.library.jar.file}">                        
       <fileset dir="${out.classes.absolute.dir}"
            includes="**/*.class"
            excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/Manifest.class ${project.app.package.path}/Manifest$*.class ${project.app.package.path}/BuildConfig.class"/>
       <!--fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" /-->
    </jar>

Now build your project ;

  1. Create a dir (say build_src) in your project dir.
  2. go into that dir (cd build_src)
  3. Create soft links (ln -s TARGET linkname) in build_src to all the source directories you want to include eg: if your original problem is to include src1, src2 along with standard src dir, create src1, src2 softlinks to those under build_src
  4. create ant.properties with source.dir=build_src

This worked fine for me and I think is the cleanest approach (also since those are soft links to actual source, you don't have multiple copies of source)

  1. One of my project had .aidl files and AIDL complained. Instead of creating softlink to src, I created softlink to src/com and fixed the issue.

At least on later versions of the build tools, the problem is that the value put into this property gets passed into a <property name="source.absolute.dirs" location="source.dirs"/> . The location attribute doesn't know how to deal with colon / semicolon-separated paths.

The fix is very simple, just use:

source.absolute.dirs=src1:src2:src3

etc., as opposed to:

source.dirs=src1:src2:src3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM