Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 23 additions & 30 deletions extension/android/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,57 @@ load("@fbsource//tools/build_defs/android:fb_android_library.bzl", "fb_android_l

oncall("executorch")

# Thin Android shell. The platform-neutral API sources moved to
# //xplat/executorch/extension/java; these targets keep their historical names
# and re-export the shared targets so existing dependents keep working.

non_fbcode_target(_kind = fb_android_library,
name = "executorch",
warnings_as_errors = False,
required_for_source_only_abi = True,
srcs = [
"executorch_android/src/main/java/org/pytorch/executorch/BackendOptionsMap.kt",
"executorch_android/src/main/java/org/pytorch/executorch/DType.kt",
"executorch_android/src/main/java/org/pytorch/executorch/EValue.kt",
"executorch_android/src/main/java/org/pytorch/executorch/ExecuTorchRuntime.kt",
"executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.kt",
"executorch_android/src/main/java/org/pytorch/executorch/MethodMetadata.kt",
"executorch_android/src/main/java/org/pytorch/executorch/Module.kt",
"executorch_android/src/main/java/org/pytorch/executorch/Tensor.kt",
"executorch_android/src/main/java/org/pytorch/executorch/annotations/Experimental.kt",
"executorch_android/src/main/java/org/pytorch/executorch/AndroidLogger.kt",
"executorch_android/src/main/java/org/pytorch/executorch/ExecuTorchInitProvider.kt",
],
autoglob = False,
language = "KOTLIN",
pure_kotlin = False,
extra_kotlinc_arguments = ["-Xjvm-default=all"],
exported_deps = [
"//xplat/executorch/extension/java:executorch",
],
deps = [
"//fbandroid/java/com/facebook/jni:jni",
"//fbandroid/libraries/soloader/java/com/facebook/soloader/nativeloader:nativeloader",
] + ([
"//xplat/executorch/backends/vulkan:vulkan_backend_lib_static",
] if read_config("executorch", "minimal_jni", "false") == "false" else []),
],
)

