Findbugs for Android project with libraries in ant build - android

Findbugs is a great software and my team uses it while working on our Android project. In Eclipse everything is nice and shiny, however now we're trying to automate our builds with ant and generate Findbugs results automatically for each build.
It's seems not that difficult. I followed this tutorial:
https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project#BuildinganAndroidappandtestproject-FindBugs
One minor issue is that I had to change ${android.jar} to ${project.target.android.jar}.
The worse part are The following classes needed for analysis were missing: warnings for classes that come form library projects we use. Some of them are our own and we would like to scan them with Findbugs, too. To make matters more complicated, one of these libs uses another lib (also our own and needs scanning), so it looks like this:
Project A --uses--> Library B --uses--> Library C
Here I thought that since Android SDK can handle all these dependencies (Library C is compiled when I issue ant debug for Project A), I can somehow make use of it, get the list of libs my project depends on and provide it to the findbugs task. Unfortunately I haven't managed to do that.
For now I settled with manually entering all libs, some into class property of findbugs task, some into auxClasspath, which has the upside that I can only analyse some of the libraries the project depends on. Still I hope that what I originally tried to do is possible. Can anybody show me how to extract a path-element

I've finally found that part of SDK build script that puts together a "classpath" of all the jars (classes.jar-s of all the libraries, including Library C from the "diagram" and jars from the libs/ folder of the project and all the libs).
My final custom-rules.xml with findbugs target looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Project_custom" default="findbugs">
<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"/>
<target name="findbugs">
<gettarget
androidJarFileOut="project.target.android.jar"
androidAidlFileOut="project.target.framework.aidl"
bootClassPathOut="project.target.class.path"
targetApiOut="project.target.apilevel"
minSdkVersionOut="project.minSdkVersion" />
<dependency
libraryFolderPathOut="project.library.folder.path"
libraryPackagesOut="project.library.packages"
libraryManifestFilePathOut="project.library.manifest.file.path"
libraryResFolderPathOut="project.library.res.folder.path"
libraryBinAidlFolderPathOut="project.library.bin.aidl.folder.path"
libraryNativeFolderPathOut="project.library.native.folder.path"
jarLibraryPathOut="project.all.jars.path"
targetApi="${project.target.apilevel}"
verbose="${verbose}" />
<findbugs home="${findbugs.home}" output="xml" outputFile="findbugs-results.xml">
<auxClasspath>
<pathelement location="${project.target.android.jar}" />
<path refid="project.all.jars.path" />
</auxClasspath>
<class location="${out.dir}" />
</findbugs>
</target>
</project>
Edit: I've upgraded the target to run indepent of the build targets, i.e. now you can run just ant findbugs not ant debug findbugs.

I added some extra definition and worked. Thank you
<target name="findbugs">
<mkdir dir="reports" />
<gettarget
androidJarFileOut="project.target.android.jar"
androidAidlFileOut="project.target.framework.aidl"
bootClassPathOut="project.target.class.path"
targetApiOut="project.target.apilevel"
minSdkVersionOut="project.minSdkVersion" />
<dependency
libraryFolderPathOut="project.library.folder.path"
libraryPackagesOut="project.library.packages"
libraryManifestFilePathOut="project.library.manifest.file.path"
libraryResFolderPathOut="project.library.res.folder.path"
libraryBinAidlFolderPathOut="project.library.bin.aidl.folder.path"
libraryNativeFolderPathOut="project.library.native.folder.path"
jarLibraryPathOut="project.all.jars.path"
libraryRFilePathOut="project.library.rfile.path"
buildToolsFolder="${sdk.dir}/build-tools"
renderscriptSupportLibsOut="project.rs.support.libs.path"
renderscriptSupportMode="${renderscript.support.mode}"
targetApi="${project.target.apilevel}"
verbose="${verbose}" />
<findbugs home="${findbugs.home}" output="xml:withMessages" outputFile="reports/findbugs.xml" excludeFilter="findbugs-exclude.xml" maxRank="9">
<!-- auxClasspath path="${project.target.android.jar}" / -->
<auxClasspath>
<pathelement location="${project.target.android.jar}" />
<path refid="project.all.jars.path" />
</auxClasspath>
<sourcePath path="${basedir}/src/" />
<class location="${basedir}/bin/classes/" />
</findbugs>
</target>

