Skip to content

Commit fbd30c5

Browse files
authored
Merge pull request #1 from Dimezis/fix-build
Fix the build process
2 parents 45925cc + 8588c10 commit fbd30c5

File tree

13 files changed

+310
-29
lines changed

13 files changed

+310
-29
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ test-mockup-os-release
3535

3636
# generated dependency packages
3737
*.tar.*
38+
39+
local.properties

Build_new.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Setup for J2V8 Native Library Building
2+
3+
## Install Android NDK
4+
5+
**Install NDK via SDK Manager**
6+
- Open Android Studio
7+
- Go to Tools > SDK Manager
8+
- Click "SDK Tools" tab
9+
- Check NDK and click apply
10+
11+
**Set environment variable**
12+
```bash
13+
# Add to ~/.zshrc or ~/.bash_profile
14+
export ANDROID_NDK_HOME=~/Library/Android/sdk/ndk-bundle
15+
16+
# Reload shell
17+
source ~/.zshrc
18+
```
19+
20+
## Run the build script
21+
22+
```bash
23+
./rebuild_native.sh
24+
```
25+
This will compile new .so libraries from V8 sources and place them in `src/main/jniLibs/` replacing the existing ones.
26+
It will also run `./gradlew assembleRelease` and build the .aar artifact.

build.gradle

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,48 @@ ext."signing.keyId" = System.getenv("KEY_ID")
1212

1313
buildscript {
1414
repositories {
15-
jcenter()
15+
google()
16+
mavenCentral()
1617
}
1718
dependencies {
18-
classpath 'com.android.tools.build:gradle:2.2.2'
19+
classpath 'com.android.tools.build:gradle:8.7.3'
1920
classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
2021
}
2122
}
2223

2324
apply plugin: 'spoon'
2425

2526
repositories {
26-
jcenter()
27+
google()
28+
mavenCentral()
2729
}
2830

2931
dependencies {
30-
testCompile 'junit:junit:4.12'
31-
//testCompile 'org.mockito:mockito-all:2.6.3'
32-
testCompile 'org.mockito:mockito-core:2.6.3'
33-
34-
androidTestCompile 'junit:junit:4.12'
35-
androidTestCompile 'org.mockito:mockito-android:2.6.3' // https://jeroenmols.com/blog/2017/01/17/mockitoandroid/
36-
androidTestCompile 'com.android.support:support-annotations:24.0.0'
37-
androidTestCompile 'com.android.support.test:runner:0.5'
38-
androidTestCompile 'com.android.support.test:rules:0.5'
32+
testImplementation 'junit:junit:4.13.2'
33+
//testImplementation 'org.mockito:mockito-all:2.6.3'
34+
testImplementation 'org.mockito:mockito-core:4.11.0'
35+
36+
androidTestImplementation 'junit:junit:4.13.2'
37+
androidTestImplementation 'org.mockito:mockito-android:4.11.0' // https://jeroenmols.com/blog/2017/01/17/mockitoandroid/
38+
androidTestImplementation 'androidx.annotation:annotation:1.9.1'
39+
androidTestImplementation 'androidx.test:runner:1.6.2'
40+
androidTestImplementation 'androidx.test:rules:1.6.1'
3941
}
4042

4143
android {
42-
compileSdkVersion 10
43-
buildToolsVersion '24.0.3'
44-
44+
namespace 'com.eclipsesource.v8'
45+
compileSdk 35
46+
4547
defaultConfig {
46-
minSdkVersion 10
47-
targetSdkVersion 10
48+
minSdkVersion 21
49+
targetSdkVersion 35
4850

49-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
51+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
52+
}
53+
54+
compileOptions {
55+
sourceCompatibility JavaVersion.VERSION_1_8
56+
targetCompatibility JavaVersion.VERSION_1_8
5057
}
5158

5259
lintOptions {
@@ -79,10 +86,12 @@ spoon {
7986
}
8087
}
8188

82-
publishing {
83-
publications {
84-
maven(MavenPublication) {
85-
artifact("$buildDir/outputs/aar/j2v8-release.aar")
89+
afterEvaluate {
90+
publishing {
91+
publications {
92+
maven(MavenPublication) {
93+
artifact bundleReleaseAar
94+
}
8695
}
8796
}
88-
}
97+
}

check_elf_alignment.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
progname="${0##*/}"
3+
progname="${progname%.sh}"
4+
5+
# Usage: check_elf_alignment.sh [path to *.so files|path to *.apk]
6+
# Copied from https://developer.android.com/guide/practices/page-sizes
7+
8+
cleanup_trap() {
9+
if [ -n "${tmp}" -a -d "${tmp}" ]; then
10+
rm -rf ${tmp}
11+
fi
12+
exit $1
13+
}
14+
15+
usage() {
16+
echo "Host side script to check the ELF alignment of shared libraries."
17+
echo "Shared libraries are reported ALIGNED when their ELF regions are"
18+
echo "16 KB or 64 KB aligned. Otherwise they are reported as UNALIGNED."
19+
echo
20+
echo "Usage: ${progname} [input-path|input-APK|input-APEX]"
21+
}
22+
23+
if [ ${#} -ne 1 ]; then
24+
usage
25+
exit
26+
fi
27+
28+
case ${1} in
29+
--help | -h | -\?)
30+
usage
31+
exit
32+
;;
33+
34+
*)
35+
dir="${1}"
36+
;;
37+
esac
38+
39+
if ! [ -f "${dir}" -o -d "${dir}" ]; then
40+
echo "Invalid file: ${dir}" >&2
41+
exit 1
42+
fi
43+
44+
if [[ "${dir}" == *.apk ]]; then
45+
trap 'cleanup_trap' EXIT
46+
47+
echo
48+
echo "Recursively analyzing $dir"
49+
echo
50+
51+
if { zipalign --help 2>&1 | grep -q "\-P <pagesize_kb>"; }; then
52+
echo "=== APK zip-alignment ==="
53+
zipalign -v -c -P 16 4 "${dir}" | egrep 'lib/arm64-v8a|lib/x86_64|Verification'
54+
echo "========================="
55+
else
56+
echo "NOTICE: Zip alignment check requires build-tools version 35.0.0-rc3 or higher."
57+
echo " You can install the latest build-tools by running the below command"
58+
echo " and updating your \$PATH:"
59+
echo
60+
echo " sdkmanager \"build-tools;35.0.0-rc3\""
61+
fi
62+
63+
dir_filename=$(basename "${dir}")
64+
tmp=$(mktemp -d -t "${dir_filename%.apk}_out_XXXXX")
65+
unzip "${dir}" lib/* -d "${tmp}" >/dev/null 2>&1
66+
dir="${tmp}"
67+
fi
68+
69+
if [[ "${dir}" == *.apex ]]; then
70+
trap 'cleanup_trap' EXIT
71+
72+
echo
73+
echo "Recursively analyzing $dir"
74+
echo
75+
76+
dir_filename=$(basename "${dir}")
77+
tmp=$(mktemp -d -t "${dir_filename%.apex}_out_XXXXX")
78+
deapexer extract "${dir}" "${tmp}" || { echo "Failed to deapex." && exit 1; }
79+
dir="${tmp}"
80+
fi
81+
82+
RED="\e[31m"
83+
GREEN="\e[32m"
84+
ENDCOLOR="\e[0m"
85+
86+
unaligned_libs=()
87+
88+
echo
89+
echo "=== ELF alignment ==="
90+
91+
matches="$(find "${dir}" -type f)"
92+
IFS=$'\n'
93+
for match in $matches; do
94+
# We could recursively call this script or rewrite it to though.
95+
[[ "${match}" == *".apk" ]] && echo "WARNING: doesn't recursively inspect .apk file: ${match}"
96+
[[ "${match}" == *".apex" ]] && echo "WARNING: doesn't recursively inspect .apex file: ${match}"
97+
98+
[[ $(file "${match}") == *"ELF"* ]] || continue
99+
100+
res="$(objdump -p "${match}" | grep LOAD | awk '{ print $NF }' | head -1)"
101+
if [[ $res =~ 2\*\*(1[4-9]|[2-9][0-9]|[1-9][0-9]{2,}) ]]; then
102+
echo -e "${match}: ${GREEN}ALIGNED${ENDCOLOR} ($res)"
103+
else
104+
echo -e "${match}: ${RED}UNALIGNED${ENDCOLOR} ($res)"
105+
unaligned_libs+=("${match}")
106+
fi
107+
done
108+
109+
if [ ${#unaligned_libs[@]} -gt 0 ]; then
110+
echo -e "${RED}Found ${#unaligned_libs[@]} unaligned libs (only arm64-v8a/x86_64 libs need to be aligned).${ENDCOLOR}"
111+
elif [ -n "${dir_filename}" ]; then
112+
echo -e "ELF Verification Successful"
113+
fi
114+
echo "====================="

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#increase jvm heap space available for gradle
22
#(allows to run dex in the same process as gradle)
33
org.gradle.jvmargs=-Xmx4608M
4+
android.useAndroidX=true
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Dec 15 11:54:06 CET 2014
2-
distributionBase=GRADLE_USER_HOME
3-
distributionPath=wrapper/dists
4-
zipStoreBase=GRADLE_USER_HOME
5-
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
1+
#Thu Jun 05 09:06:59 CEST 2025
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

jni/com_eclipsesource_v8_V8Impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
#define TAG "J2V8_V8Impl"
2323

24+
#ifdef _WIN32
2425
#pragma comment(lib, "userenv.lib")
2526
#pragma comment(lib, "IPHLPAPI.lib")
2627
#pragma comment(lib, "Ws2_32.lib")
2728
#pragma comment(lib, "WINMM.lib")
2829
#pragma comment( lib, "psapi.lib" )
30+
#endif
2931

3032
using namespace std;
3133
using namespace v8;

rebuild_native.sh

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

src/main/jniLibs/arm64-v8a/libj2v8.so

100644100755
-619 KB
Binary file not shown.

src/main/jniLibs/armeabi-v7a/libj2v8.so

100644100755
-335 KB
Binary file not shown.

0 commit comments

Comments
 (0)