Quick guide on how to compile v8 8.4-lkgr for Android on Ubuntu in one copy/paste script. I use this script regularly, so it is well tested.
If you are looking for some already pre-compiled ready-to-use v8 version, you can find them here.
# Install git: sudo apt install git # Install depot tools Follow instructions https://v8.dev/docs/source-code. # Fetch v8 source code. # Use the branch of your choice. In will use 8.4-lkgr (last known good release). # I'd advise always using -lkgr branches fetch v8 cd v8 git pull git checkout 8.4-lkgr # Enter v8 folder. cd v8 # Install all dependencies, ndk, sdk, etc. # This may take a while. It downloads android tools: sdk+ndk, etc. build/install-build-deps-android.sh # Set android target. This op will take some time, ndk alone is +1Gb echo "target_os = ['android']" >> ../.gclient && gclient sync # Generate compilation target: # Change android_arm.release to the folder name of your choice, in this # case: out.gn/android_arm.release # # Use this to compile for arm/arm64 tools/dev/v8gen.py arm.release # Use this to compile for x86 tools/dev/v8gen.py ia32.release # Edit gn configuration file: # I’d recommend disabling icu support, and set # symbol_level=0 for faster compilation and thinner # output libs. You can get the whole list of # compilation options by executing: # `gn args out.gn/android_arm.release —-list` # # Optionally set `target_cpu="arm64"` or `target_cpu="x86"` (if ia32 was used) vi out.gn/android_arm.release/args.gn # This is my file contents: android_unstripped_runtime_outputs = false is_component_build = false is_debug = false symbol_level = 1 target_cpu = "arm" target_os = "android" use_goma = false use_custom_libcxx = false use_custom_libcxx_for_host = false v8_target_cpu = "arm" v8_use_external_startup_data = false v8_enable_i18n_support= false v8_android_log_stdout = true v8_static_library = true v8_monolithic = true v8_enable_pointer_compression = false # to compile arm64, just change target_cpu and v8_target_cpu to arm64 # Compile target: # This may take up to 1 hour depending on your setup. # Optionally use a -j value suitable for your system. ninja -C out.gn/android_arm.release v8_monolithic # Fat lib file has been generated by v8_monolithic parameter at # out.gn/<e.g. android_arm.release>/obj/libv8_monolithic.a # source headers, for inspector compilation. mkdir -p src/base/platform mkdir -p src/common mkdir -p src/inspector mkdir -p src/json mkdir -p src/utils mkdir -p src/init cp -R ../../../../src/common/*.h ./src/common cp -R ../../../../src/base/*.h ./src/base cp -R ../../../../src/base/platform/*.h ./src/base/platform cp -R ../../../../src/inspector/*.h ./src/inspector cp -R ../../../../src/json/*.h ./src/json cp -R ../../../../src/utils/*.h ./src/utils cp -R ../../../../src/init/*.h ./src/init # copy v8 compilation header files: cp -R ../../../../include ./ # For compilation on Android, always use the same ndk as # `gclient sync` downloaded. # Enjoy v8 embedded in an Android app
Compile for Android emulator
tools/dev/v8gen.py gen ia32.release # edit out.gn/ia32.release/args.gn, to contain the following: is_debug = false target_cpu = "x86" use_goma = false target_os = "android" v8_use_external_startup_data = false v8_enable_i18n_support = false v8_monolithic = true
Hi
I needed to built v8 with NDK version 21 which was different than what gclient sync downloaded. So i changed the ndk paths used by v8 to refer to NDK version 21 paths. After building for arm64 and x86_64 and using libv8_monolith.a files in android studio, the project runs successfully.
But for 32-bit devices ( i.e. arm and x86), though the build is successful, the android project crashes. I am not able to find any specific argument that might be needed for building v8 for 32-bit devices. Could you please suggest where i might be wrong?
I have used the following arguments to build v8 for arm:
target_os = “android”
is_debug = true
is_component_build = false
target_cpu = “arm”
v8_target_cpu = “arm”
v8_enable_pointer_compression = false
clang_use_chrome_plugins = false
v8_monolithic=true
v8_use_external_startup_data=false
symbol_level=2
v8_enable_i18n_support=false
LikeLike
That config file looks correct to me.
Since you have debug+symbol_level, you should be able to get an accurate grasp of when things derail.
For x86, you need to set `target_cpu = “x86″` (simulator). But the arm should work straight away. Maybe you can share some stack traces.
LikeLike
Hi,
I am trying to build V8 for the android x86 arch, I have followed the above-shared steps but am getting the following error while integrating with the android sample app.
[x86] SharedLibrary : libhello-jni.so
D:/workspace/v8workspace/SampleV8v8.x/app/src/main/cpp/hello-jni.cpp:42: error: undefined reference to ‘v8::platform::NewDefaultPlatform(int, v8::platform::IdleTaskSupport, v
8::platform::InProcessStackDumping, std::__ndk1::unique_ptr<v8::TracingController, std::__ndk1::default_delete >)’
../../buildtools/third_party/libc++/trunk/include/vector:1027: error: undefined reference to ‘std::__1::__vector_base_common::__throw_length_error() const’
../../src/api/api.cc:0: error: undefined reference to ‘std::__1::__vector_base_common::__throw_length_error() const’
../../buildtools/third_party/libc++/trunk/include/memory:3498: error: undefined reference to ‘std::__1::__shared_weak_count::__release_weak()’
Please help.
Regards,
Kiran
LikeLike
Two potential things here:
have you tried `use_custom_libcxx=false` as config param ?
Also, NDK has a different sodlib prefix. for android, and more specifically on latest versions, I must have had to make a change in `v8/buildtools/third_party/libc++/trunk/include/__config` file:
set `_LIBCPP_CONCAT(__ndk,_LIBCPP_ABI_VERSION)`, so that stdlib’s prefix changes from __1 to __ndk1
Hope that helps.
LikeLike