咱们知道,能够经过以下设置将一个普通的Android工程转换成Android Library工程android
设置先后工程变化以下app
使用Ant编译时(经过android.bat update project 命令生成 build.xml),普通的Android工程会生成apk文件,而Android Library工程只生成jar文件。因为要生成dex并打包apk资源,前者比后者要耗时很多。post
有时咱们须要从普通工程中导出部分代码生成jar包,能够手动完成ui
或者,按照上面的作法,先将一个普通的Android工程转换成Android Library工程,再执行 ant release 或 ant debugdebug
其实,还有一种更简单地方法:使用以下方式执行 ant releasecode
ant release -Dandroid.library=true
编译后将生成一个classes.jar,编译输出以下xml
... -compile: [jar] Building jar: F:\xxx\bin\classes.jar -post-compile: -obfuscate: -dex: [echo] Library project: do not convert bytecode... ...
最后,咱们还能够经过添加一个 custom_rules.xml 文件来对生成的jar文件进行更灵活地控制:资源
<?xml version="1.0" encoding="UTF-8"?> <project name="tinyUtils" default="help"> <target name="-post-compile"> <!-- copy from <sdk>\tools\ant\build.xml '-compile' task --> <if condition="${project.is.library}"> <then> <echo level="info">Creating my library output jar file...</echo> <property name="out.mylibrary.jar.file" location="${out.absolute.dir}/my_classes.jar" /> <if> <condition> <length string="${android.package.excludes}" trim="true" when="greater" length="0" /> </condition> <then> <echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo> </then> </if> <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" /> <jar destfile="${out.mylibrary.jar.file}"> <!-- 自定义 --> </jar> </then> </if> </target> </project>
-Dandroid.library=true
参数从普通Android项目中导出jar包