首先来看下我们是如何使用自己的动态库:
public class MyActivity extends Activity {
/** Called when the activity is first created. */
//WebView webView;
public native int sum(int i, int j);
static{
System.loadLibrary("test");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("这是一个测试Android的helloWorld! sum() = " + sum(2, 3));
setContentView(tv);
}
}
@system.java
public static void loadLibrary(String libName) {
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
}
@Runtime.java
private Runtime(){
String pathList = System.getProperty("java.library.path", ".");
String pathSep = System.getProperty("path.separator", ":");
String fileSep = System.getProperty("file.separator", "/");
mLibPaths = pathList.split(pathSep);
// Add a '/' to the end so we don't have to do the property lookup
// and concatenation later.
for (int i = 0; i < mLibPaths.length; i++) {
if (!mLibPaths[i].endsWith(fileSep)) {
mLibPaths[i] += fileSep;
}
}
}...
void loadLibrary(String libraryName, ClassLoader loader) {
if (loader != null) {
String filename = loader.findLibrary(libraryName);
if (filename == null) {
throw new UnsatisfiedLinkError("Couldn't load " + libraryName + ": " +
"findLibrary returned null");
}
String error = nativeLoad(filename, loader);
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
return;
}
String filename = System.mapLibraryName(libraryName);
List candidates = new ArrayList();
String lastError = null;
for (String directory : mLibPaths) {
String candidate = directory + filename;
candidates.add(candidate);
if (new File(candidate).exists()) {
String error = nativeLoad(candidate, loader);
if (error == null) {
return; // We successfully loaded the library. Job done.
}
lastError = error;
}
}
if (lastError != null) {
throw new UnsatisfiedLinkError(lastError);
}
throw new UnsatisfiedLinkError("Library " + libraryName + " not found; tried " + candidates);
}
@System.java
public static String getProperty(String prop, String defaultValue) {
if (prop.isEmpty()) {
throw new IllegalArgumentException();
}
return getProperties().getProperty(prop, defaultValue);
}
public static Properties getProperties() {
if (systemProperties == null) {
initSystemProperties();
}
return systemProperties;
}
private static void initSystemProperties() {
VMRuntime runtime = VMRuntime.getRuntime();
Properties p = new Properties();
String projectUrl = "http://www.android.com/";
String projectName = "The Android Project";
p.put("java.boot.class.path", runtime.bootClassPath());
p.put("java.class.path", runtime.classPath());
// None of these four are meaningful on Android, but these keys are guaranteed
// to be present for System.getProperty. For java.class.version, we use the maximum
// class file version that dx currently supports.
p.put("java.class.version", "50.0");
p.put("java.compiler", "");
p.put("java.ext.dirs", "");
p.put("java.version", "0");
p.put("java.home", getenv("JAVA_HOME", "/system"));
p.put("java.io.tmpdir", "/tmp");
p.put("java.library.path", getenv("LD_LIBRARY_PATH"));
上面的系统环境变量“LD_LIBRARY_PATH”,在init.rc文件中进行配置
export LD_LIBRARY_PATH/vendor/lib:/system/lib