ant编译android程序,遇到butterknife的特殊处理

来源:互联网 发布:上海华讯网络2018校招 编辑:程序博客网 时间:2024/05/16 01:28

原文链接


Building Project Ant

What you should know before starting

AndroidAnnotations works by generating code at compile time.

AndroidAnnotations provides two jars:

  • androidannotations-X.Y.jar is needed to generate the code at compile time. There is no reason to keep this jar at runtime because:
    • Its code will never be executed at runtime.
    • It makes your APK size bigger than needed.
  • androidannotations-X.Y-api.jar only contains the code you need at runtime.

Prerequisites

  • This tutorial is based on the SDK v19. If you use another version, you may need to adapt this tutorial.

  • if you don't already have a build.xml file, you can easily generate one:

android update project --path "$PROJECT_ROOT$"

How to

  • Create a new folder at the root of your project (compile-libs would be a good candidate) and put androidannotations-X.Y.jar in this folder.

  • Put androidannotations-X.Y-api.jar in the $PROJECT_ROOT$/libs

  • Create a custom_rules.xml Ant file next to your build.xml Ant script. Thebuild.xml script generated by the Android tools automatically importscustom_rules.xml if it exists. This enables you to customize the build, without having to modify build.xml, which can therefore be easily updated.

  • Add properties for the generated source folder in custom_rules.xml

    <property name="generated.dir" value=".apt_generated" />    <property name="generated.absolute.dir" location="${generated.dir}" />    <property name="java.compilerargs" value="-s &apos;${generated.absolute.dir}&apos;" />

Note: In some case you may have to replace .apt_generated by gen to make the whole thing works.

  • Override the -pre-compile target in custom_rules.xml
    <target name="-pre-compile">        <mkdir dir="${generated.absolute.dir}" />    </target>
  • Override the -compile target in custom_rules.xml

    • Open $ANDROID_SDK_ROOT$/tools/ant/build.xml

    • Locate the -compile target in this file:

<target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile"> ...</target>
  • Copy the target and its content into custom_rules.xml

  • Modify the classpath when javac is invoked by adding a <fileset> node, and configure javac to generates the sources in a dedicated folder:

<target name="-compile" ...>...            <path id="project.javac.classpath">                ...+               <fileset dir="compile-libs" includes="*.jar"/>            </path>...</target>
  • You should now be able to build you project using ant:
ant clean release

Next steps

  • Configure Eclipse or IntelliJ
  • Start using AndroidAnnotations

Potential issues

  • If you put the two AndroidAnnotations jars in the $PROJECT_ROOT$/libsyou will encounter the following error:
java.lang.IllegalArgumentException: already added: Lcom/googlecode/androidannotations/annotations/AfterViews;

androidannotations-X.Y-api.jar is a subset of androidannotations-X.Y.jar. So each class in androidannotations-X.Y-api.jar is present in androidannotations-X.Y.jar.

This error is thrown when the dx command is invoked and two classes with the same name and package name are detected. To prevent this error you have to move androidannotations-X.Y.jar file away from the $PROJECT_ROOT$/libsfolder.

  • Anything else? Contact us on the mailing list, or create an issue, and we'll try to help you.

  • After upgrading the Android SDK, the content of$ANDROID_SDK_ROOT$/tools/ant/build.xml may have changed. Therefore, your ant build may be broken. The solution is to replace the content of the -compile target in custom_rules.xml with the new -compile target content defined in$ANDROID_SDK_ROOT$/tools/ant/build.xml



在实际操作中
<fileset dir="compile-libs" includes="*.jar"/>
这句是删掉的
0 0
原创粉丝点击