How to build Android SDK samples using Gradle?

First, my environment is Ubuntu 14.04 LTS, and I would like to compile Android SDK samples. The present write-up is a follow-up to this:

where compilation is done using the traditional “ant”, at present is a version using “gradle”.

First go to the Android sample directory and issue the compilation command:

cd /opt/android-sdk-linux/samples/android-23/testing/ActivityInstrumentation

./gradlew build

Got error, and so I created a file (as requested by error) called “” with the following content (just one liner):


And recompile, and got error again.

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ‘:Application:compileDebugJava’.
> invalid source release: 1.7

Meaningless error message, but a intuition just arrive, checking java version:

java -version
java version “1.6.0_45”
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

Possibly this version is not right, though it is the right version (in the past, not sure now?) for compiling AOSP. And anyway, after updating to java7:

export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH

(where the above “java-7-openjdk” comes from is via “sudo apt-get install openjdk-7-jdk”.)

now the entire sample builds to completion without error:

./gradlew build
:Application:preBuild UP-TO-DATE
:Application:preDebugBuild UP-TO-DATE
:Application:preReleaseBuild UP-TO-DATE
:Application:compileReleaseUnitTestSources UP-TO-DATE


Of course, if you want to debug what are the root cause of error, another way is to generate verbose output with the following command:

./gradlew build –stacktrace –info –debug

Example 2

First cd to directory:

cd /opt/android_sdk/samples/android-22/renderscript/BasicRenderScript

Issue the command (after ensuring that “gradlew” command exists):

“./gradlew build”

:Application:compileDebugNdk UP-TO-DATE
:Application:assembleRelease UP-TO-DATE
:Application:assemble UP-TO-DATE
Ran lint on variant debug: 49 issues found
Ran lint on variant release: 49 issues found
Wrote HTML report to file:/sda7/android_sdk/samples/android-22/renderscript/BasicRenderScript/Application/build/outputs/lint-results.html
Wrote XML report to /sda7/android_sdk/samples/android-22/renderscript/BasicRenderScript/Application/build/outputs/lint-results.xml


Total time: 19.919 secs

In between above there were some errors, corrections of which are shown below:

In the file “/opt/android_sdk/samples/android-22/renderscript/BasicRenderScript/Application/build.gradle”:

39 lintOptions {
40 abortOnError false
41 }

was inserted inside the android {} tag:

35 android {
36 compileSdkVersion 21
37 buildToolsVersion "21.0.1"
39 lintOptions {
40 abortOnError false
41 }
43 defaultConfig {
44 minSdkVersion 8
45 targetSdkVersion 20
46 }

But many times, the lint errors is not supposed to be ignored by 
adding "abortOnError false" inside build.gradle file.   For example is the error below:

ls -1 /sda7/android_sdk/samples/android-22/renderscript/BasicRenderScript/Application/src/main/res/values/


There is a bug in base-strings.xml, just ensure that there are no aprostrophe inside the CDATA tags, or specifically the following (aprotrophe has been removed):

20 <![CDATA[
23             This sample demonstrates using RenderScript to perform basic image manipulation. Specifically, it allows users
24             to dynamically adjust the saturation for an image using a slider. A custom RenderScript kernel performs the saturation
25             adjustment, running the computation on the device GPU or other compute hardware as deemed appropriate by the system.
28         ]]>

Using the above method I was able to compile successfully 84 of the samples under the “Android-22” directory in “samples”, and 19 failures, which mostly are attributable to lint error:



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: