Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions .github/workflows/android-16kb-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: Android 16KB Page Size Verification
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayanshaw24 is it ok to add a new workflow for this check? or would it be preferable to add it to the existing CI build pipeline?


on:
pull_request:
paths:
- 'CMakeLists.txt'
- 'cmake/**'
- 'java/**'
- 'tools/build.py'
- '.github/workflows/android-16kb-check.yml'
push:
branches:
- 'enable-16KB-for-android' # Your branch name

jobs:
verify-android-16kb-alignment:
name: Build & Verify 16KB Page Alignment (arm64-v8a)
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '11'

- name: Setup Android NDK
uses: nttld/setup-ndk@v1
id: setup-ndk
with:
ndk-version: r25c
add-to-path: false

- name: Set up Android SDK
uses: android-actions/setup-android@v3

- name: Build Android arm64-v8a with 16KB support
env:
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
run: |
set -e
# Use the project's build script
python tools/build.py \
--android \
--android_abi=arm64-v8a \
--android_api=27 \
--android_home=$ANDROID_HOME \
--android_ndk_path=$ANDROID_NDK_HOME \
--config=Release \
--parallel \
--build \
--cmake_extra_defines ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON
- name: Verify 16KB page alignment
env:
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
run: |
set -e
echo "=== Verifying 16KB page size alignment ==="
# Find built libraries
LIBS=$(find build/Android -name "*.so" -type f)
if [ -z "$LIBS" ]; then
echo "ERROR: No .so files found in build output"
find build -type f -name "*.so" || true
exit 1
fi
# Use llvm-readelf from NDK
READELF="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-readelf"
if [ ! -f "$READELF" ]; then
echo "ERROR: llvm-readelf not found at $READELF"
exit 1
fi
echo "Found libraries:"
echo "$LIBS"
echo ""
FAILED=0
for LIB in $LIBS; do
echo "Checking: $(basename $LIB)"
echo "Full path: $LIB"
# Display LOAD segments
$READELF -l "$LIB" | grep -A 1 "LOAD" || true
# Check for 16KB alignment (0x4000)
if $READELF -l "$LIB" | grep -q "0x4000"; then
echo "PASS: Found 16KB alignment (0x4000) in $(basename $LIB)"
else
echo "FAIL: Did not find 16KB alignment (0x4000) in $(basename $LIB)"
echo "Full segment details:"
$READELF -l "$LIB"
FAILED=1
fi
echo "---"
done
if [ $FAILED -ne 0 ]; then
echo ""
echo "ERROR: One or more libraries do not have 16KB page alignment"
exit 1
fi
echo ""
echo "SUCCESS: All libraries have correct 16KB page alignment"
- name: Upload build artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: android-arm64-libraries
path: build/Android/**/lib/*.so
retention-days: 7
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ if(OCOS_BUILD_ANDROID)
endif()

set(OCOS_BUILD_JAVA ON CACHE INTERNAL "")

# Add 16KB page size compatibility for Android 15+ devices
# See: https://developer.android.com/guide/practices/page-sizes
# This flag ensures binaries work on both 4KB and 16KB page size systems
option(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES "Enable 16KB page size support for Android" ON)

if(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES)
# Android NDK uses Clang + lld which supports this flag
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
message(STATUS "Android build: 16KB page size support enabled")
else()
message(WARNING "Compiler ${CMAKE_C_COMPILER_ID} may not support -Wl,-z,max-page-size. Skipping.")
endif()
else()
message(WARNING "Android build: 16KB page size support disabled (not recommended for production)")
endif()
endif()

# Build the libraries with -fPIC
Expand Down Expand Up @@ -905,6 +923,11 @@ if(OCOS_BUILD_SHARED_LIB)
endif()
endif()

# Add 16KB page size compatibility for Android shared library
if(ANDROID AND ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES)
target_link_options(extensions_shared PRIVATE "-Wl,-z,max-page-size=16384")
endif()

target_include_directories(extensions_shared PUBLIC $<TARGET_PROPERTY:ortcustomops,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(extensions_shared PRIVATE ortcustomops)
set_target_properties(extensions_shared PROPERTIES OUTPUT_NAME "ortextensions")
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Introduction: ONNXRuntime-Extensions is a C/C++ library that extends the capabil
## Quickstart
The library can be utilized as either a C/C++ library or other advance language packages like Python, Java, C#, etc. To build it as a shared library, you can use the `build.bat` or `build.sh` scripts located in the root folder. The CMake build definition is available in the `CMakeLists.txt` file and can be modified by appending options to `build.bat` or `build.sh`, such as `build.bat -DOCOS_BUILD_SHARED_LIB=OFF`. For more details, please refer to the [C API documentation](./docs/c_api.md).

### Android 16KB Page Size Support
Android builds automatically enable 16KB page size compatibility to support Android 15+ devices. The linker flag `-Wl,-z,max-page-size=16384` sets the maximum page alignment, ensuring binaries work on both traditional 4KB and newer 16KB page size systems. This is controlled by the `ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES` CMake option (default: ON) and applies safely to all Android ABIs including 32-bit architectures. The flag only sets a maximum alignment constraint and does not negatively impact devices using 4KB pages.

### **Python installation**
```bash
pip install onnxruntime-extensions
Expand Down
5 changes: 5 additions & 0 deletions cmake/ext_java.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ else()
target_link_libraries(onnxruntime_extensions4j_jni PRIVATE ortcustomops)
endif()

# Add 16KB page size compatibility for Android JNI library
if(ANDROID AND ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES)
target_link_options(onnxruntime_extensions4j_jni PRIVATE "-Wl,-z,max-page-size=16384")
endif()

if(LINUX)
set_property(TARGET onnxruntime_extensions4j_jni APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,--version-script -Wl,${JAVA_ROOT}/ortx_jni.ver")
Expand Down
10 changes: 10 additions & 0 deletions java/build-android.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ android {
versionName = project.version

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}

externalNativeBuild {
cmake {
arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
}
}
}

android {
Expand Down
14 changes: 12 additions & 2 deletions java/src/test/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}

externalNativeBuild {
cmake {
arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
}
}
}

buildTypes {
Expand All @@ -37,9 +47,9 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.microsoft.onnxruntime:onnxruntime-android:latest.release'
if (ortExtensionsAarLocalPath != null) {
implementation files(ortExtensionsAarLocalPath)
implementation files(ortExtensionsAarLocalPath)
} else {
implementation 'com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.release'
implementation 'com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.release'
}
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
1 change: 1 addition & 0 deletions tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ def _generate_build_tree(
+ str((args.android_ndk_path / "build" / "cmake" / "android.toolchain.cmake").resolve(strict=True)),
f"-DANDROID_PLATFORM=android-{args.android_api}",
f"-DANDROID_ABI={args.android_abi}",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
]

if is_macOS() and not args.android:
Expand Down