Skip to content

Instantly share code, notes, and snippets.

@magmastonealex
Last active July 15, 2022 14:53
Show Gist options
  • Save magmastonealex/a9158173828932def9ffb81672d6c578 to your computer and use it in GitHub Desktop.
Save magmastonealex/a9158173828932def9ffb81672d6c578 to your computer and use it in GitHub Desktop.
Building J2V8 for Android

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 the LOCAL_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
  • Append -fPIC to the LOCAL_CFLAGS variable at the bottom of the file.

  • Edit jni/Application.mk, appending x86_64 and arm64-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 to libj2v8.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.

@mrmaffen
Copy link

I moved my issue here: eclipsesource/J2V8#240

@SKrPl
Copy link

SKrPl commented Jun 15, 2017

How do I include NodeJS for android?

@eranbet
Copy link

eranbet commented Oct 17, 2017

Hi,
I've been trying this guide out but getting the following error for arm (getting other but similar errors for arm64) during build:

In file included from /usr/include/limits.h:25:
/usr/include/features.h:374:12: fatal error: 'sys/cdefs.h' file not found

Did any of you encounter something similar?

I'm trying it on Ubuntu 14.04 (3.13.0-119-generic x86_64)

Thanks

EDIT: installing libc6-dev-i386 solved this one but now I get this: ../testing/gmock/include/gmock/gmock-cardinalities.h:42:10: fatal error: 'ostream' file not found

EDIT 2: installing libc++-dev solved that one.

EDIT 3: Build was successful and everything seems to be working.

@yuzhidi
Copy link

yuzhidi commented Mar 28, 2018

Does this page mean we can build v8 for arm64?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment