Android 如何生成 so 库

前言

Android 开发中难免会使用到 jni,实践中虽然简单,但也还是有不少坑点,本文将十分详细介绍在生成 so 库过程中细节。

步骤

创建 Native C++ 项目

这里新建了一个名为 SoTest 的项目,包名为 com.me.sotest,这里强调一下包名,因为大部分的坑点都出在包名上了。

系统为我们自动生成了一个 MainActivity.java 类。并在该类里加载了 sotest 库,然后调用了 stringFromJNI() 方法。

java 复制代码
package com.me.sotest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.me.sotest.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'sotest' library on application startup.
    static {
        System.loadLibrary("sotest");
    }

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Example of a call to a native method
        TextView tv = binding.sampleText;
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'sotest' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

该 native 方法是定义在 native-lib.cpp 中的。

C++ 复制代码
#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_me_sotest_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

构建得到 so 文件

当 build 后,会在SoTest\app\build\intermediates\merged_native_libs\debug\mergeDebugNativeLibs\out\lib\arm64-v8a 路径下生成一个 libsotest.so 文件,注意这里的 cpu 架构是 arm64-v8a。

目前看起来一切都很完美,成功的获得了 so 文件。但我们仔细看看这个 jni 方法的签名 Java_com_me_sotest_MainActivity_stringFromJNIcom_me_sotest 对应的是我们的包名(com.me.sotest),MainActivity 显然是对应类名。

当调用方使用我们的 jni 方法时,为了与我们的方法签名对应,就只能在包为 com.me.sotest 且类名为 MainActivity 的类里使用,这就不太科学,MainActivity 是有特殊含义的,我们不如把 jni 方法专门放到另外的类里去。

新增 JniMethod 类

现在我们新增一个 java 类叫做 JniMethod。

java 复制代码
package com.me.sotest;

public class JniMethod {
    // Used to load the 'sotest' library on application startup.
    static {
        System.loadLibrary("sotest");
    }

    public static native String stringFromJNI();
}

然后可以让 Android Studio 帮我们生成 jni 函数的声明,我们再补全定义即可,此时函数的签名变为了 Java_com_me_sotest_JniMethod_stringFromJNI,看起来似乎更合适。

C++ 复制代码
#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_me_sotest_JniMethod_stringFromJNI(JNIEnv *env, jclass clazz) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

对应的 MainActivity 中的调用改动如下,跑了一下没问题,且也得到了一个 libsotest.so

java 复制代码
TextView tv = binding.sampleText;
tv.setText(JniMethod.stringFromJNI());

至此,so 文件的生成过程就结束了,下一节我们将测试我们的 so 文件能否正常被使用。

Android 如何使用 so 库 - 掘金 (juejin.cn)

相关推荐
不吃凉粉29 分钟前
Android Studio USB串口通信
android·ide·android studio
zhangphil31 分钟前
android studio设置大内存,提升编译速度
android·android studio
编程乐学2 小时前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业
大熊的瓜地3 小时前
Android automotive 框架
android·android car
私人珍藏库4 小时前
[Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
android·时钟
珹洺5 小时前
Java-Spring入门指南(二十七)Android Studio 第一个项目搭建与手机页面模拟器运行
java·spring·android studio
消失的旧时光-19436 小时前
ScheduledExecutorService
android·java·开发语言
小糖学代码6 小时前
MySQL:14.mysql connect
android·数据库·mysql·adb
怪兽20148 小时前
请谈谈什么是同步屏障?
android·面试
帅锅锅0079 小时前
SeLinux 全面详解
android·linux