Android逆向:AndroidStudio JNI编写SO

本文是在这篇博客的基础上开发的so。
另外,也可以自己从头开发Native C++应该更简单一些,不会有这么多坑。

JNI(Java Native Interface),他是java平台的特性,不是安卓系统提供的。他定义了一些JNI函数,来让开发者可以通过调用这些函数来实现java代码调用C/C++代码。

C++实现

新建java 类文件:src\main\java\com\lilongsy\TestJNI.java,用Java实现逻辑。
1

package com.lilongsy;

public class TestJNI {
    static {
        System.loadLibrary("JniTest");
    }
    public static native String sayHello();
}

在java目录下,运行javah命令,生成com_lilongsy_TestJNI.h头文件:

D:\java-test\MyApplication\app\src\main\java>javah com.lilongsy.TestJNI
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_lilongsy_TestJNI */

#ifndef _Included_com_lilongsy_TestJNI
#define _Included_com_lilongsy_TestJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_lilongsy_TestJNI
 * Method:    sayHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_lilongsy_TestJNI_sayHello
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

在main下新建jni文件夹
1
com_lilongsy_TestJNI.h头文件剪切到jni文件夹下,并新建main.cpp文件,其代码如下:

#include "com_lilongsy_TestJNI.h"
JNIEXPORT jstring JNICALL Java_com_lilongsy_TestJNI_sayHello
        (JNIEnv *env, jclass obj) {
  return env->NewStringUTF((char *)"登录成功_JNI");
};

设置CMake编译环境

在AndroidStudio File > Settings > Android SDK > SDK Tools下,安装NDK、CMake、LLDB(国内需要单独下载到cmake同级目录,并用lldb小写)。
1

1
另外一种方法是手动下载NDK,下载地址:https://developer.android.google.cn/ndk/downloads/,然后在项目目录下的local.properties设置ndk.dir,我这里设置如下:

ndk.dir=D\:\\android\\android-ndk-r21b
sdk.dir=D\:\\android\\android-sdk

查看SDK是否设置成功:
1

在APP下新建CMakeLists.txt文件:
1
其内容如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
#CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        # 设置so文件名称.
        JniTest

        # Sets the library as a shared library.
        SHARED
        # 设置这个so文件为共享.

        # Provides a relative path to your source file(s).
        # 设置这个so文件为共享.
        src/main/jni/main.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        # 制定目标库.
        JniTest

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib} )

配置build.gradle

为了自动构建时,调用cmake,在APP>build.gradleandroid加入如下代码:

externalNativeBuild {
    cmake {
        path file('CMakeLists.txt')
    }
}

为了让cmake生成多个abi下的so文件,在在APP>build.gradledefaultConfig加入如下代码:

externalNativeBuild {
    cmake {
        cppFlags ""
        //生成多个版本的so文件
        abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
    }
}

重构(rebuild project)

build下,rebuild project
1
会在\app\build\intermediates\merged_native_libs目录下生成不同abi的so文件。
1

调用so

在app\src\main目录下新建jniLIB文件夹,并把上文生成的lib下的所有内容都复制到jniLIB下:
1
在MainActivity,加入如下代码,测试TestJNI.sayHello()的返回值:

Toast.makeText(MainActivity.this, TestJNI.sayHello(), Toast.LENGTH_SHORT).show();

运行APK,登录成功会有Toast提示:登录成功_JNI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小龙在山东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值