|
First, the tools catalog under Android sdk added to the system path environment variable, or you have to directly specify the absolute path android.bat
For a new project, you can use this command to create the required ant build environment (see android default project file structure)
android create project -k com.foo -a Test1 -t android-8 -p d: \ temp
If it is an existing project, the main project and subprojects run
Project Directory> android update project -s -p. -t Android-19
-s is because the tape project
-t is specified target version, the version does not cause the compiler to fail
among them
build.xml ant task file is the basic need to modify
custom_rules.xml for compilation task requires self-configuring, wrote this document, will be loaded build.xml
ant.properties ant run variables involved wrote here
local.properties which set the path sdk.dir without modification
project.properties set a target to compile and project type, if there is sub, then there subprojects path without modification
Then execute the following command to compile the
Project directory> D: \ apache-ant-1.8.4 \ bin \ ant.bat clean
Project directory> D: \ apache-ant-1.8.4 \ bin \ ant.bat release
According to the newspaper the wrong way, make adjustments
If the sub-compilation errors, you can run ant clean ant release and in the sub-directory, respectively, and to exclude errors
Two similar examples custom_rules.xml
< ? Xml version = "1.0" encoding = "UTF-8"?>
< Project name = "custom_rules">
< Property name = "generated.dir" value = "gen" />
< Property name = "generated.absolute.dir" location = "$ {generated.dir}" />
< Property name = "java.compilerargs" value = "- s & apos; $ {generated.absolute.dir} & apos;" />
< Target name = "- pre-compile">
< Mkdir dir = "$ {generated.absolute.dir}" />
< / Target>
< Target name = "- compile" depends = "- build-setup, -pre-build, -code-gen, -pre-compile">
< Do-only-if-manifest-hasCode elseText = "hasCode = false. Skipping ...">
< -! Merge the project's own classpath and the tested project's classpath ->
< Path id = "project.javac.classpath">
< Path refid = "project.all.jars.path" />
< Path refid = "tested.project.classpath" />
< Fileset dir = "compile-libs" includes = "*. Jar" />
< / Path>
< Javac encoding = "$ {java.encoding}"
source = "$ {java.source}" target = "$ {java.target}"
debug = "true" extdirs = "" includeantruntime = "false"
destdir = "$ {out.classes.absolute.dir}"
bootclasspathref = "project.target.class.path"
verbose = "$ {verbose}"
classpathref = "project.javac.classpath"
fork = "$ {need.javac.fork}">
< Src path = "$ {source.absolute.dir}" />
< Src path = "$ {gen.absolute.dir}" />
< Compilerarg line = "$ {java.compilerargs}" />
< / Javac>
< -! If the project is instrumented, intrument the classes ->
< If condition = "$ {build.is.instrumented}">
< Then>
< Echo level = "info"> Instrumenting classes from $ {out.absolute.dir} / classes ... < / echo>
< -! Build the filter to remove R, Manifest, BuildConfig ->
< Getemmafilter
appPackage = "$ {project.app.package}"
libraryPackagesRefId = "project.library.packages"
filterOut = "emma.default.filter" />
< -!. Define where the .em file is going This may have been
setup already if this is a library ->
< Property name = "emma.coverage.absolute.file" location = "$ {out.absolute.dir} /coverage.em" />
< -! It only instruments class files, not any external libs ->
< Emma enabled = "true">
< Instr verbosity = "$ {verbosity}"
mode = "overwrite"
instrpath = "$ {out.absolute.dir} / classes"
outdir = "$ {out.absolute.dir} / classes"
metadatafile = "$ {emma.coverage.absolute.file}">
< Filter excludes = "$ {emma.default.filter}" />
< Filter value = "$ {emma.filter}" />
< / Instr>
< / Emma>
< / Then>
< / If>
< -! If the project is a library then we generate a jar file ->
< If condition = "$ {project.is.library}">
< Then>
< Echo level = "info"> Creating library output jar file ... < / echo>
< Property name = "out.library.jar.file" location = "$ {out.absolute.dir} /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.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} /BuildConfig.class" />
< Fileset dir = "$ {source.absolute.dir}" excludes = "** / *. Java $ {android.package.excludes}" />
< / Jar>
< / Then>
< / If>
< / Do-only-if-manifest-hasCode>
< / Target>
< / Project>
another
< ? Xml version = "1.0" encoding = "UTF-8"?>
< Project name = "app_project" default = "help">
< 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;" />
< Target name = "- pre-compile">
< Mkdir dir = "$ {generated.absolute.dir}" />
< / Target>
< -! Compiles this project's .java files into .class files -.>
< Target name = "- compile" depends = "- pre-build, -build-setup, -code-gen, -pre-compile">
< Do-only-if-manifest-hasCode elseText = "hasCode = false. Skipping ...">
< -! Merge the project's own classpath and the tested project's classpath ->
< Path id = "project.javac.classpath">
< Path refid = "project.all.jars.path" />
< Path refid = "tested.project.classpath" />
< Path path = "$ {java.compiler.classpath}" />
< Fileset dir = "compile-libs" includes = "*. Jar" />
< / Path>
< Javac encoding = "$ {java.encoding}"
source = "$ {java.source}" target = "$ {java.target}"
debug = "true" extdirs = "" includeantruntime = "false"
destdir = "$ {out.classes.absolute.dir}"
bootclasspathref = "project.target.class.path"
verbose = "$ {verbose}"
classpathref = "project.javac.classpath"
fork = "$ {need.javac.fork}">
< Src path = "$ {source.absolute.dir}" />
< Src path = "$ {gen.absolute.dir}" />
< Compilerarg line = "$ {java.compilerargs}" />
< / Javac>
< -! If the project is instrumented, intrument the classes ->
< If condition = "$ {build.is.instrumented}">
< Then>
< Echo level = "info"> Instrumenting classes from $ {out.absolute.dir} / classes ... < / echo>
< -! Build the filter to remove R, Manifest, BuildConfig ->
< Getemmafilter
appPackage = "$ {project.app.package}"
libraryPackagesRefId = "project.library.packages"
filterOut = "emma.default.filter" />
< -!. Define where the .em file is going This may have been setup already if this is a library ->
< Property name = "emma.coverage.absolute.file" location = "$ {out.absolute.dir} /coverage.em" />
< -! It only instruments class files, not any external libs ->
< Emma enabled = "true">
< Instr verbosity = "$ {verbosity}"
mode = "overwrite"
instrpath = "$ {out.absolute.dir} / classes"
outdir = "$ {out.absolute.dir} / classes"
metadatafile = "$ {emma.coverage.absolute.file}">
< Filter excludes = "$ {emma.default.filter}" />
< Filter value = "$ {emma.filter}" />
< / Instr>
< / Emma>
< / Then>
< / If>
< -! If the project is a library then we generate a jar file ->
< If condition = "$ {project.is.library}">
< Then>
< Echo level = "info"> Creating library output jar file ... < / echo>
< Property name = "out.library.jar.file" location = "$ {out.absolute.dir} /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.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} /BuildConfig.class" />
< Fileset dir = "$ {source.absolute.dir}" excludes = "** / *. Java $ {android.package.excludes}" />
< / Jar>
< / Then>
< / If>
< / Do-only-if-manifest-hasCode>
< / Target>
< / Project>
Examples of ant.properties, and above is the corresponding xml
key.store = D: /workAndroid/files/android_key.store
key.alias = alia
key.store.password = 111111
key.alias.password = 111111
java.encoding = UTF-8
java.target = 7
java.source = 7
Use the version number in the generated apk file name, rather than using the default xxx-release.apk
http://jeffreysambells.com/2013/02/14/build-your-android-apk-with-the-manifest-version-number
< Target name = "- set-release-mode"
depends = "rename-release-with-version-number, android_rules.-set-release-mode">
< Echo message = "target: $ {build.target}"> < / echo>
< / Target>
< Target name = "rename-release-with-version-number">
< Xmlproperty file = "AndroidManifest.xml" prefix = "themanifest" collapseAttributes = "true" />
< -! See $ {sdk.dir} /tools/ant/build.xml -set-release-mode ->
< Property name = "out.packaged.file"
location = "$ {out.absolute.dir} / $ {ant.project.name} - $ {themanifest.manifest.android:versionName}-release-unsigned.apk" />
< Property name = "out.final.file"
location = "$ {out.absolute.dir} / $ {ant.project.name} - $ {themanifest.manifest.android:versionName}-release.apk" />
< / Target>
The -set-release-mode target overrides the same target in $ {sdk.dir} /tools/ant/build.xml where the out.final.file file name is originally defined. To add the version number, I override -set -release-mode by calling my rename-release-with-version-number target in the dependencies and then calling the original android_rules.-set-release-mode to finish any other setup there.
The rename-release-with-version-number target simply reads in the manifest and adds the version number to both out.final.file and out.packaged.file.
Now the build APK is ProjectName-version-release.apk and it's automatically used by the rest of the ant build process without clumsy file renaming. |
|
|
|