加载so有两种方法
System.load() 和System.loadLibrary(); 前者需传入库文件的绝对路径,后者只需传入库文件名。
首先我的jar包目录如下:
Loader是加载类:
static {
try {
InputStream is = null;
if(isCPUInfo64()) {
is = Loader.class.getResource("arm64/libhellojni.so").openStream();
}else {
is = Loader.class.getResource("arm32/libhellojni.so").openStream();
}
File tempFile = File.createTempFile("hellojni", ".so");
FileOutputStream fos = new FileOutputStream(tempFile);
int i;
byte[] buf = new byte[1024];
while ((i = is.read(buf)) != -1) {
fos.write(buf, 0, i);
}
is.close();
fos.close();
System.load(tempFile.getAbsolutePath());
tempFile.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean isCPUInfo64() {
File cpuInfo = new File("/proc/cpuinfo");
if (cpuInfo != null && cpuInfo.exists()) {
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream(cpuInfo);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512);
String line = bufferedReader.readLine();
if (line != null && line.length() > 0 && line.toLowerCase(Locale.US).contains("arch64")) {
return true;
} else {
return false;
}
} catch (Throwable t) {
Log.d("isCPUInfo64", " error = " + t.toString());
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return false;
}