Using Volley in Xamarin — Part 1 Building the Source

Volley is a useful HTTP library for Android with support for queues, caching and multi-threaded requests and because it is a Google project it gets it’s own section in the official Android documentation.

Volley isn’t available as a .jar from source so you need to jump through a couple of additional hoops to compile and create bindings for it before using it.

First of all, get the source using GIT

> git clone https://github.com/google/volley

To compile it we’re going to use Gradle, you can download it here and remember to add it to your PATH environment variable to make it easy to use.

When you first run gradle against the newly downloaded source you are likely to get the following error

FAILURE: Build failed with an exception.
* Where:
Build file ‘C:\Repos\Volley\volley\build.gradle’ line: 26
* What went wrong:
A problem occurred evaluating root project ‘volley’.
> Failed to apply plugin [id ‘com.android.library’]
> Gradle version 2.2 is required. Current version is 3.3. If using the gradle wrapper, try editing the distributionUrl in C:\Repos\Volley\volley\gradle\wrapper\gradle-wrapper.properties to gradle-2.2-all.zip

Well that’s annoying and you don’t want to go off chasing specific gradle versions so we need to configure the wrapper and rather frustratingly wecan’t configure the wrapper without being able to compile the project, it’s a bit of a catch 22. Instead we’ll have to create an empty project with the wrapper installed.

Configure the Gradle Wrapper

Run the following to get yourself all the wrapper files we need.

> mkdir tmp
> cd tmp
> gradle wrapper --gradle-version 2.2
> xcopy . .. /e
> cd ..
> rd tmp /s/q

You’ll notice there is are a few more files and folders with the name gradle. We can now try and build the project using the gradle wrapper.

> gradlew build

Depending on how your environment is configured you might be able to skip ahead .

Define ANDROID_HOME

FAILURE: Build failed with an exception.
* Where:
Script 'C:\Repos\Volley\volley\bintray.gradle' line: 30
* What went wrong:
problem occurred evaluating script.
SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

It seems that Xamarin doesn’t care if it has this environment variable defined or not but you need it here so you’ll need to create a new one, mine was c:\android-sdk but you might also find it in your c:\Program Files(x86) depending on if you installed it with Visual Studio or not.

With that defined, let’s try again

> gradlew build

The chances are you still got some issues if you are using java 8 to compile.

Fixing the JavaDoc

If you are compiling with java 8 you’ll see a lot of issues, so many in fact that I needed to send them to a file to find them

> gradlew build >> build.log 2>&1

From here we can see the errors and the warnings created in the javadoc

C:\Repos\Volley\volley\src\main\java\com\android\volley\toolbox\BasicNetwork.java:268: error: malformed HTML
* Converts Headers[] to Map<String, String>.
^
C:\Repos\Volley\volley\src\main\java\com\android\volley\toolbox\BasicNetwork.java:268: error: bad use of '>'
* Converts Headers[] to Map<String, String>.
^
C:\Repos\Volley\volley\src\main\java\com\android\volley\VolleyLog.java:40: error: self-closing element not allowed
* <br />
^
C:\Repos\Volley\volley\src\main\java\com\android\volley\VolleyLog.java:42: error: self-closing element not allowed
* <br />
^
C:\Repos\Volley\volley\src\main\java\com\android\volley\VolleyLog.java:28: error: self-closing element not allowed
* <p/>
^
C:\Repos\Volley\volley\src\main\java\com\android\volley\VolleyLog.java:29: error: self-closing element not allowed
* to see Volley logs call:<br/>
^

It seems that java 8 is a lot stricter on what it allows in the javadoc comments so you’ll need to update code. I used Visual Studio Code for my edits but there are plenty of good text editors out there.

Update BasicNetwork.java

267   /**
268 * Converts Headers[] to Map&lt;String, String&gt;.
269 */
270 protected static Map<String, String> convertHeaders(Header[] headers) {

And VolleyLog.java

26    /**
27 * Logging helper class.
28 * <p>
29 * to see Volley logs call:<br>
30 * {@code <android-sdk>/platform-tools/adb shell setprop log.tag.Volley VERBOSE}
31 */
32 public class VolleyLog {
...
37    /**
38 * Customize the log tag for your application, so that other apps
39 * using Volley don't mix their logs with yours.
40 * <br>
41 * Enable the log property for your tag before starting your app:
42 * <br>
43 * {@code adb shell setprop log.tag.&lt;tag&gt;}
44 */
45 public static void setTag(String tag) {

Now your javadoc will build, but your project still won’t.

Fixing the Lint Errors

The final set of issues occur because Volley wants to be part of a bigger project. The linting process attempts to ensure that it has all the permissions it needs to run, unfortunately we just want the .arr to bind to so our build fails.

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...

Add the override to the android section of the build.gradle

32    android {
33 compileSdkVersion 22
34 buildToolsVersion = '22.0.1'
35 lintOptions {
36 abortOnError false
37 }
38 }

Build the Project

Now we can finally build the project successfully

> gradlew build

The shiny new .aar files will be in build\outputs\arr .