Skip to content

Commit 99612ae

Browse files
authored
Build OTIO for Android and add GitHub Actions workflow (#3)
* Initialize android build * Update gradle.yml * Update gradle.yml * Add Android build instructions to README.md * Update OpenTimelineIO and fix include paths * remove submodule * Fix submodule and abi * Remove comments and unintentional debug leftovers
1 parent 209f292 commit 99612ae

File tree

8 files changed

+125
-12
lines changed

8 files changed

+125
-12
lines changed

.github/workflows/gradle.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ jobs:
4343
name: Package-${{ matrix.os }}
4444
path: build/natives/bin/Release
4545

46+
android:
47+
name: Build for android
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Install NDK and CMake
51+
shell: bash
52+
run: $ANDROID_HOME/tools/bin/sdkmanager "ndk;22.0.7026061" "cmake;3.10.2.4988404"
53+
- uses: actions/checkout@v2
54+
with:
55+
submodules: 'recursive'
56+
- name: Build
57+
shell: bash
58+
run: ./gradlew build -x test -PandroidBuild -Psdk_path=$ANDROID_HOME
59+
- name: Upload artifact
60+
uses: actions/upload-artifact@v2
61+
with:
62+
name: Android-JAR
63+
path: build/libs
64+
4665
combine:
4766
name: Download platform-specific binaries and combine into single JAR
4867
needs: [build]

.gitmodules

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
[submodule "deps/OpenTimelineIO"]
22
path = deps/OpenTimelineIO
33
url = https://github.com/PixarAnimationStudios/OpenTimelineIO
4-
branch = master

CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
22

33
project(jotio VERSION 0.14.0 LANGUAGES CXX)
44

@@ -11,7 +11,11 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/$<CONFIG>/${CMAKE_SYS
1111
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>/${CMAKE_SYSTEM_NAME})
1212

1313
# jvm
14-
find_package(Java REQUIRED)
14+
if(NOT DEFINED ANDROID_ABI)
15+
find_package(Java REQUIRED)
16+
else()
17+
set(JAVA_INCLUDE_PATH2 NotNeeded)
18+
endif()
1519
# https://stackoverflow.com/questions/51047978/cmake-could-not-find-jni
1620
set(JAVA_AWT_LIBRARY NotNeeded)
1721
set(JAVA_JVM_LIBRARY NotNeeded)
@@ -36,6 +40,14 @@ if(NOT CMAKE_BUILD_TYPE)
3640
FORCE)
3741
endif()
3842

43+
set(OTIO_PYTHON_INSTALL OFF CACHE BOOL "")
44+
set(OTIO_CXX_INSTALL OFF CACHE BOOL "")
45+
set(OTIO_DEPENDENCIES_INSTALL OFF CACHE BOOL "")
46+
set(OTIO_INSTALL_COMMANDLINE_TOOLS OFF CACHE BOOL "")
47+
set(OTIO_SHARED_LIBS ON)
48+
49+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
50+
3951
include_directories(src/main/include)
4052
add_subdirectory(deps/OpenTimelineIO build/natives)
4153
add_subdirectory(src/main/cpp)

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,70 @@ gradle build # this builds and runs all tests
6565

6666
You can find the generated jar file in the `build/libs` directory.
6767

