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)

相关推荐
miao_zz6 分钟前
基于react native的锚点
android·react native·react.js
安卓美女9 分钟前
Android自定义View性能优化
android·性能优化
Dingdangr1 小时前
Android中的四大组件
android
mg6682 小时前
安卓玩机工具-----无需root权限 卸载 禁用 删除当前机型app应用 ADB玩机工具
android
安卓机器2 小时前
Android架构组件:MVVM模式的实战应用与数据绑定技巧
android·架构
码龄3年 审核中3 小时前
MySQL record 05 part
android·数据库·mysql
服装学院的IT男3 小时前
【Android 13源码分析】WindowContainer窗口层级-1-初识窗口层级树
android
技术无疆4 小时前
Hutool:Java开发者的瑞士军刀
android·java·开发语言·ide·spring boot·gradle·intellij idea
qluka7 小时前
Android 应用安装-提交阶段
android
ansondroider7 小时前
Android MediaPlayer + GLSurfaceView 播放视频
android·opengl·mediaplayer·glsurfaceview