Android源码中编译自己的so库

Android源码中编译自己的so库1,首先看看这边文章:http://www.linuxidc.com/Linux/2013-02/79007.htm2,Android使用C/C++调用SO库 有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用。这样人家反编译你的apk,就知道你侵犯了人家的版权。为了达到混淆的目的,我们可以再写一个so库

大家好,又见面了,我是你们的朋友全栈君。

1,首先看看这边文章:http://www.linuxidc.com/Linux/2013-02/79007.htm

2 ,Android使用C/C++调用SO库 

有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用。
这样人家反编译你的apk,就知道你侵犯了人家的版权。为了达到混淆的目的,我们可以再写一个so库调用人家的so库,即把人家的so库放到root的某个路径下,用c/c++语言调用这个so库。比如说,我得到一个APK,反编译这个APK看到下面的代码:

[html] 
view plain
copy

  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);  

很明显,这个so库是libNativeExampleActivity.so, 库里面有两个native函数addFunction和getString。


虽然知道了这两个native函数,但是我们还不能直接使用,因为这两个native函数在so库里面的真实函数名不是addFunction和getString,


它在native函数名之前还有包名,所以必须使用nm命令,查看so库里面的函数名。


显示so库函数的命令:


nm -A libNativeExampleActivity.so


或者


nm -D libNativeExampleActivity.so


这样我们看到so库里的主要信息:


Java_org_natives_example_NativeExampleActivity_addFunction


Java_org_natives_example_NativeExampleActivity_getString


看到没有,在addFunction函数前面还有包名,这就是为什么直接使用人家的so库的时候,一定要使用原来的package名字!


好了,现在是怎么调用这两个函数了,4个步骤完成。


1.用Eclipse创建一个项目

[html] 
view plain
copy

  1. package so.hello;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class SoHelloActivity extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13.     static {  
  14.         try {  
  15.             System.loadLibrary(“soHello”);  
  16.         } catch (Throwable t) {  
  17.         }  
  18.     }  
  19.     public native int addFunction1(int a, int b);  
  20.     public native String getString1(String name);  
  21. }  

2.在终端进入到项目的路径soHello/bin/classes,执行命令:


guo@guo-desktop:~/workspace/soHello/bin/classes$ javah -jni so.hello.SoHelloActivity


在soHello/bin/classes目录下会生成一个文件so_hello_SoHelloActivity.h

[html] 
view plain
copy

  1. /* DO NOT EDIT THIS FILE – it is machine generated */  
  2. #include <jni.h>  
  3. /* Header for class so_hello_SoHelloActivity */  
  4.   
  5. #ifndef _Included_so_hello_SoHelloActivity  
  6. #define _Included_so_hello_SoHelloActivity  
  7. #ifdef __cplusplus  
  8. extern “C” {  
  9. #endif  
  10. /*  
  11.  * Class:     so_hello_SoHelloActivity  
  12.  * Method:    addFunction1  
  13.  * Signature: (II)I  
  14.  */  
  15. JNIEXPORT jint JNICALL Java_so_hello_SoHelloActivity_addFunction1  
  16.   (JNIEnv *, jobject, jint, jint);  
  17.   
  18. /*  
  19.  * Class:     so_hello_SoHelloActivity  
  20.  * Method:    getString1  
  21.  * Signature: (Ljava/lang/String;)Ljava/lang/String;  
  22.  */  
  23. JNIEXPORT jstring JNICALL Java_so_hello_SoHelloActivity_getString1  
  24.   (JNIEnv *, jobject, jstring);  
  25.   
  26. #ifdef __cplusplus  
  27. }  
  28. #endif  
  29. #endif  



3.写一个so_hello_SoHelloActivity.cpp文件

