All Articles

Vagrant build box for torch-android

The first move to get Torch working in Android is to build the native binaries and headers that you can use in your native C/C++ code. Unfortunately building torch-android requires a lot of tedious manual work and it’s error prone. I spent a week on the task, finally after the authors got some issues fixed I had a break through. Judging from the issues in Github I’m one of the very few.

The most common issue reported in the Github repo today is related to building on systems with the newest versions of cmake, the build mysteriously just fails. There are other OS/platform specific errors as well, such errors are hard to address and subject to great variation. Therefore I created a Vagrant Box that automatically builds torch-android for ARMv7 and ARMv8. You might as well grab a cup of coffee and let the build box do the heavy lifting.

For heavy lifters, the manual process is documented below as well. Performing the ~30 commands/actions and waiting will easily take a couple of hours so I wouldn’t recommend it, but in case Torch breaks it might be a good source of information.

Build it!

Github: paramsen/torch-android-build-box

Prerequisite; Install Vagrant and VirtualBox.

The Vagrant Box creates an Ubuntu 14.04 64bit virtual machine, provisions it with Java8, cmake 3.4.0, Android SDK/NDK and sets needed env vars. After provision it builds Torchv7 and finally torch-android for ARMv7 and ARMv8. Finally it copies the built binaries and headers to the /mounted directory that’s shared with the host as ./mounted.

Let’s get started:

  1. git clone https://github.com/paramsen/torch-android-build-box.git
  2. cd torch-android-build-box
    • Protip; vim Vagrantfile, increase RAM/CPUs as much as your computer can afford
    • Protip 2; vim Vagrantfile, update to the newest NDK if it isn’t up to date
  3. vagrant up -> Vagrant builds torch-android (expect it to take 30min-2hrs depending on the available computation power)
  4. ls mounted -> Verify that the build completed successfully, mounted will contain the following directories each containing built files
    mounted
    ├── arm64-v8a
    ├── armeabi-v7a
    ├── headers
    └── lua
    
  5. vagrant halt -> Stop the VM
  6. vagrant destroy -f -> Delete the VM from disk, keeps the built stuff in ./mounted
  7. You’re ready to proceed to the next article and use the built binaries in your Android Studio project

Please report build issues in the Vagrant build box Github repo
If you wish to contribute or update the Vagrant build box, don’t hesitate to pull request

Building torch-android with CUDA

I will create a separate article for this, CUDA requires some manual work.

Building torch-android manually

I encourage you to check out my Vagrantfile. If you read past the fancy VM stuff, Vagrant is just executing UNIX commands in Bash; First for provisioning, then building. It should be self-explanatory, if not just ask. The real power of my Vagrant build box is that the platform and dependencies have been verified to work when building torch-android.

Next up

With the binaries, headers and everything built we can integrate torch-android into our Android Studio project. This will be covered in the next article.

Published 15 Apr 2017