Related

How does gradle execute an ant build file

Ive been driving myself crazy trying to rebuild my custom_rules.xml into something in gradle and its proving to be quite difficult. So my next step is Im trying to just import the last few things I cant do in gradle as an build.xml.
However this doesnt seem to do anything. When I try using
ant.importBuild 'build.xml'
I get no feed back or no echos from my script. Ive read through gradles documentation a lot especially on ant and for the life of me I cant figure out what Im supposed to do once the build gets imported. How does the script get executed?
This is my build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="postPackage" >
<property name="config_path" location="${cert.dir}" />
<property name="out.pre.widevine.signed.file" location="release-pre-widevine-sign.apk" />
<property name="out.widevine.signed.file" location="release-widevine-signed.apk" />
<echo>sign with widevine certificate</echo>
<touch file="res/raw/wv.properties"/>
<copy file="${out.packaged.file}" tofile="${out.pre.widevine.signed.file}"/>
<java jar="apksigtool.jar" failonerror="true" fork="true">
<arg value="${out.packaged.file}"/>
<arg value="private_key.der" />
<arg value="my.crt" />
</java>
<copy file="${out.packaged.file}" tofile="${out.widevine.signed.file}"/>
</target>
</project>
I was able to accomplish this by using the following snippet in a method
ant.importBuild 'build.xml'
postPackage.doFirst{println("Im starting")}
postPackage.execute()
ant.importBuild will create an equally named Gradle task for each Ant target found in the Ant build. You can then invoke those tasks from the command line and/or make other tasks depend on them. For more information, see "16.2. Importing an Ant build" in the Gradle User Guide,
Hmm I think this Kotlin is working for me;
val t : Task = tasks.named("war").get()
t.actions.forEach { a -> a.execute(t) }

Embed external libraries to Android build.xml

