You must do this on a Linux system. Mac V8 builds (at least for Android) are broken, and the build processs generates thin archives with absolute paths. So, until the J2V8 compile is done and has produced an AAR, you'll need to stay on the same machine. The AAR, however, is portable everywhere.
These directions are the result of a lot of trial-and-error, and were written for an Arch system, but there's no reason it wouldn't work on any distro.
Note that you must be careful to reconstruct your environment if you exit your shell session!
Building V8:
mkdir v8-builder
cd v8-builder
- Follow https://www.chromium.org/developers/how-tos/install-depot-tools
fetch v8
cd v8
git checkout 4.10.253
gclient sync
echo "target_os = ['android']" >> ../.gclient && gclient sync --nohooks
- the above will take quite a while - it downloads the NDK and other Android tools using Git.
export CPPFLAGS="-fPIC"
export CCFLAGS="-fPIC"
export CPPFLAGS_host="-stdlib=libc++"
- This uses LLVM's c++ lib for the host. Note that these libs and executables are never used. Just newed to be compiled for the build to succeed. Make sure it's installed! (libc++ on Arch)
make android_arm.release -j4 i18nsupport=off snapshot=off
make android_arm64.release -j4 i18nsupport=off snapshot=off
make android_ia32.release -j4 i18nsupport=off snapshot=off arm_version=default
make android_x64.release -j4 i18nsupport=off snapshot=off arm_version=default
cd out
ln -s android_arm.release android_armeabi-v7a.release
ln -s android_arm.release android_armeabi.release
ln -s android_ia32.release android_x86.release
ln -s android_x64.release android_x86_64.release
ln -s android_arm64.release android_arm64-v8a.release
- J2V8's ndk build expects this naming.
Building Android Archive:
-
At this point you will need to install the Android SDK, if it's not on your computer already. You can use basically any build tools and SDK version, I used API 22 and build tools 22.0.1. Keep track of those versions though. It can be different from your final target project.
-
Checkout J2V8.
-
export PATH=$PATH:/path/to/v8-builder/v8/third_party/android_tools/ndk
- Building with another NDK will cause weird errors, just use V8's known-working version.
-
Edit
jni/Android.mk
, updating theLOCAL_SRC_FILES
to replace all/data/jenkins/
instances with the path to your main v8 folder (v8-builder/v8/
).- E.G:
/data/jenkins/v8/include
->/path/to/v8-builder/v8/include
- E.G:
-
Append
-fPIC
to the LOCAL_CFLAGS variable at the bottom of the file. -
Edit
jni/Application.mk
, appendingx86_64
andarm64-v8a
to the archetectures list. -
run
ndk-build
-
Edit build.gradle, updating the SDK version, build tools, and adding:
sourceSets.main.jni.srcDirs= [] //Disable automatic ndk-build.
sourceSets.main.jniLibs.srcDir 'libs'
to the android{} block of the file.
cd libs
- In each subfolder here, rename the
.so
file tolibj2v8.so
- The Java code just expects the name to be j2v8, no regard to archetecture. Android will handle finding the right ABI.
- Or
perl-rename -v 's/libj2v8_android_[a-z-_0-9A-Z]*.so/libj2v8.so/' **/*
- Last step!
./gradlew assembleRelease
- Now, build/outputs/aar should have
j2v8-release.aar
! This can be used in any project, and used on any platform (not just this Linux machine.)
I'm not sure if the licences of J2V8 and V8 allow me to post my compiled AAR file, otherwise I would do that as a temporary measure until the official builds are updated.
These instructions worked for me, and I have not extensively tested the compiled version of V8 or J2V8. My needs within the application are simple, and only require simple JS constructs.
Thanks for the guide! Helped me a lot. I successfully built an aar. When I install an armv7a version on my Galaxy S7 it works fine, however the whole point of this endeavour was to get a working arm64-v8a version. When I try installing the arm64-v8a version I get a SIGILL:
Any ideas on why that might be the case? :( Been searching for many hours for a solution but I couldn't find any unfortunately
Thanks for your time!