[html] 
view plain
copy

  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.   
  9. void *filehandle = NULL;  
  10. jint (*addFunc)(JNIEnv *,jobject,jint,jint) = NULL;  
  11. jstring (*getStringFunc)(JNIEnv *, jobject, jstring) = NULL;  
  12.   
  13. JNIEXPORT jint JNICALL Java_so_hello_SoHelloActivity_addFunction1  
  14.   (JNIEnv *env, jobject obj, jint a, jint b);  
  15. {  
  16.     jint mun = 0;  
  17.     //事先把libNativeExampleActivity放到root/system/lib/目录下  
  18.     filehandle = dlopen(“/system/lib/libNativeExampleActivity.so”, RTLD_LAZY);  
  19.     if(filehandle)  
  20.     {  
  21.         addFunc = (jint (*)(JNIEnv *,jobject,jint,jint))dlsym(filehandle, “Java_org_natives_example_NativeExampleActivity_addFunction”);  
  22.         if(addFunc)  
  23.             mun = addFunc(env, obj, a, b);  
  24.         dlclose(filehandle);   
  25.         filehandle = NULL;  
  26.     }  
  27.     return mun  
  28. }  
  29.   
  30. /*  
  31.  * Class:     so_hello_SoHelloActivity  
  32.  * Method:    getString1  
  33.  * Signature: (Ljava/lang/String;)Ljava/lang/String;  
  34.  */  
  35. JNIEXPORT jstring JNICALL Java_so_hello_SoHelloActivity_getString1  
  36.   (JNIEnv *, jobject, jstring name)  
  37. {  
  38.     jstring mun = 0;  
  39.     //事先把libNativeExampleActivity放到root/system/lib/目录下  
  40.     filehandle = dlopen(“/system/lib/libNativeExampleActivity.so”, RTLD_LAZY);  
  41.     if(filehandle)  
  42.     {  
  43.         getStringFunc = (jstring (*)(JNIEnv *,jobject,jstring))dlsym(filehandle, “Java_org_natives_example_NativeExampleActivity_getString”);  
  44.         if(getStringFunc)  
  45.         {  
  46.             mun = getStringFunc(env, obj, name);  
  47.         }  
  48.         dlclose(filehandle);   
  49.         filehandle = NULL;  
  50.     }  
  51.     return mun  
  52. }  



4.编写Android.mk

[html] 
view plain
copy

  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_LDLIBS := -llog  
  6. LOCAL_C_INCLUDES += system/core/include/cutils  
  7. LOCAL_SHARED_LIBRARIES := \  
  8.     libdl \  
  9.     libcutils  
  10.   
  11. LOCAL_PRELINK_MODULE :false  
  12. LOCAL_MODULE      :libsoHello  
  13. LOCAL_MODULE_TAGS :optional  
  14. LOCAL_SRC_FILES   :so_hello_SoHelloActivity.cpp  
  15. LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog  
  16.   
  17. include $(BUILD_SHARED_LIBRARY)  



使用mm命令编译so_hello_SoHelloActivity.cpp,便可以生成libsoHello.so库。


然后这个so库就可以用啦!




综述:


这里主要使用了dlopen、dlsym、dlclose三个函数来加载so库:


void *filehandle = NULL;


jint (*addFunc)(JNIEnv *,jobject,jint,jint) = NULL;


jint mun = 0


//事先把libNativeExampleActivity放到root/system/lib/目录下


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;


}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150149.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • J2EE的13个规范之(二) JDBC 及其使用「建议收藏」

    J2EE的13个规范之(二) JDBC 及其使用

    2022年1月26日
    34
  • git提交空目录的方法

    git提交空目录的方法

    2021年10月17日
    40
  • windows操作系统定时关机和取消定时关机命令

    windows操作系统定时关机和取消定时关机命令windows操作系统定时关机和取消定时关机命令1、定时关机shutdown-s-t60(60表示定时60秒)2、取消定时关机shutdown-a(取消定时关机)…

    2022年5月15日
    45
  • c++中fstream是什么意思_汽车配置参数图文详解

    c++中fstream是什么意思_汽车配置参数图文详解在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,一,c++文件流的结构:‍1,几个文件流类名称:fstream,ifstream,ofstream,iofstream2,之间的关系:ifstream(inputfilestream)和ofstream(outpufilestream),ifstream默认以输入方式打开文件,而ofstream默认以输出方式打…

    2022年9月19日
    0
  • 计算机二级公共基础知识笔记

    计算机二级公共基础知识笔记计算机二级公共基础知识计算机系统考点一:计算机概述1.计算机的发展历程目前公认的第一台电子数字计算机是ENIAC,它于1946年在美国宾夕法尼亚大学研制成功。根据计算机本身采用的物理器件不同,将其发展分为4个阶段第一阶段是电子管计算机时代,时间为1946年到20世纪50年代第二阶段是晶体管计算机时代,时间为20世纪50年代后期到50世纪60年代中期第三阶段是中小规模集成电路计算机时代,时间是20世纪60年代中期到20世纪70年代初期第四阶段是大规模和超大规模集成电路计算机时代,时间是20

    2022年6月9日
    35
  • 51单片机IIC通信协议

    51单片机IIC通信协议/*——————————————————————————*@fileI2C.H*@authorByron(from3900@gmail.com)*@versionV1.0.0*@date05/12/2020*@brief51系列单片机I2C通信协议头文件*——————————————-

    2022年6月5日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号