Fangjun Kuang
Committed by GitHub

Disable loading libs from jar on Android. (#2557)

This PR disables loading native libraries from JAR resources specifically on Android platforms. The change prevents potential issues with JAR-based library loading on Android while maintaining compatibility with other platforms.
@@ -64,12 +64,14 @@ public class LibraryUtils { @@ -64,12 +64,14 @@ public class LibraryUtils {
64 } 64 }
65 65
66 // 2. Load from resources contains in some jar file 66 // 2. Load from resources contains in some jar file
67 - try {  
68 - if (loadFromResourceInJar()) {  
69 - return; 67 + if (!isAndroid()) {
  68 + try {
  69 + if (loadFromResourceInJar()) {
  70 + return;
  71 + }
  72 + } catch (IOException e) {
  73 + // pass
70 } 74 }
71 - } catch (IOException e) {  
72 - // pass  
73 } 75 }
74 76
75 // 3. fallback to -Djava.library.path 77 // 3. fallback to -Djava.library.path
@@ -106,13 +108,11 @@ public class LibraryUtils { @@ -106,13 +108,11 @@ public class LibraryUtils {
106 } 108 }
107 109
108 private static boolean loadFromResourceInJar() throws IOException { 110 private static boolean loadFromResourceInJar() throws IOException {
109 -  
110 String libFileName = System.mapLibraryName(LIB_NAME); 111 String libFileName = System.mapLibraryName(LIB_NAME);
111 String sherpaOnnxJniPath = "sherpa-onnx/native/" + getOsArch() + '/' + libFileName; 112 String sherpaOnnxJniPath = "sherpa-onnx/native/" + getOsArch() + '/' + libFileName;
112 113
113 Path tempDirectory = null; 114 Path tempDirectory = null;
114 try { 115 try {
115 -  
116 if (!resourceExists(sherpaOnnxJniPath)) { 116 if (!resourceExists(sherpaOnnxJniPath)) {
117 if (debug) { 117 if (debug) {
118 System.out.printf("%s does not exist\n", sherpaOnnxJniPath); 118 System.out.printf("%s does not exist\n", sherpaOnnxJniPath);
@@ -181,7 +181,7 @@ public class LibraryUtils { @@ -181,7 +181,7 @@ public class LibraryUtils {
181 181
182 String detectedArch; 182 String detectedArch;
183 String arch = System.getProperty("os.arch", "generic") 183 String arch = System.getProperty("os.arch", "generic")
184 - .toLowerCase(Locale.ENGLISH); 184 + .toLowerCase(Locale.ENGLISH);
185 if (arch.startsWith("amd64") || arch.startsWith("x86_64")) { 185 if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
186 detectedArch = "x64"; 186 detectedArch = "x64";
187 } else if (arch.startsWith("x86")) { 187 } else if (arch.startsWith("x86")) {
@@ -191,7 +191,7 @@ public class LibraryUtils { @@ -191,7 +191,7 @@ public class LibraryUtils {
191 detectedArch = "aarch64"; 191 detectedArch = "aarch64";
192 } else if (arch.startsWith("arm")) { 192 } else if (arch.startsWith("arm")) {
193 detectedArch = "arm"; //armv8l架构 193 detectedArch = "arm"; //armv8l架构
194 - } else { 194 + } else {
195 throw new IllegalStateException("Unsupported arch:" + arch); 195 throw new IllegalStateException("Unsupported arch:" + arch);
196 } 196 }
197 197
@@ -236,4 +236,10 @@ public class LibraryUtils { @@ -236,4 +236,10 @@ public class LibraryUtils {
236 } 236 }
237 dir.deleteOnExit(); // schedule the directory itself 237 dir.deleteOnExit(); // schedule the directory itself
238 } 238 }
  239 +
  240 + String vmName = System.getProperty("java.vm.name", "").toLowerCase(Locale.ROOT);
  241 + String specVendor = System.getProperty("java.specification.vendor", "");
  242 + return vmName.contains("dalvik") || vmName.contains("art") ||
  243 + specVendor.equals("The Android Project");
  244 + }
239 } 245 }