From 5651c232718cb4cb2865cc32729ace4bfdecd76f Mon Sep 17 00:00:00 2001 From: gwang-msft <62914304+gwang-msft@users.noreply.github.com> Date: Wed, 2 Sep 2020 00:48:06 -0700 Subject: [PATCH] Fix for Android ORT android initOsArch exception (#5006) --- BUILD.md | 2 +- java/src/main/java/ai/onnxruntime/OnnxRuntime.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/BUILD.md b/BUILD.md index e5dffe9f21..3b807f9572 100644 --- a/BUILD.md +++ b/BUILD.md @@ -16,7 +16,7 @@ * [Intel DNNL/MKL-ML](#DNNL-and-MKLML) * [Intel nGraph](#nGraph) * [Intel OpenVINO](#openvino) - * [Android NNAPI](#Android-NNAPI) + * [Android NNAPI](#Android-NNAPI-Execution-Provider) * [Nuphar Model Compiler](#Nuphar) * [DirectML](#DirectML) * [ARM Compute Library](#ARM-Compute-Library) diff --git a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java index ce46a00951..320783e447 100644 --- a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java +++ b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java @@ -37,6 +37,8 @@ final class OnnxRuntime { private static boolean loaded = false; + private static final boolean IS_ANDROID = isAndroid(); + /** The API handle. */ static long ortApiHandle; @@ -52,8 +54,10 @@ final class OnnxRuntime { detectedOS = "win"; } else if (os.contains("nux")) { detectedOS = "linux"; - } else { + } else if (IS_ANDROID) { detectedOS = "android"; + } else { + throw new IllegalStateException("Unsupported os:" + os); } String detectedArch = null; String arch = System.getProperty("os.arch", "generic").toLowerCase(Locale.ENGLISH); @@ -61,6 +65,8 @@ final class OnnxRuntime { detectedArch = "x64"; } else if (arch.indexOf("x86") == 0) { detectedArch = "x86"; + } else if (IS_ANDROID) { + detectedArch = arch; } else { throw new IllegalStateException("Unsupported arch:" + arch); } @@ -76,14 +82,14 @@ final class OnnxRuntime { if (loaded) { return; } - Path tempDirectory = isAndroid() ? null : Files.createTempDirectory("onnxruntime-java"); + Path tempDirectory = IS_ANDROID ? null : Files.createTempDirectory("onnxruntime-java"); try { load(tempDirectory, ONNXRUNTIME_LIBRARY_NAME); load(tempDirectory, ONNXRUNTIME_JNI_LIBRARY_NAME); ortApiHandle = initialiseAPIBase(ORT_API_VERSION_3); loaded = true; } finally { - if (!isAndroid()) { + if (!IS_ANDROID) { cleanUp(tempDirectory.toFile()); } } @@ -129,7 +135,7 @@ final class OnnxRuntime { */ private static void load(Path tempDirectory, String library) throws IOException { // On Android, we simply use System.loadLibrary - if (isAndroid()) { + if (IS_ANDROID) { System.loadLibrary("onnxruntime4j_jni"); return; }