|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# A script to rebuild J2V8 native libraries for Android |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +echo "J2V8 Native Library Rebuild Script" |
| 8 | +echo "==================================" |
| 9 | +echo "✅ Builds 16KB-aligned .so libraries for Google Play compliance" |
| 10 | + |
| 11 | +# Check if Android NDK is available |
| 12 | +if [ -z "$ANDROID_NDK_HOME" ]; then |
| 13 | + echo "ERROR: ANDROID_NDK_HOME environment variable not set" |
| 14 | + echo "Please install Android NDK and set ANDROID_NDK_HOME" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Create output directories (replace existing libraries in src/main/jniLibs) |
| 19 | +mkdir -p src/main/jniLibs/arm64-v8a |
| 20 | +mkdir -p src/main/jniLibs/armeabi-v7a |
| 21 | +mkdir -p src/main/jniLibs/x86 |
| 22 | +mkdir -p src/main/jniLibs/x86_64 |
| 23 | +mkdir -p build_native/android |
| 24 | + |
| 25 | +if [ -d "v8.out" ]; then |
| 26 | + echo "✅ V8 libraries found:" |
| 27 | + find v8.out -name "*.a" | sort |
| 28 | +else |
| 29 | + # Taken from https://download.eclipsesource.com/j2v8/v8/libv8_9.3.345.11_monolith.zip |
| 30 | + # Added to VCS in case the link breaks |
| 31 | + unzip v8_monolith/libv8_9.3.345.11_monolith.zip -d v8.out |
| 32 | +fi |
| 33 | + |
| 34 | +API_LEVEL=21 |
| 35 | + |
| 36 | +# Function to build for a specific architecture |
| 37 | +build_arch() { |
| 38 | + local android_abi=$1 |
| 39 | + local v8_arch=$2 |
| 40 | + local ndk_arch=$3 |
| 41 | + |
| 42 | + echo "" |
| 43 | + echo "Building for $android_abi ($v8_arch)..." |
| 44 | + |
| 45 | + # Set up compiler paths |
| 46 | + local TOOLCHAIN="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64" |
| 47 | + if [ ! -d "$TOOLCHAIN" ]; then |
| 48 | + # Try linux toolchain |
| 49 | + TOOLCHAIN="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64" |
| 50 | + fi |
| 51 | + |
| 52 | + if [ ! -d "$TOOLCHAIN" ]; then |
| 53 | + echo "ERROR: Could not find Android NDK toolchain" |
| 54 | + return 1 |
| 55 | + fi |
| 56 | + |
| 57 | + local CC="$TOOLCHAIN/bin/${ndk_arch}${API_LEVEL}-clang" |
| 58 | + local CXX="$TOOLCHAIN/bin/${ndk_arch}${API_LEVEL}-clang++" |
| 59 | + |
| 60 | + if [ ! -f "$CC" ]; then |
| 61 | + echo "ERROR: Compiler not found: $CC" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + |
| 65 | + echo "Using compiler: $CC" |
| 66 | + |
| 67 | + # Generate JNI header if needed |
| 68 | + if [ ! -f "jni/com_eclipsesource_v8_V8Impl.h" ]; then |
| 69 | + echo "Generating JNI headers..." |
| 70 | + if [ -d "build/intermediates/javac/release/classes" ]; then |
| 71 | + javah -cp build/intermediates/javac/release/classes -o jni com.eclipsesource.v8.V8Impl || echo "Warning: Could not generate JNI headers" |
| 72 | + else |
| 73 | + echo "Warning: Java classes not found, using existing JNI headers" |
| 74 | + fi |
| 75 | + fi |
| 76 | + |
| 77 | + # Compile flags with release optimizations |
| 78 | + local CPPFLAGS="-I$ANDROID_NDK_HOME/sysroot/usr/include" |
| 79 | + CPPFLAGS="$CPPFLAGS -I$ANDROID_NDK_HOME/sysroot/usr/include/$ndk_arch" |
| 80 | + CPPFLAGS="$CPPFLAGS -Iv8.out/include" |
| 81 | + CPPFLAGS="$CPPFLAGS -Ijni" |
| 82 | + CPPFLAGS="$CPPFLAGS -fPIC -std=c++17 -O3 -DNDEBUG" |
| 83 | + |
| 84 | + # Link flags with 16KB alignment for Google Play compliance and symbol stripping |
| 85 | + local LDFLAGS="-shared -llog -s" |
| 86 | + LDFLAGS="$LDFLAGS -Wl,-z,max-page-size=16384" |
| 87 | + LDFLAGS="$LDFLAGS -Wl,-z,common-page-size=16384" |
| 88 | + |
| 89 | + # Output directly to src/main/jniLibs (replace existing files under version control) |
| 90 | + local OUTPUT="src/main/jniLibs/$android_abi/libj2v8.so" |
| 91 | + |
| 92 | + echo "Compiling libj2v8 for $android_abi..." |
| 93 | + |
| 94 | + # Compile the JNI implementation |
| 95 | + if ! $CXX $CPPFLAGS -c jni/com_eclipsesource_v8_V8Impl.cpp -o "build_native/android/v8impl_$android_abi.o"; then |
| 96 | + echo "ERROR: Compilation failed for $android_abi" |
| 97 | + return 1 |
| 98 | + fi |
| 99 | + |
| 100 | + echo "Linking libj2v8 for $android_abi..." |
| 101 | + |
| 102 | + # Link with V8 and output directly to jniLibs |
| 103 | + if ! $CXX $LDFLAGS -o "$OUTPUT" "build_native/android/v8impl_$android_abi.o" "$v8_lib"; then |
| 104 | + echo "ERROR: Linking failed for $android_abi" |
| 105 | + return 1 |
| 106 | + fi |
| 107 | + |
| 108 | + echo "✅ Built: $OUTPUT" |
| 109 | + ls -lh "$OUTPUT" |
| 110 | +} |
| 111 | + |
| 112 | +# Build for each architecture |
| 113 | +echo "Building for all Android architectures..." |
| 114 | + |
| 115 | +build_arch "arm64-v8a" "android.arm64" "aarch64-linux-android" |
| 116 | +build_arch "armeabi-v7a" "android.arm" "armv7a-linux-androideabi" |
| 117 | +build_arch "x86" "android.x86" "i686-linux-android" |
| 118 | +build_arch "x86_64" "android.x64" "x86_64-linux-android" |
| 119 | + |
| 120 | +echo "" |
| 121 | +echo "Native library rebuild complete!" |
| 122 | +echo "Libraries compiled and replaced in src/main/jniLibs:" |
| 123 | +ls -la src/main/jniLibs/*/libj2v8.so 2>/dev/null || echo "No libraries found in src/main/jniLibs/" |
| 124 | +echo "" |
| 125 | +./check_elf_alignment.sh src/main/jniLibs |
| 126 | +echo "Running './gradlew assembleRelease' to build the AAR with the new native libraries" |
| 127 | +./gradlew assembleRelease |
0 commit comments