10

These 4 files (build.xml, local.properties, projects.properties, proguard.cfg) are auto-generated when running:

android update project --name TestApp --target 10 -p .

Updated project.properties

Updated local.properties

Added file ./build.xml

Updated file ./proguard.cfg

But I want the "auto-gen" build.xml to also include the following "pre-compile" code as well, is there a way to do that? Is there some "include" file or "template" file which can include that?

  <target name="config">

  <property name="config-target-path" value="${source.dir}/com/androidengineer/antbuild"/>

  <!-- Copy the configuration file, replacing tokens in the file. -->
  <copy file="config/Config.java" todir="${config-target-path}"
        overwrite="true" encoding="utf-8">
   <filterset>
    <filter token="CONFIG.LOGGING" value="${config.logging}"/>
   </filterset>
  </copy>

 </target>
Community
  • 1
  • 1
samxiao
  • 2,587
  • 5
  • 38
  • 59
  • Try to paste that code into your build.xml and then modify the "compile" target (it may be another name) like ... – Dante WWWW Dec 16 '11 at 01:45
  • Hi, What I mean is "build.xml" is auto-generated when running "android update". I can't paste that code in there. That's my problem and want to know if I can paste that code somewhere else but still able to get included when "android update" runs. – samxiao Dec 16 '11 at 23:37
  • Will the build.xml run right after it is generated? – Dante WWWW Dec 19 '11 at 07:07
  • It can be delay, but yes, now it is run right after it is generated. – samxiao Dec 19 '11 at 20:00

3 Answers3

13

The build.xml as generated by the android tool (at least when using Android SDK r20) contains this piece of code:

<!--
    Import per project custom build rules if present at the root of the project.
    This is the place to put custom intermediary targets such as:
        -pre-build
        -pre-compile
        -post-compile (This is typically used for code obfuscation.
                       Compiled code location: ${out.classes.absolute.dir}
                       If this is not done in place, override ${out.dex.input.absolute.dir})
        -post-package
        -post-build
        -pre-clean
-->
<import file="custom_rules.xml" optional="true" />

So what I do to create additional targets or customize existing targets is to create a custom_rules.xml file with these new targets. Note that in my tests the targets needed to be nested in a <project> tag, so simply copy the first two lines of your generated build.xml to custom_rules.xml, and don't forget about the closing </project> tag in the end. As custom_rules.xml will not be overwritten by android update your changes will be persistent and can be checked into your SCM tool of choice.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
3

Maybe you can check the answer to this question:

Does the ADT plugin automatically create an ant build file?

in the answer there is a paragraph saying...

Alternatively, you can just copy the build.xml template directly from $ANDROID_HOME/tools/lib/build.template then just change the project name.

Modify this file and run the commands to see if it works.

Update

Also check "Customizing the build" of this article: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

Community
  • 1
  • 1
Dante WWWW
  • 2,729
  • 1
  • 17
  • 33
  • Thanks! But this does not work as I always run "android update project" at each time it builds which always generated a new "build.xml" and replace the existing one. I'm looking for a solution to that. – samxiao Dec 20 '11 at 23:31
  • Then I don't know why it doesn't work. Since there is a build.xml template, I think it should generate a new build.xml from the template each time you run "android update project", but it seems that is doesn't... Sorry for that. – Dante WWWW Dec 21 '11 at 01:27
  • Hi coolcfan, thanks for your ansewr, but I don't think you understand my problem here. I can make changes to the template, but that would be affecting all of my "other" projects as they are using the same SDK. I only want the customized changed for this particular project for this particular build. `So here is my situation:` `1. Copy $ANDROID_HOME/tools/lib/build.template to build.xml` `2. Modify the build.xml` `3. Run "android update project"` `4. "android update" generate a new build.xml which now replaced my copy of modified build.xml` – samxiao Dec 21 '11 at 02:38
  • A stupid way is to write a script which overwrites the build-template at the start of your building process, then runs the commands, and then, whether the build succeeds or not, restore the original build-template in the end...... – Dante WWWW Dec 21 '11 at 02:48
  • Thanks coolcfan, that's what I originally think about. I was just curious to know if there's a better solution for this like a way to have "android update project" to include some customized build.xml content when generating it.. – samxiao Dec 21 '11 at 05:01
  • Since you have more than one projects... Maybe there isn't. Does "android update project" has the ability to let you specify a build.xml template as a parameter or in a properties file? If not... Then the "stupid" way has to be the choice :P – Dante WWWW Dec 21 '11 at 05:46
1

In build.xml itself, it tells you how to do this. See the following comment from the file:

<!--

    ***********************
    ****** IMPORTANT ******
    ***********************
    In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
    in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • I tried, and it didn't work :-( But it could because a bug I had in my Android SDK. I will try again with the new update. – samxiao May 31 '12 at 22:39