Android calls the so library using C + +

Source: Internet
Author: User

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
    1. STATIC {  
    2.     try  {  
    3.         system.loadlibrary (" Nativeexampleactivity ");   
    4.     } catch  (throwable t)  {  
    5.     }  
    6. }  
    7. public native int addfunction (int a, int b);   
    8. 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
  1. Package So.hello;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. public class Sohelloactivity extends Activity {
  5. /** called when the activity is first created. */
  6. @Override
  7. public void OnCreate (Bundle savedinstancestate) {
  8. Super.oncreate (savedinstancestate);
  9. Setcontentview (R.layout.main);
  10. }
  11. static {
  12. try {
  13. System.loadlibrary ("Sohello");
  14. } catch (Throwable t) {
  15. }
  16. }
  17. public native int AddFunction1 (int a, int b);
  18. Public native string getString1 (string name);
  19. }

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
  1. /* Don't EDIT this file-it are machine generated */
  2. #include <jni.h>
  3. /* Header for class so_hello_sohelloactivity */
  4. #ifndef _included_so_hello_sohelloactivity
  5. #define _included_so_hello_sohelloactivity
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class:so_hello_sohelloactivity
  11. * Method:addfunction1
  12. * Signature: (II) I
  13. */
  14. Jniexport Jint Jnicall Java_so_hello_sohelloactivity_addfunction1
  15. (JNIEnv *, Jobject, Jint, Jint);
  16. /*
  17. * Class:so_hello_sohelloactivity
  18. * METHOD:GETSTRING1
  19. * Signature: (ljava/lang/string;) ljava/lang/string;
  20. */
  21. Jniexport jstring Jnicall java_so_hello_sohelloactivity_getstring1
  22. (JNIEnv *, Jobject, jstring);
  23. #ifdef __cplusplus
  24. }
  25. #endif
  26. #endif


3. Write a so_hello_sohelloactivity.cpp file

[HTML]View Plaincopy
  1. #include "So_hello_sohelloactivity.h"
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <android/log.h>
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #include <dlfcn.h>
  8. void *filehandle = NULL;
  9. Jint (*addfunc) (jnienv *,jobject,jint,jint) = NULL;
  10. Jstring (*getstringfunc) (jnienv *, jobject, jstring) = NULL;
  11. Jniexport Jint Jnicall Java_so_hello_sohelloactivity_addfunction1
  12. (jnienv *env, Jobject obj, jint A, jint b);
  13. {
  14. Jint mun = 0;
  15. Put the libnativeexampleactivity in the root/system/lib/directory beforehand.
  16. filehandle = dlopen ("/system/lib/libnativeexampleactivity.so", Rtld_lazy);
  17. if (filehandle)
  18. {
  19. Addfunc = (jint (*) (jnienv *,jobject,jint,jint)) Dlsym (FileHandle, "Java_org_natives_example_  Nativeexampleactivity_addfunction ");
  20. if (Addfunc)
  21. mun = addfunc (env, obj, a, b);
  22. Dlclose (FileHandle);
  23. filehandle = NULL;
  24. }
  25. Return Mun
  26. }
  27. /*
  28. * Class:so_hello_sohelloactivity
  29. * METHOD:GETSTRING1
  30. * Signature: (ljava/lang/string;) ljava/lang/string;
  31. */
  32. Jniexport jstring Jnicall java_so_hello_sohelloactivity_getstring1
  33. (JNIEnv *, Jobject, jstring name)
  34. {
  35. jstring mun = 0;
  36. Put the libnativeexampleactivity in the root/system/lib/directory beforehand.
  37. filehandle = dlopen ("/system/lib/libnativeexampleactivity.so", Rtld_lazy);
  38. if (filehandle)
  39. {
  40. Getstringfunc = (jstring (*) (jnienv *,jobject,jstring)) Dlsym (FileHandle, "Java_org_natives_example_  Nativeexampleactivity_getstring ");
  41. if (Getstringfunc)
  42. {
  43. mun = getstringfunc (env, obj, name);
  44. }
  45. Dlclose (FileHandle);
  46. filehandle = NULL;
  47. }
  48. Return Mun
  49. }


4. Writing android.mk

[HTML]View Plaincopy
  1. Local_path : = $ (call My-dir)
  2. Include $ (clear_vars)
  3. Local_ldlibs : =-llog
  4. Local_c_includes + = System/core/include/cutils
  5. Local_shared_libraries : = \
  6. LIBDL \
  7. Libcutils
  8. Local_prelink_module : = false
  9. Local_module : = Libsohello
  10. Local_module_tags : = Optional
  11. Local_src_files : = so_hello_sohelloactivity.cpp
  12. Local_ldlibs : =-l$ (sysroot)/usr/lib-llog
  13. 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 + + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.