How to use Ant to auto renaming output apk file? - java

I'm currently playing with Ant to do some auto branding work. I modified default build.xml and setup my own target. What I hope to ask is that is there a way in Ant Script that could automatic renaming the apk file just build with the certain name?
I currently has this Ant target setup in my build.xml:
<target name="release-brandA"
depends="default, -set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
description="Builds the application in release mode for brandA.">
<delete dir="${res}" />
<copydir dest="${res}" src="${branding}/brandA" forceoverwrite="ture" />
<replaceregexp flags="g" byline="false">
<regexp pattern="import com.arthur.android(.*).R;"/>
<substitution expression="import com.arthur.android.brandA.R;"/>
<fileset dir="src" includes="**/*.java" />
</replaceregexp>
<replaceregexp flags="g" byline="false">
<regexp pattern="package="com.arthur.android(.*)"" />
<substitution expression="package="com.arthur.android.brandA"" />
<fileset dir="" includes="AndroidManifest.xml" />
</replaceregexp>
</target>
Is there a way that I could add some more task, to let the output file just be like brandA.apk?
Thank you!

The final apk filename is actually defined by the property 'out.final.file'
So you could create a new Task that sets this property:
<target name="-set-out-final-file">
<property name="out.final.file" location="${out.absolute.dir}/brandA.apk" />
</target>
Finally, you just need to invoke the -set-out-final-file target before calling the debug or release targets.
Using your release-brandA task, it would become this:
<target name="release-brandA"
depends="default, -set-out-final-file, -set-release-mode, -release-obfuscation-check...

Here I found more better explanation and more suitable way to override release target.
Please refer to section:
<target
name="release"
depends="custom-set-release-mode, android_rules.release" />

Its very simple.Refer this link [1]: https://ant.apache.org/manual/running.html
So in the above case,we need to run it like this,
ant debug -Dout.final.file=brandA.apk
Thats it.

Related

Using Ant to build dependencies of the current build target