68+
Building OpenTimelineIO-Java-Bindings for Android
69+
------------------------
70+
71+
This has been built and tested on Ubuntu 18.04LTS.
72+
73+
Install Android Studio according to these [steps](https://developer.android.com/studio).
74+
75+
Install NDK and CMake using [sdkmanager](https://developer.android.com/studio/command-line/sdkmanager) as follows:
76+
77+
`sdkmanager "ndk;22.0.7026061" "cmake;3.10.2.4988404"`
78+
79+
You can find `sdkmanager` in `$ANDROID_HOME/tools/bin/`. `$ANDROID_HOME` is the location of Android SDK which is generally `~/Android/Sdk`.
80+
81+
Set the `ANDROID_HOME` environment variable and from the root directory of the project run:
82+
83+
```console
84+
gradle clean
85+
gradle build -x test -PandroidBuild -Psdk_path=$ANDROID_HOME
86+
```
87+
88+
You'll find the JAR in `build/libs`
89+
90+
#### How to include the Android JAR in your project
91+
92+
Copy the JAR to the `libs` directory and add this to the app level `build.gradle`:
93+
94+
```groovy
95+
android {
96+
...
97+
98+
ndkVersion '22.0.7026061'
99+
100+
compileOptions {
101+
sourceCompatibility JavaVersion.VERSION_1_8
102+
targetCompatibility JavaVersion.VERSION_1_8
103+
}
104+
...
105+
}
106+
107+
task unjar {
108+
ant.unzip(src: 'libs/java-opentimelineio-0.14.0.jar', dest: 'JARUnzip')
109+
110+
copy {
111+
from 'JARUnzip/arm64-v8a'
112+
into 'src/main/jniLibs/arm64-v8a'
113+
}
114+
copy {
115+
from 'JARUnzip/x86_64'
116+
into 'src/main/jniLibs/x86_64'
117+
}
118+
}
119+
120+
build.dependsOn unjar
121+
122+
dependencies {
123+
...
124+
implementation fileTree(dir: "libs", include: ["*.jar"])
125+
implementation files('libs/java-opentimelineio-0.14.0.jar')
126+
...
127+
}
128+
129+
```
130+
131+
68132
Examples
69133
--------
70134

build.gradle

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,21 @@ task compileJNI {
6666
if (OperatingSystem.current().isLinux() ||
6767
OperatingSystem.current().isMacOsX() ||
6868
OperatingSystem.current().isUnix()) {
69-
commandLine 'sh', '-c', 'mkdir -p build/natives && cd build/natives && cmake ../.. && cmake --build . --config Release'
69+
if (project.hasProperty("androidBuild") && project.hasProperty("sdk_path")){
70+
def sdk_path = project.getProperties().getAt('sdk_path').toString()
71+
commandLine 'sh', '-c', 'mkdir -p build/natives && ' +
72+
'cd build/natives && ' +
73+
'cmake -G"Ninja" -DANDROID_ABI=arm64-v8a -DANDROID_NDK='+sdk_path+'/ndk/22.0.7026061 -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM='+sdk_path+'/cmake/3.10.2.4988404/bin/ninja -DCMAKE_TOOLCHAIN_FILE='+sdk_path+'/ndk/22.0.7026061/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=23 -DANDROID_TOOLCHAIN=clang ../.. && ' +
74+
'cmake --build . --config Release && ' +
75+
'mv lib/Release/Android/ lib/Release/arm64-v8a && ' +
76+
'rm CMakeCache.txt && ' +
77+
'cmake -G"Ninja" -DANDROID_ABI=x86_64 -DANDROID_NDK='+sdk_path+'/ndk/22.0.7026061 -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM='+sdk_path+'/cmake/3.10.2.4988404/bin/ninja -DCMAKE_TOOLCHAIN_FILE='+sdk_path+'/ndk/22.0.7026061/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=23 -DANDROID_TOOLCHAIN=clang ../.. && ' +
78+
'cmake --build . --config Release && ' +
79+
'mv lib/Release/Android/ lib/Release/x86_64'
80+
}
81+
else {
82+
commandLine 'sh', '-c', 'mkdir -p build/natives && cd build/natives && cmake ../.. && cmake --build . --config Release'
83+
}
7084
} else if (OperatingSystem.current().isWindows()) {
7185
commandLine "cmd", "/c", 'if not exist "build\\natives" mkdir build\\natives && cd build\\natives && cmake ..\\.. && cmake --build . --config Release'
7286
}

deps/OpenTimelineIO

src/main/cpp/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(jotio-${CMAKE_PROJECT_VERSION} SHARED
1+
add_library(jotio SHARED
22
io_opentimeline_OTIOFinalizer.cpp
33
class_codes.cpp
44
utilities.cpp
@@ -41,8 +41,10 @@ add_library(jotio-${CMAKE_PROJECT_VERSION} SHARED
4141
io_opentimeline_opentimelineio_Deserialization.cpp
4242
io_opentimeline_opentimelineio_OTIOTest.cpp)
4343

44-
target_include_directories(jotio-${CMAKE_PROJECT_VERSION} PUBLIC
45-
"${PROJECT_SOURCE_DIR}/../")
44+
target_include_directories(jotio PUBLIC
45+
"${PROJECT_SOURCE_DIR}/deps/OpenTimelineIO/src"
46+
"${PROJECT_SOURCE_DIR}/deps/OpenTimelineIO/src/deps"
47+
"${PROJECT_SOURCE_DIR}/deps/OpenTimelineIO/src/deps/optional-lite/include")
4648

47-
target_link_libraries(jotio-${CMAKE_PROJECT_VERSION} opentime)
48-
target_link_libraries(jotio-${CMAKE_PROJECT_VERSION} opentimelineio)
49+
target_link_libraries(jotio opentime)
50+
target_link_libraries(jotio opentimelineio)

src/main/java/io/opentimeline/LibraryLoader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ private static String getOSName() {
1818
String osName = System.getProperty("os.name").toLowerCase();
1919
if (osName.contains("win")) return "Windows";
2020
else if (osName.contains("mac")) return "Darwin";
21-
else if (osName.contains("nux")) return "Linux";
21+
else if (osName.contains("nux")) {
22+
if(System.getProperty("java.vm.name").toLowerCase().contains("dalvik"))
23+
return "Android";
24+
return "Linux";
25+
}
2226
return "";
2327
}
2428

2529
public static void load(String name) {
2630
if (libLoaded)
2731
return;
28-
name += ("-" + OTIO_VERSION);
2932
final String libname = System.mapLibraryName(name);
3033
final String opentimelibname = System.mapLibraryName("opentime");
3134
final String OTIOlibname = System.mapLibraryName("opentimelineio");

0 commit comments

Comments
 (0)