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,6 +64,7 @@ public class LibraryUtils { @@ -64,6 +64,7 @@ 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 + if (!isAndroid()) {
67 try { 68 try {
68 if (loadFromResourceInJar()) { 69 if (loadFromResourceInJar()) {
69 return; 70 return;
@@ -71,6 +72,7 @@ public class LibraryUtils { @@ -71,6 +72,7 @@ public class LibraryUtils {
71 } catch (IOException e) { 72 } catch (IOException e) {
72 // pass 73 // pass
73 } 74 }
  75 + }
74 76
75 // 3. fallback to -Djava.library.path 77 // 3. fallback to -Djava.library.path
76 // java -Djava.library.path=C:\mylibs;D:\otherlibs -cp sherpa-onnx.jar xxx.java 78 // java -Djava.library.path=C:\mylibs;D:\otherlibs -cp sherpa-onnx.jar xxx.java
@@ -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);
@@ -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 }