I want to build an Android application using ant (as generated by the android tool). The application needs external libraries that are available as .jar. Since other (related) projects need the same libraries, I want to store them in a common place, outside of /libs (let's say "../somewhere/else/something.jar"). The application additionally needs a shared object, which is stored in /libs/armeabi/libSomething.so (it is copied there by ndk-build).
Does anybody know how I must configure my stuff to embed the classes in ../somewhere/else/something.jar into my .APK?
I tried various ways in ant.properties
...
java.compiler.classpath=../somewhere/else/something.jar
jar.libs.dir=libs:../somewhere/else
external.libs.dir=libs:../../ext/android/tomtom
and build.xml
...
<package-helper>
<extra-jars>
<jarfile path="../somewhere/else/something.jar" />
</extra-jars>
</package-helper>
...
<!-- From http://stackoverflow.com/questions/3217643/how-to-add-external-jar-libraries-to-an-android-project-from-the-command-line -->
<target name="-pre-compile">
<!-- HACK to add the android-support-v4.jar to the classpath directly from the SDK -->
<echo>ORIGINAL jars.path : ${toString:project.all.jars.path}</echo>
<path id="project.all.jars.path.hacked">
<path path="${toString:project.all.jars.path}"/>
<!-- <path path="${sdk.dir}/extras/android/support/v4/android-support-v4.jar"/> -->
</path>
<path id="project.all.jars.path">
<path path="${toString:project.all.jars.path.hacked}"/>
</path>
<echo>HACKED jars.path : ${toString:project.all.jars.path}</echo>
None of the above mentioned ways worked... :(
Does anybody have a solution? I am searching and trying now for hours, and I can't imagine that nobody ever had the same problem.
Thanks for any help!!!

javac error "error: warnings found and -Werror specified" disable "-Werror" in ant

this is an android project and my ant build script sometimes fails when it decides to treat warnings as errors when running the javac program. Seriously, it only does this sometimes, which is a different question I may ask.
It will print errors and abruptly cancel the build
[javac] 1 error
[javac] 9 warnings
as I did deeper I see the "error" is
error: warnings found and -Werror specified
which is not anything I explicitly set. Now this may be an argument buried deep in the build.xml file, or maybe in this particular sub library's build.xml file in one particular condition I don't currently know about
It is the android facebook sdk that causes this, sometimes. But there is no Werror argument within the ant build.xml files but I want to disable it or work around it
This is for a build server, where I have other conditions to stop a build. Inconsistent ant and javac issues don't really have a place.
but any insight about it is appreciated.
The file "tools/ant/build.xml" under my Android SDK directory contains the following:
<property name="java.compilerargs" value="" />
Perhaps the Android SDK used by the build that fails due to warnings being treated like errors includes "-Werror" in the compiler args? (If not, a recursive grep for "compilerargs" in the directory of the offending Android SDK instance could find the culprit.)
UPDATE:
On the other hand, that's in my Android SDK, the property is not in itself mandatory -- it just happens to be used here:
<!-- 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}" />
</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>
The element that has to be there is the "compilerarg" one on the next-to-last line, so a grep for "compilerarg" instead of "compilerargs" would be the better choice.

Generating android code coverage though changes in build.xml and ant.properties

I have been trying to generate android code coverage for my android test project which tests a android project which includes a external jar. When I run command
ant emma install debug test
it shows coverage of only the android project but does not include the package and functions of the external jar included.
As per my researches I have found that some changes should be done in ant.properties file and emma report tag in build.xml.
Any suggestions are most welcome.
[Edit]
On furthur study, I went through a blog which says enter the variable
tested.android.library.source.dir="path to external jars" in ant.properties
and same variable should be added in report sourcepath as
<emma>
<report sourcepath="${tested.project.source.absolute.dir}:${tested.project.lib.source.path.value}:${tested.android.library.source.dir}" verbosity="${verbosity}">
<!-- TODO: report.dir or something like should be introduced if necessary -->
<infileset file="${out.absolute.dir}/coverage.ec" />
<infileset file="${tested.project.out.absolute.dir}/coverage.em" />
<!-- TODO: reports in other, indicated by user formats -->
<html outfile="${out.absolute.dir}/coverage.html" />
</report>
</emma>
But still the coverage is not showing functions and package of External jar.
I wanted to know that my jars are not in the android test project but in the android project which is tested. So how to give path to those external jars in ant.properties of the test project.
I am also working on the same from quite some time.
Refer to code coverage reports for library projects
External jars coverage support is in ADT-r20.
I can only point to the direction I am working.
I am working on modifying build.xml inside ant :
<!-- This is macro that enable passing variable list of external jar files to ApkBuilder
Example of use:
<package-helper>
<extra-jars>
<jarfolder path="my_jars" />
<jarfile path="foo/bar.jar" />
<jarfolder path="your_jars" />
</extra-jars>
</package-helper> -->
<macrodef name="package-helper">
<element name="extra-jars" optional="yes" />
<sequential>
<apkbuilder
outfolder="${out.absolute.dir}"
resourcefile="${resource.package.file.name}"
apkfilepath="${out.packaged.file}"
debugpackaging="${build.is.packaging.debug}"
debugsigning="${build.is.signing.debug}"
verbose="${verbose}"
hascode="${manifest.hasCode}"
previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}"
buildType="${build.is.packaging.debug}/${build.is.signing.debug}">
<dex path="${intermediate.dex.file}"/>
<sourcefolder path="${source.absolute.dir}"/>
<jarfile refid="project.all.jars.path" />
<nativefolder path="${native.libs.absolute.dir}" />
<nativefolder refid="project.library.native.folder.path" />
<extra-jars/>
</apkbuilder>
</sequential>
</macrodef>
I have no luck till yet.
After lots of searching and trying , finally got the package of my External jar in my Main project.
Just entered tested.android.library.source.dir in ant.properties and build.xml
ant.properties: tested.android.library.source.dir="Path to libs folder of main project where jars are present"
Build.xml : Under emma tag in report tag add this variable seperated by colon.
Now go to command prompt and run
In Main Project :android update project -p .
In Test Project: android update test-project -m "Path of main project" -p .
Now copy test target from build.xml (sdk/tools/ant/build.xml) and paste it in build.xml of test project above the line
<import file="${sdk.dir}/tools/ant/build.xml" />
Dont Forget to Change Version Tag to
Now Again open command prompt and run:
In Main Project: ant emma debug install
In Test Project: ant emma debug install test
Your Code coverage report generated will contain the package of the external jar
I struggled with this for 2 days and eventually figured it out. The code snippet in the first post only generates instrument report from emma metadatafile and runtime coverage file but does not perform instrumentation on jar. To instrument the code you will have to manipulate the byte code, like how android ant build.xml target does it. Take a look at the element nested in <-compile> and you will see comment like it is only instrumenting class files. To instrument the jars add a classpath like element to the jars along with path to class.
Emma Documentation: http://emma.sourceforge.net/reference/ch02s03s02.html
<emma enabled="${emma.enabled}" >
<instr mode="fullcopy"
outdir="${out.instr.dir}"
merge="no"
filter="${emma.filter}">
<instrpath>
<fileset dir="${out.dir}" includes="**/*.jar" />
</instrpath>
</instr>
</emma>
In Android's build.xml. One just have to delete instrpath attribute and make it look like the example above.
756 <emma enabled="true">
757 <instr verbosity="${verbosity}"
758 mode="overwrite"
759 instrpath="${out.absolute.dir}/classes"
760 outdir="${out.absolute.dir}/classes"
761 metadatafile="${emma.coverage.absolute.file}">
762 <filter excludes="${emma.default.filter}" />
763 <filter value="${emma.filter}" />
764 </instr>
765 </emma>
So something like this: (pardon the line#s)
756 <emma enabled="true">
757 <instr verbosity="${verbosity}"
758 mode="overwrite"
760 outdir="${out.absolute.dir}/classes"
761 metadatafile="${emma.coverage.absolute.file}">
759 <instrpath>
759 <pathelement path="${out.absolute.dir}/classes"/>
759 <fileset dir="${you-class-path}"/>
759 <include name="**/*.jar"/>
759 </fileset>
759 </instrpath>
762 <filter excludes="${emma.default.filter}" />
763 <filter value="${emma.filter}" />
764 </instr>
765 </emma>

Android - How do I dynamically set the package name at build time for an Open-source project?

I'm working on an Open-source project. As it is intended that anyone can download the source and build it themselves, I do not want to hard-code the package name anywhere - including the directory structure.
I use ant for building. Apparently I can modify build.xml, but I believe this is overwritten by android update. Whatever is used will be committed to the Git repo, and it should not be too complicated.
Currently the process to build the code straight from the Git repo is fairly simple. Here's an excerpt from the README file:
$ cd ~/src/isokeys/IsoKeys
$ android list targets # I build against API level 10.
$ android update project --name IsoKeys --target 1 --path ./ # Only needed first time.
$ ant debug && adb -d install -r bin/IsoKeys-debug.apk
To me, it makes sense to put the package name in local.properties, because this is .gitignore'd. As the package name won't be anywhere else, the build will fail without doing this. So there needs to be at least 1 extra step in the README, but I want to keep it to a minimum.
Edit: Of course, another requirement is that diffs make sense - which they don't if you manually rename the package name.
I did something similar (but not for this reason) which required updating the manifest at build time. The way I accomplished this was by making a second AndroidManifest and putting it under a directory named config.
So in config/AndroidManifest you could have something like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="#CONFIG.APP_PACKAGE_NAME#"
android:versionCode="#CONFIG.APP_VERSION_CODE#"
android:versionName="#CONFIG.APP_VERSION#">
<!-- EVERYTHING ELSE GOES HERE -->
</manifest>
Then you can use the regular bare bones build.xml ant script with just a few modifications (no need to copy the whole script from the android build system as they added some hooks for you to use without reinventing the wheel).
The build script should be reading local.properties by default, but if not add (or uncomment) a line like this:
<property file="local.properties" />
In your build script you should see a task called "-pre-build", change it like this:
<target name="-pre-build">
<copy file="config/AndroidManifest.xml" todir="." overwrite="true" encoding="utf-8">
<filterset>
<filter token="CONFIG.APP_PACKAGE_NAME" value="${app.packagename}" />
<filter token="CONFIG.APP_VERSION" value="${app.version}" />
<filter token="CONFIG.APP_VERSION_CODE" value="${app.versioncode}" />
</filterset>
</copy>
</target>
Then your local.properties file you would put the package name, version name/code like so:
app.version=1.0
app.versioncode=1
app.packagename=com.mypackage.name
Now you just need to make sure in your manifest that you fully qualify all of your activities/services/broadcast listeners etc.. That means you always specify the full package of your source code. If you want the package for your own source code to be dynamic you could replace out each of the prefixes to each class.. But that seems kind of silly.. It is easy enough to package your code up under your own package name and they can use it from any project by simply including the source or a jar in their project.
-- UPDATE --
Oh and one other thing you can do to notify the user that they must define a package name is use the fail tag in your build xml like this:
<fail message="app.packagename is missing. This must be defined in your local.properties file" unless="app.packagename" />
Put this after the line which reads the local.properties file
With thanks to Matt Wolfe for his help, I'm posting a partial answer with my efforts so far.
I noticed that the default barebones build.xml would also import custom_rules.xml:
<import file="custom_rules.xml" optional="true" />
So I created this file and started tinkering. This is what I have come up with so far:
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" default="debug">
<target name="-pre-build">
<fail message="Please define app.packagename in your local.properties file." unless="app.packagename" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="/usr/share/java/ant-contrib.jar"/>
</classpath>
</taskdef>
<!-- How do I check for propertyregex?
<fail message="Depends on ant-contrib's propertyregex for app.packagename.path." unless="propertyregex" />
-->
<propertyregex property="app.packagename.path"
input="${app.packagename}/"
regexp="\."
replace="/"
global="true"
/>
<copy todir="build/" overwrite="true" encoding="utf-8">
<fileset dir="./">
<include name="AndroidManifest.xml" />
<include name="res/**" />
<include name="lib/**" />
</fileset>
<filterset>
<filter token="CONFIG.APP_PACKAGE_NAME" value="${app.packagename}" />
</filterset>
</copy>
<copy todir="build/src/${app.packagename.path}" overwrite="true" encoding="utf-8">
<fileset dir="./src/isokeys/">
<include name="**" />
</fileset>
<filterset>
<filter token="CONFIG.APP_PACKAGE_NAME" value="${app.packagename}" />
</filterset>
</copy>
</target>
<target name="-pre-clean" description="Removes output files created by -pre-build.">
<delete file="build/AndroidManifest.xml" verbose="${verbose}" />
<delete dir="build/res/" verbose="${verbose}" />
<delete dir="build/lib/" verbose="${verbose}" />
<delete dir="build/src/" verbose="${verbose}" />
</target>
<!-- NOW CHANGE DIRECTORY TO build/ BEFORE HANDING BACK OVER TO build.xml!!! -->
</project>
This sets everything up in build/ (which has the added bonus of keeping things neat and tidy), now the intention is for the sdk tools build.xml to run from this build/ directory. However, I can't find any way of cd'ing.
Easiest way might be replace the package name as late as possible. This way, you don't even have to touch your code. There is a nice article named Renaming the Android Manifest package(http://www.piwai.info/renaming-android-manifest-package/). Summary:
You can use aapt --rename-manifest-package to modify the package name
Alternatively, if you want package name replacement to be a part of the ant build process, you can override the -package-resources target:
copy the -package-resources target from SDK's build.xml
add manifestpackage parameter

Categories

Resources