Sometimes, we decompile apk to get a so library, if directly use this so library, must use the original so library the same package name, in order to use.
If you do this, you will know that you have violated the copyright of someone else. In order to achieve the purpose of confusion, we can write a so library to call someone else's so library, that is, the other people's so library under a path to root, the C + + language to call the so library. For example, I get an apk and decompile this apk to see the following code:
[HTML]View Plaincopy
- STATIC {  
- try {
- system.loadlibrary (" Nativeexampleactivity ");
- } catch (throwable t) {
-     }  
- }
- public native int addfunction (int a, int b);
- Public native string getstring (string name);
Obviously, this so library is libnativeexampleactivity.so, with two native functions AddFunction and getstring in the library.
Although we know these two native functions, we cannot use them directly, because the real function names of the two native functions in the so library are not addfunction and GetString,
It has a package name before the native function name, so you must use the NM command to see the function names inside the so library.
command to display the so library function:
Nm-a libnativeexampleactivity.so
Or
Nm-d libnativeexampleactivity.so
So we see the main information in so library:
Java_org_natives_example_nativeexampleactivity_addfunction
Java_org_natives_example_nativeexampleactivity_getstring
See no, in front of the AddFunction function also has the package name, this is why directly use someone else's so library, must use the original packages name!
OK, now is how to call these two functions, 4 steps to complete.
1. Create a project with Eclipse
[HTML]View Plaincopy
- Package So.hello;
- Import android.app.Activity;
- Import Android.os.Bundle;
- public class Sohelloactivity extends Activity {
- /** called when the activity is first created. */
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- }
- static {
- try {
- System.loadlibrary ("Sohello");
- } catch (Throwable t) {
- }
- }
- public native int AddFunction1 (int a, int b);
- Public native string getString1 (string name);
- }
2. In the terminal entry to the project path sohello/bin/classes, execute the command:
[Email protected]:~/workspace/sohello/bin/classes$ javah-jni so.hello.SoHelloActivity
A file is generated under the Sohello/bin/classes directory So_hello_sohelloactivity.h
[HTML]View Plaincopy
- /* Don't EDIT this file-it are machine generated */
- #include <jni.h>
- /* Header for class so_hello_sohelloactivity */
- #ifndef _included_so_hello_sohelloactivity
- #define _included_so_hello_sohelloactivity
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class:so_hello_sohelloactivity
- * Method:addfunction1
- * Signature: (II) I
- */
- Jniexport Jint Jnicall Java_so_hello_sohelloactivity_addfunction1
- (JNIEnv *, Jobject, Jint, Jint);
- /*
- * Class:so_hello_sohelloactivity
- * METHOD:GETSTRING1
- * Signature: (ljava/lang/string;) ljava/lang/string;
- */
- Jniexport jstring Jnicall java_so_hello_sohelloactivity_getstring1
- (JNIEnv *, Jobject, jstring);
- #ifdef __cplusplus
- }
- #endif
- #endif
3. Write a so_hello_sohelloactivity.cpp file
[HTML]View Plaincopy
- #include "So_hello_sohelloactivity.h"
- #include <stdlib.h>
- #include <fcntl.h>
- #include <android/log.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <dlfcn.h>
- void *filehandle = NULL;
- Jint (*addfunc) (jnienv *,jobject,jint,jint) = NULL;
- Jstring (*getstringfunc) (jnienv *, jobject, jstring) = NULL;
- Jniexport Jint Jnicall Java_so_hello_sohelloactivity_addfunction1
- (jnienv *env, Jobject obj, jint A, jint b);
- {
- Jint mun = 0;
- Put the libnativeexampleactivity in the root/system/lib/directory beforehand.
- filehandle = dlopen ("/system/lib/libnativeexampleactivity.so", Rtld_lazy);
- if (filehandle)
- {
- Addfunc = (jint (*) (jnienv *,jobject,jint,jint)) Dlsym (FileHandle, "Java_org_natives_example_ Nativeexampleactivity_addfunction ");
- if (Addfunc)
- mun = addfunc (env, obj, a, b);
- Dlclose (FileHandle);
- filehandle = NULL;
- }
- Return Mun
- }
- /*
- * Class:so_hello_sohelloactivity
- * METHOD:GETSTRING1
- * Signature: (ljava/lang/string;) ljava/lang/string;
- */
- Jniexport jstring Jnicall java_so_hello_sohelloactivity_getstring1
- (JNIEnv *, Jobject, jstring name)
- {
- jstring mun = 0;
- Put the libnativeexampleactivity in the root/system/lib/directory beforehand.
- filehandle = dlopen ("/system/lib/libnativeexampleactivity.so", Rtld_lazy);
- if (filehandle)
- {
- Getstringfunc = (jstring (*) (jnienv *,jobject,jstring)) Dlsym (FileHandle, "Java_org_natives_example_ Nativeexampleactivity_getstring ");
- if (Getstringfunc)
- {
- mun = getstringfunc (env, obj, name);
- }
- Dlclose (FileHandle);
- filehandle = NULL;
- }
- Return Mun
- }
4. Writing android.mk
[HTML]View Plaincopy
- Local_path : = $ (call My-dir)
- Include $ (clear_vars)
- Local_ldlibs : =-llog
- Local_c_includes + = System/core/include/cutils
- Local_shared_libraries : = \
- LIBDL \
- Libcutils
- Local_prelink_module : = false
- Local_module : = Libsohello
- Local_module_tags : = Optional
- Local_src_files : = so_hello_sohelloactivity.cpp
- Local_ldlibs : =-l$ (sysroot)/usr/lib-llog
- Include $ (build_shared_library)
The libsohello.so library can be generated by compiling so_hello_sohelloactivity.cpp with the mm command.
Then this so library can be used!
Review:
The main use of Dlopen, Dlsym, dlclose three functions to load so library:
void *filehandle = NULL;
Jint (*addfunc) (jnienv *,jobject,jint,jint) = NULL;
Jint mun = 0
Put the libnativeexampleactivity in the root/system/lib/directory beforehand.
FileHandle = Dlopen ("/system/lib/libnativeexampleactivity.so", Rtld_lazy);
if (filehandle)
{
Addfunc = (Jint (*) (jnienv *,jobject,jint,jint)) Dlsym (FileHandle, "java_org_natives_example_nativeexampleactivity_ AddFunction ");
if (Addfunc)
mun = Addfunc (env, obj, a, b);
Dlclose (FileHandle);
FileHandle = NULL;
}
Android calls the so library using C + + +