Invoking the dynamic library file (*.so) in Android is done in a jni way, and often when you call the so file in an APK or jar package, you package the corresponding so file into the APK or jar package, the project catalog:
Problems with the above way:
1, lack of flexibility compared to static loading (not static loading), can load the so file binding dead;
2, but so many or very large files, will cause the corresponding APK and jar package is large;
3, unable to dynamically update so files;
The provided API for loading so files in Android:
void System.load (String pathName);
Description
1, pathName: filename + file path strength;
2, the method is called successfully after the export function in the so file will be inserted into the system provided by a mapping table (type map);
See the above on System.load (String pathName), the function of the description can someone will think of the so file into a specified directory and then through the parameters PathName Direct reference to the directory of the path and the corresponding so file problem does not solve it?
Here is a question that is ignored, that is, system.load can only load two directory path under the so file:
1,/system/lib;
2, the installation package of the road, that is:/data/data/<packagename>/...
And these two road strength is to have the right to protect not direct access;
Problem Solving methods:
Download so files from the network first to the phone directory (for example:/test/device/test.so) –> load test.so into memory (Bytearrayoutputstream) –> and then save to the pair with the installation package directory;
The specific code is as follows:
try {
String LocalPath = environment.getexternalstoragedirectory () + path;
LOG.V (TAG, "Lazybandinglib localPath:" + LocalPath);
string[] tokens = mpatterns.split (path);
if (null = = Tokens | | tokens.length <= 0
|| TOKENS[TOKENS.LENGTH-1] = = "") {
LOG.V (TAG, "Illegal file path! ");
return-3;
}
Open an input stream
File InFile = new file (LocalPath);
Determine if the file to be loaded exists
if (!infile.exists ()) {
Download Remote driver files
LOG.V (TAG, Infile.getabsolutepath () + "is not fond!");
return 1;
}
FileInputStream fis = new FileInputStream (inFile);
File dir = context.getdir ("Libs", context.mode_private);
Get the driver file output stream
File Sofile = new file (dir, tokens[tokens.length-1]);
if (!sofile.exists ()) {
LOG.V (TAG, "# #" + sofile.getabsolutepath () + "is NOT exists");
FileOutputStream fos = new FileOutputStream (sofile);
LOG.V (TAG, "FileOutputStream:" + fos.tostring () + ", Tokens:"
+ tokens[tokens.length-1]);
byte array output stream, written to memory (RAM)
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len =-1;
while (len = fis.read (buffer))! =-1) {
Baos.write (buffer, 0, Len);
}
From memory to writing to specific files
Fos.write (Baos.tobytearray ());
Close file stream
Baos.close ();
Fos.close ();
}
Fis.close ();
LOG.V (TAG, "# # System.load start");
Load Peripheral Drivers
System.load (Sofile.getabsolutepath ());
LOG.V (TAG, "# # System.load End");
return 0;
} catch (Exception e) {
LOG.V (TAG, "Exception" + e.getmessage ());
E.printstacktrace ();
return-1;
}
Android Dynamic load so file