non_fbcode_target(_kind = fb_android_library,
name = "executorch_training",
warnings_as_errors = False,
srcs = [
"executorch_android/src/main/java/org/pytorch/executorch/training/SGD.kt",
"executorch_android/src/main/java/org/pytorch/executorch/training/TrainingModule.kt",
],
autoglob = False,
language = "KOTLIN",
deps = [
":executorch",
"//fbandroid/java/com/facebook/jni:jni",
"//fbandroid/libraries/soloader/java/com/facebook/soloader/nativeloader:nativeloader",
exported_deps = [
"//xplat/executorch/extension/java:executorch_training",
],
)

non_fbcode_target(_kind = fb_android_library,
name = "executorch_llama",
warnings_as_errors = False,
srcs = [
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmCallback.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmGenerationConfig.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModule.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModuleConfig.kt",
autoglob = False,
language = "KOTLIN",
exported_deps = [
"//xplat/executorch/extension/java:executorch_llama",
],
)

non_fbcode_target(_kind = fb_android_library,
name = "executorch_asr",
warnings_as_errors = False,
autoglob = False,
language = "KOTLIN",
extra_kotlinc_arguments = ["-Xjvm-default=all"],
deps = [
":executorch",
"//fbandroid/java/com/facebook/jni:jni",
"//fbandroid/libraries/soloader/java/com/facebook/soloader/nativeloader:nativeloader",
exported_deps = [
"//xplat/executorch/extension/java:executorch_asr",
],
)
157 changes: 96 additions & 61 deletions extension/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,84 @@ if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(NOT ANDROID)
message(FATAL_ERROR "This directory is for Android build only")
endif()

set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
set(_common_compile_options
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations -fPIC>
)
if(NOT ANDROID_PLATFORM)
set(ANDROID_PLATFORM android-30)
endif()

# We need to download fbjni library from maven, and use its "prefab" library and
# headers, and link executorch library against that fbjni library. We don't know
# which NDK is used to compile fbjni, and we need to link our executorch library
# to the version which Android APK links against for runtime to ensure the
# libc++ dependencies are consistent. WARNING # Users need to use the SAME fbjni
# version here and in app gradle dependency for runtime compatibility!
if(NOT FBJNI_VERSION)
set(FBJNI_VERSION 0.7.0)
endif()
if(NOT ANDROID)
find_package(JNI REQUIRED)
if(NOT FBJNI_HEADERS_DIR OR NOT FBJNI_LIBRARY)
message(
FATAL_ERROR
"For non-Android platforms, please specify -DFBJNI_HEADERS_DIR=/path/to/fbjni/include and -DFBJNI_LIBRARY=/path/to/libfbjni.so"
)
endif()

set(FBJNI_AAR_URL
https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar
)
set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar)
add_library(fbjni SHARED IMPORTED)
set_target_properties(fbjni PROPERTIES IMPORTED_LOCATION "${FBJNI_LIBRARY}")
set(_common_compile_options
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations -fPIC>
)
else()
set(_common_compile_options
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations -fPIC>
)
if(NOT ANDROID_PLATFORM)
set(ANDROID_PLATFORM android-30)
endif()

if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
endif()
# We need to download fbjni library from maven, and use its "prefab" library
# and headers, and link executorch library against that fbjni library. We
# don't know which NDK is used to compile fbjni, and we need to link our
# executorch library to the version which Android APK links against for
# runtime to ensure the libc++ dependencies are consistent. WARNING # Users
# need to use the SAME fbjni version here and in app gradle dependency for
# runtime compatibility!
if(NOT FBJNI_VERSION)
set(FBJNI_VERSION 0.7.0)
endif()

add_custom_command(
OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
DEPENDS "${FBJNI_DOWNLOAD_PATH}"
)
set(FBJNI_AAR_URL
https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar
)
set(FBJNI_DOWNLOAD_PATH
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar
)

add_custom_target(
fbjni_prefab
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
endif()

add_library(fbjni SHARED IMPORTED)
add_dependencies(fbjni fbjni_prefab)
set_target_properties(
fbjni
PROPERTIES
IMPORTED_LOCATION
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
add_custom_command(
OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
DEPENDS "${FBJNI_DOWNLOAD_PATH}"
)

add_custom_target(
fbjni_prefab
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)

executorch_target_link_options_shared_lib(executorch)
add_library(fbjni SHARED IMPORTED)
add_dependencies(fbjni fbjni_prefab)
set_target_properties(
fbjni
PROPERTIES
IMPORTED_LOCATION
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
endif()

if(ANDROID)
executorch_target_link_options_shared_lib(executorch)
endif()

add_library(
executorch_jni SHARED jni/jni_layer.cpp jni/log.cpp jni/jni_layer_runtime.cpp
Expand Down Expand Up @@ -242,19 +261,35 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/fb/extended_targets/CMakeLists.txt")
include("${CMAKE_CURRENT_SOURCE_DIR}/fb/extended_targets/CMakeLists.txt")
endif()

target_include_directories(
executorch_jni
PRIVATE
${_common_include_directories}
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
)
if(ANDROID)
target_include_directories(
executorch_jni
PRIVATE
${_common_include_directories}
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
)
else()
target_include_directories(
executorch_jni
PRIVATE
${_common_include_directories}
${FBJNI_HEADERS_DIR}
${JNI_INCLUDE_DIRS}
)
endif()

target_compile_options(executorch_jni PUBLIC ${_common_compile_options})

target_link_options(
executorch_jni PRIVATE
"LINKER:--version-script,${CMAKE_CURRENT_SOURCE_DIR}/jni/version_script.txt"
)
if(NOT WIN32)
target_link_options(
executorch_jni PRIVATE
"LINKER:--version-script,${CMAKE_CURRENT_SOURCE_DIR}/jni/version_script.txt"
)
endif()
target_link_options_gc_sections(executorch_jni)

target_link_libraries(executorch_jni ${link_libraries} log)
if(ANDROID)
target_link_libraries(executorch_jni ${link_libraries} log)
else()
target_link_libraries(executorch_jni ${link_libraries} ${JNI_LIBRARIES})
endif()
6 changes: 6 additions & 0 deletions extension/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ allprojects {
dependencies {
classpath 'com.android.tools.build:gradle:8.9.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.34.0'
classpath 'com.diffplug.spotless:spotless-plugin-gradle:8.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23"
}
}

ext {
fbjniJavaOnlyVersion = "0.7.0"
soLoaderNativeLoaderVersion = "0.10.5"
}

repositories {
Expand Down
20 changes: 11 additions & 9 deletions extension/android/executorch_android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
*/


plugins {
id "com.android.library" version "8.9.0"
id "com.vanniktech.maven.publish" version "0.31.0"
id 'com.diffplug.spotless' version '8.0.0'
alias(libs.plugins.jetbrains.kotlin.android)
}
apply plugin: 'com.android.library'
apply plugin: 'com.vanniktech.maven.publish'
apply plugin: 'com.diffplug.spotless'
apply plugin: 'kotlin-android'

spotless {
kotlin {
Expand All @@ -37,8 +35,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

sourceSets {
Expand All @@ -50,7 +48,7 @@ android {
}
}
kotlinOptions {
jvmTarget = "11"
jvmTarget = "17"
freeCompilerArgs += ["-Xjvm-default=all"]
}
}
Expand All @@ -60,6 +58,10 @@ task copyTestRes(type: Exec) {
}

dependencies {
// Platform-neutral API (Module, Tensor, EValue, ...) shared with the desktop JVM artifact.
// Declared as `api` so consumers of executorch-android resolve executorch-java transitively,
// exactly like the classes that used to ship inside the AAR.
api project(':executorch_java')
implementation 'com.facebook.fbjni:fbjni:0.7.0'
implementation 'com.facebook.soloader:nativeloader:0.10.5'
implementation libs.core.ktx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<provider
android:name="org.pytorch.executorch.ExecuTorchInitProvider"
android:authorities="${applicationId}.executorch-init"
android:exported="false" />
</application>
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

package org.pytorch.executorch

import android.util.Log as AndroidLog

/**
* Android [Logger] implementation that forwards ExecuTorch logs to `android.util.Log` (logcat).
* Installed automatically at process start by [ExecuTorchInitProvider].
*/
class AndroidLogger : Logger {
override fun e(tag: String, msg: String) {
AndroidLog.e(tag, msg)
}

override fun w(tag: String, msg: String) {
AndroidLog.w(tag, msg)
}

override fun i(tag: String, msg: String) {
AndroidLog.i(tag, msg)
}

override fun d(tag: String, msg: String) {
AndroidLog.d(tag, msg)
}
}
Loading
Loading