Say I have a library and a binary target, let's call them MyLib and MyBin,
MyBin depends on MyLib.
I'm trying to create an Ant buildfile for MyBin that first builds MyLib and then includes it in the classpath when building MyBin.
I've tried using Ant tasks as in Building other, dependent projects with Ant .
However, it's not working, and from ant -v I think the MyBin build-deps target is not even building MyLib. Seems like it's confusing MyBin and MyLib properties? I'm not sure how to prevent this though.
I'm dumping only MyBin/build.xml below, but the MyLib is almost identical, except it does not have the build-deps target.
<project name="MyBin" default="main" basedir=".">
<property name="projectName" value="MyBin" />
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="dist.dir" location="dist" />
<property name="dist.lib.dir" location="dist/lib" />
<property name="lib.dir" value="lib" />
<target name="build-deps" depends="init">
<!-- MyLib main target does clean -> build -> jar to dist folder -->
<!-- Its build.xml uses many of the same property values as above -->
<ant antfile="../MyLib/build.xml" target="main"/>
</target>
<path id="classpath">
<fileset dir="${basedir}/">
<include name="../MyLib/dist/**/*.jar" />
</fileset>
</path>
<!-- Need classpath to run this -->
<target name="compile" depends="build-deps" description="compile the source ">
<javac includeantruntime="false" srcdir="${src.dir}"
destdir="${build.dir}" classpathref="classpath" />
</target>
<!-- Group all dependencies into a big dependency-all.jar -->
<target name="copy-dependencies">
<mkdir dir="${dist.lib.dir}" />
<jar jarfile="${dist.lib.dir}/dependencies-all.jar">
<zipgroupfileset dir="${lib.dir}">
<include name="**/*.jar" />
</zipgroupfileset>
</jar>
</target>
<!-- jar it, extract above dependency-all.jar and zip it with project files -->
<target name="jar" depends="compile, copy-dependencies"
description="package, output to JAR">
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.lib.dir}" />
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
</manifest>
<zipfileset src="${dist.lib.dir}/dependencies-all.jar"
excludes="META-INF/*.SF" />
</jar>
</target>
<target name="clean" description="clean up">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Default, run this -->
<target name="main" depends="clean, compile, jar" />
</project>
What I see with ant -v in MyBin is something along the lines of:
build-deps:
Project base dir set to: /MyBin
[ant] calling target(s) [main] in build file /MyLib/build.xml
parsing buildfile /MyLib/build.xml with URI = file:/MyLib/build.xml
Project base dir set to: /MyBin
Override ignored for property "projectName"
Override ignored for property "build.dir"
Override ignored for property "dist.dir"
Override ignored for property "dist.lib.dir"
Override ignored for property "lib.dir"
[pathconvert] Set property classpath.name =
[ant] Entering /MyLib/build.xml...
Build sequence for target(s) `main' is [clean, init, copy-dependencies, jar, main]
Complete build sequence is [clean, init, copy-dependencies, jar, main, ]
clean:
[delete] Deleting directory /MyBin/bin
[delete] Deleting directory /MyBin/bin
init:
[mkdir] Created dir: /MyBin/bin
copy-dependencies:
[ant] Exiting /MyLib/build.xml.
On your specific question:
seems like it's confusing MyBin and MyLib properties? I'm not sure how
to prevent this though.
You are using this to invoke the MyLib build:
<ant antfile="../MyLib/build.xml" target="main" />
A build invoked via the <ant> task this way by default inherits all properties from the caller, including basedir, hence the build is run in the wrong place.
Instead you could use, for example:
<ant dir="../MyLib" />
That will run build.xml in the specified directory, set the basedir property, and call the default target, which should be main if you are using a very similar buildfile for the library as you say. If you don't want to inherit properties from MyBin when executing the MyLib task, specify inheritAll=false in the task.
From the <ant> task docs for the dir attribute:
the directory to use as a basedir for the new Ant project (unless
useNativeBasedir is set to true). Defaults to the current project's
basedir, unless inheritall has been set to false, in which case it
doesn't have a default value. This will override the basedir setting
of the called project. Also serves as the directory to resolve the
antfile and output attribute's values (if any).

Can not compile project with ANT

I'm trying to compile project using:
ant compile
I've receive following error in terminal:
taskdef class org.testng.TESTNGNGAntTask cannot be found
using the classloader AntClassLoader[]
Here is my teskdef from build.xml file
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TESTNGNGAntTask" />
Could you include the ant file (by default is named build.xml) ?
Ensure you had installed Apache Ant in your computer: open a command line and execute ant -v. If appears a version message It's works.
If you find the ant file, include a compile target (or if you have one, modify it) like this:
<property name="src" location="src" />
<property name="build" location="build" />
<target name="compile">
<mkdir dir="${build}" />
<javac srcdir="${src}" destdir="${build}" />
</target>
The propierties are just constants.
Good luck!
The name of the class is TestNGAntTask (with one NG and with only the T of Test capitalized), not TESTNGNGAntTask (with two NG's and an entirely capitalized TEST).
Try the following instead:
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />

Could not find or load main class Java

Good evening Stack Overflow!
I started learning Java quite a few days ago, but not using an IDE once my computer is a bit slow. So, I decided to use Sublime Text, and compile things using ant on the cmd, since it seems to be the most reasonable thing to do.
Today I started ( at least tried ) to follow along a series of LWJGL tutorials (which are really cool), from ThinMatrix, but I can't manage to solve an error which I get every time I try to compile the 'project'.
This is the structure of my project:
LWJGL
src
com
game
test
MainGameLoop.java
renderEngine
DisplayManager.java
build.xml
And ladies and gentlemen... the build.xml (following Ant's official HelloWorld tutorial):
<project name="LWJGL" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="lib.dir" value="lib"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.game.test.MainGameLoop"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<jvmarg value="-Djava.library.path=lib/natives-win" />
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
Every time I run ant on the command line, inside the root folder of my project, I get this:
[java] Error: Could not find or load main class com.game.test.MainGameLoop
[java] Java Result: 1
I'm struggling to understand what is the root problem of this, I kinda sexually abused Google Search trying to find an answer on Java forums, Blogs, and even here...
I don't usually like populating Stack Overflow with noob questions, but I have to admit I don't know what to do.
Thanks in advance!
Your code depends on libraries. You have successfully added the libraries in the classpath to compile your code, and have thus successfully created an executable jar files containing your classes. But these classes depend on libraries to run. And when running the jar file, you don't secify anywhere that Java should look for classes in libraries in addition to your jar file.
See Generate manifest class-path from <classpath> in Ant for how to add a Class-Path entry to the manifest of your executbale jar file. Beware: the paths of the libraries must be relatie to the path of the jar.
Ant is a bit outdated. If I were you, I'd try using gradle, which has a neat application plugin doing all that for you, and much more.
I finally managed to solve this problem that I was struggling with by the help from a good friend of mine. There are two tricky points:
First, when specifying the "Main-Class" in the manifest section, just use the following format "packageName.className", and there is no need to specify the "build" or "source" folders!
Second, try and zip all the jar files that your "jar" file is going to depend on using "zipgroupfileset"
This image shows how I have commented out the "Class-Path" attribute that I would use for addressing the dependencies of the project, and have replaced it with the "zipgroupfileset". I hope that help you as well.
Small thing that can cause this issue is using uppercase letters in file extension. For example you can't use MainGameLoop.Java, The correct extension should be all lowercase like below.
MainGameLoop.java
Please check that first.

JooQ Ant Codegeneration not working

Following the example given at http://www.jooq.org/doc/2.4/manual/META/Configuration/#N10607 on how to run the jooq code-generation I ran into the problem that the build fails with the message:
codegen.xml:7: taskdef class org.jooq.util.GenerationTask cannot be found
For a reference, here is codegen.xml
<project name="..." default="generate-test-classes"
basedir=".">
<property name="path.to.jooq.distribution" value="${basedir}/libs"/>
<property name="path.to.mysql.driver" value="${basedir}/libs"/>
<property name="mysql.driver" value="mysqlcon"/>
<!-- Task definition -->
<taskdef name="generate-classes" classname="org.jooq.util.GenerationTask">
<classpath>
<fileset dir="${path.to.jooq.distribution}">
<include name="jooq.jar" />
<include name="jooqmeta.jar" />
<include name="jooqcodegen.jar" />
</fileset>
<fileset dir="${path.to.mysql.driver}">
<include name="${mysql.driver}.jar" />
</fileset>
</classpath>
</taskdef>
<!-- Run the code generation task -->
<target name="generate-test-classes">
<generate-classes jdbcurl="jdbc:mysql://localhost:3306/crawler"
jdbcuser="..." jdbcpassword="..." generatordatabaseinputschema="..."
generatortargetpackage="model.persistence.jooq"
generatortargetdirectory="${basedir}/src" />
</target>
</project>
I triple checked the definition of the classpath and every file listed exists under the given location. So what would be the problem? Am I missing something? Do I need to configure ant somewhere to recognize the task?
Since I already checked ant: failed to create task or type, I tried to move the taskdef inside the target, but to no avail.
The ant task was an example implementation in jOOQ 2.x. It has been deprecated a long time ago and removed from jOOQ 3.0:
http://www.jooq.org/notes.php?version=3.0
https://github.com/jOOQ/jOOQ/issues/1118
http://www.jooq.org/doc/3.1/manual/code-generation/codegen-configuration/#N12E23
The version of the manual that you've linked is quite outdated.

How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse

I'm working on a small library for our in-company use, and have been heavily documenting it. Now I'm building my jar with the following code:
<project name="commonutils" default="compile" basedir=".">
<property name="src" location="src" />
<property name="build" location="buildDirecotry" />
<target name="compile">
<delete file="${ant.project.name}.jar" />
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" debug="on" target="1.5">
<classpath>
<pathelement location="lib/build/server.zip" />
<pathelement path="${java.class.path}/"/>
</classpath>
</javac>
<jar basedir="${build}" destfile="${ant.project.name}.jar" />
<delete dir="${build}" />
</target>
</project>
Which works fine, it builds my jar file with all the src files in it, but when I include the jar file in another project I no-longer have any of my javadoc comments. Using JDDecompiler I cannot see the comments in the class file, although I'm not sure if its the java compiler that's stripping them or JD.
My question is: How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse.
If you include the source files in the jar (each class and java file in the same package-directory) it should work.
<target name="jar.noCompile.src">
<jar destfile="${ant.project.name}.jar">
<fileset dir="${build}"/>
<fileset dir="${src}" includes="**/*.java"/>
</jar>
</target>
AFAIK the documentation is an Eclipse feature. You have to configure it manually. In your build generate the documentation (usually into folder 'javadoc') and package it with the JAR. Once someone wants to use your library, he/she has to go into Java Build Path select libraries, add yours, click next to it to open the tree node and then double click on Javadoc location to configure it.

Categories

Resources