Android 如何查找so文件所在的目录:OS<=2.2 和 OS>=2.3 的方法有所区别,见下面代码
/**
* The function use to find so path for compatible android system
* Android OS >= 2.3
*/
public static String findLibrary1(Context context, String libName) {
String result = null;
ClassLoader classLoader = (context.getClassLoader());
if (classLoader != null) {
try {
Method findLibraryMethod = classLoader.getClass().getMethod("findLibrary", new Class<?>[] { String.class });
if (findLibraryMethod != null) {
Object objPath = findLibraryMethod.invoke(classLoader, new Object[] { libName });
if (objPath != null && objPath instanceof String) {
result = (String) objPath;
}
}
} catch (NoSuchMethodException e) {
Log.e("findLibrary1", e.toString());
} catch (IllegalAccessException e) {
Log.e("findLibrary1", e.toString());
} catch (IllegalArgumentException e) {
Log.e("findLibrary1", e.toString());
} catch (InvocationTargetException e) {
Log.e("findLibrary1", e.toString());
} catch (Exception e) {
Log.e("findLibrary1", e.toString());
}
}
return result;
}
/**
* The function use to find so path for compatible android system
* Android OS <= 2.2
*/
public static String findLibrary2(Context context, String libName) {
String result = null;
ClassLoader classLoader = (context.getClassLoader());
if (classLoader != null) {
try {
Method findLibraryMethod = classLoader.getClass().getDeclaredMethod("findLibrary", new Class<?>[] { String.class });
if (findLibraryMethod != null) {
if (!findLibraryMethod.isAccessible()) {
findLibraryMethod.setAccessible(true);
}
Object objPath = findLibraryMethod.invoke(classLoader, new Object[] { libName });
if (objPath != null && objPath instanceof String) {
result = (String) objPath;
}
}
} catch (NoSuchMethodException e) {
Log.e("findLibrary2", e.toString());
} catch (IllegalAccessException e) {
Log.e("findLibrary2", e.toString());
} catch (IllegalArgumentException e) {
Log.e("findLibrary2", e.toString());
} catch (InvocationTargetException e) {
Log.e("findLibrary2", e.toString());
} catch (Exception e) {
Log.e("findLibrary2", e.toString());
}
}
return result;
}