标签:
项目需要,使用NDK和java开发库,这样就打包完后两个独立的个体:so和jar包,提供给客户不仅麻烦,还有两个版本要维护,因此想到把so放入jar包中。
做法很简单就是把so库拷入指定目录下,然后再从此目录load到jar包中。
传统load so方法:将so放入至libs/armeabi目录(arm架构),然后用以下代码load so库即可。
static { System.loadLibrary("native-jni"); }
so放入jar包中,则需将so库后缀去掉,放入assets资源文件夹下,然后以下面代码load。
public static boolean loadUrlSoLib(Context context){ String PACKAGE_NAME = context.getPackageName(); String DB_URL_LIB = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME+"/"+"lib"; String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME+"/"+"libs"+"/"+"native-jni.so"; String DB_URL = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME+"/"+"libs"; if(Util.IS_LOGOUT){ Log.e("drvLib", "new File(DB_URL_LIB).lastModified()- System.currentTimeMillis()="+(new File(DB_URL_LIB).lastModified()- System.currentTimeMillis())); } if(!(new File(DB_PATH).exists()) || (new File(DB_URL_LIB).exists() && (System.currentTimeMillis() - new File(DB_URL_LIB).lastModified() < 6000))) { try{ InputStream is = context.getAssets().open("libnative-jni"); //libnative-jni 生成的SO去后缀名称
String command = "chmod 777 " + DB_URL; Runtime runtime = Runtime.getRuntime(); runtime.exec(command); File dir = new File(DB_URL); if (!dir.exists()) { dir.mkdir(); } FileOutputStream fos = new FileOutputStream(DB_PATH,false); byte[] buffer = new byte[1024*4]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); }catch(Exception e){ e.printStackTrace(); return false; } } System.load(DB_PATH); return true; }
标签:
原文地址:http://www.cnblogs.com/myPersonalTailor/p/4206038.html