简体   繁体   English

使用Ant编译Android库时排除Java文件

[英]Exclude java files when compiling an android library using ant

I have an android library project which is build using ant. 我有一个使用ant构建的android库项目。 Everything works fine, but I have some test java files there that I do not want to be included in the generated jar file, anybody know how to make that? 一切正常,但是我那里有一些我不想包含在生成的jar文件中的测试Java文件,有人知道怎么做吗?

Thanks! 谢谢!

Android Studio/Gradle allows you to specify classes to exclude from the build; Android Studio / Gradle允许您指定要从构建中排除的类; in Android Studio you can exclude classes by editing the build.gradle file and adding a custom SourceSet . 在Android Studio中,您可以通过编辑build.gradle文件并添加自定义SourceSet来排除类。 Something like this: 像这样:

android
{
    compileSdkVersion 20
    buildToolsVersion "20.0.4"

    defaultConfig
    {
        minSdkVersion 19
        targetSdkVersion 19
        packageName "org.google.home"
        testPackageName "org.google.home.test"
    }
    sourceSets
    {
        main
        {
            java
            {
                exclude '**/ClassToExclude.java'
            }
        }
        androidTest
        {
            java
            {
                exclude '**/TestforClassToExclude.java'
            }
        }
    }
}

As you can see above, you can exclude both test files (located in you project test directory) and standard source files. 如上所示,您可以排除测试文件(位于项目测试目录中)和标准源文件。

EDIT 编辑

For ant you can use something similiar, known as a fileset so specify files that should be exluded from the build. 对于ant您可以使用类似的名称(称为fileset因此指定应从构建中排除的文件。 The user manual has an excellent section that describes how to accomplish this: https://ant.apache.org/manual/Types/fileset.html . 用户手册中有一个出色的章节介绍了如何完成此操作: https : //ant.apache.org/manual/Types/fileset.html

ant also provides an <exclude> tag that you can use to specify files to exclude: ant还提供了<exclude>标记,可用于指定要排除的文件:

<exclude name="**/dir_name_to_exclude/**" />

There is also a stackoverflow post that discusses a similar issue: How to exclude a directory from ant fileset, based on directories contents 还有一个stackoverflow帖子,讨论了类似的问题: 如何基于目录内容从ant文件集中排除目录

I finally fi it adding the following lines in the build.xml of my android project: 我终于在Android项目的build.xml中添加了以下几行:

<target name="-post-compile">
    <jar destfile="bin/myLibrary.jar">
        <zipfileset src="bin/classes.jar" excludes="com/my/package** com/something/unittest/**"/>
    </jar>
</target>

This will remove the unwanted files and update the jar file 这将删除不需要的文件并更新jar文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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