Skip to content

Instantly share code, notes, and snippets.

@magmastonealex
Last active July 15, 2022 14:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

mrmaffen commented Mar 25, 2017

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:

03-25 19:10:50.812  30573-30628/? A/libc﹕ Fatal signal 4 (SIGILL), code 1, fault addr 0x7b48330e40 in tid 30628 (ScriptAccount_f)
03-25 19:10:50.812  30573-30627/? A/libc﹕ Fatal signal 4 (SIGILL), code 1, fault addr 0x7b8c32e560 in tid 30627 (ScriptAccount_f)
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ Build fingerprint: 'samsung/heroltexx/herolte:7.0/NRD90M/G930FXXU1DQB3:user/release-keys'
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ Revision: '8'
03-25 19:10:50.940  30656-30656/? A/DEBUG﹕ ABI: 'arm64'
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ pid: 30573, tid: 30628, name: ScriptAccount_f  >>> my.app.up <<<
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr 0x7b48330e40
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x0   0000000000000000  x1   0000007bd784e699  x2   0000000000000000  x3   0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x4   0000007b48330e20  x5   00000000000000c8  x6   0000007bdc8958b0  x7   0000007bee4ee0d0
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x8   0000007bdbbf26a8  x9   0000007bec4f5f68  x10  0000007bd7804139  x11  0000007b483204e1
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x12  0000007b48320540  x13  ffffffffffffffff  x14  00341d0693000000  x15  003b9aca00000000
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x16  0000007bd7804139  x17  0000007b483204e1  x18  0000007bd7fafc8f  x19  0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x20  0000007bd7804139  x21  0000007bd7804139  x22  0000007bd7804139  x23  0000007bd7804139
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x24  0000007bd7804139  x25  0000007bd7804139  x26  0000007bee4edcf8  x27  0000007bd783f051
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ x28  0000007bdbbf23a0  x29  0000007bdbbf23b8  x30  0000007b48320600
03-25 19:10:50.941  30656-30656/? A/DEBUG﹕ sp   0000007bdbbf23a0  pc   0000007b48330e40  pstate 0000000060000000
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ backtrace:
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ #00 pc 000000000002ae40  <anonymous:0000007b48306000>
03-25 19:10:50.943  30656-30656/? A/DEBUG﹕ #01 pc 000000000001a5fc  <anonymous:0000007b48306000>

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!

@mrmaffen
Copy link

I have done another completely clean build but I still get the same error when using my armv8a.so. I'm not very knowledgable when it comes to native coding in c. I'd be very grateful if you could help me out how to debug this issue. Pretty stumped right now :(
Thanks!

@mrmaffen
Copy link

Just tested my aar on an arm64-v8a android virtual device. Works just fine. Something specific about the Galaxy S7 seems to cause the crash. Probably some sort of command that simply isn't available on the S7, which causes the SIGILL. Any ideas?

@theido
Copy link

theido commented Apr 4, 2017

Can I use that to generate all the missing libs (arm64-v8a,mips,mips64,x86_64)?

@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