The hardest technical challenge we faced while building J2V8 was not bridging the different technologies, but rather picking the right native bits at runtime. In order to support a variety of platforms (including 64 and 32 bit Windows, Linux, MacOS, and several different Android architectures) we developed a library loader that would pick the proper native library based on current architecture. The correct library would be unpacked and dynamically loaded. We developed this to make consuming J2V8 much easier, but we quickly learned that the road to hell is paved with good intentions.
The System.loadLibrary() method on Android will load a library from a number of predefined locations, however, none of these locations are writeable at runtime. The System.load() method, which allows you to load a library given an absolute path, does not work in many cases. To solve this, consumers of J2V8 needed to manually extract the native libraries from our jar and include them in the correct jniLibs directory within their own apk. This was complicated, error prone and would often result in duplicate copies of the same native library in your final apk.
To solve this problem, we’ve changed the way J2V8 is packaged for Android. Instead of shipping J2V8 as a jar with a smart library loader, we now ship J2V8 as an aar. And aar is a binary distribution of an Android Library Project. An aar file can contain both a jar as well as resources. In our case, j2v8-3.1.0-SNAPSHOT.aar contains the Java bindings as well as pre-built native bits for Android ARM and x86. If you consume this library in your build, the native bits will be assembled into the right locations at build time, making the library loading much easier.
To use J2V8 in your Android application, simply add the following to your build.gradle file.
This is currently available in 3.1.0-SNAPSHOT. You will need to consume this from the maven snapshot repository https://oss.sonatype.org/content/repositories/snapshots. You can also consume it in Maven projects with the following pom dependency:
With this change, there is no need to provide a location to extract the native libraries when creating the V8Runtime.
A special thanks goes to my colleague Moritz Post who did the hard work of creating the gradle build for J2V8.
If you’re working with J2V8 on Android, please give this new format a try and let me know if it works. If things are working well, I’ll publish a 3.1.0 final.