在android studio下使用cmake 生成so库

在android studio下使用cmake 生成so库

编译环境
Android studio 版本3.4.1
cmake版本3.10.2.4988404
ndk版本21.1.6352462
代码编写
1、新建调用native的代码

public class MyTest {
    static{
        System.loadLibrary("native-lib");
    }
    public static native String stringFroMJNI();
}

这里只需要注意library的名称,要跟生成的库名称一致也就是CMakeLists.txt里面的名字一致
2、根据java代码生成.h文件2、根据java代码生成.h文件@[TOC]
在Terminal窗口中,进入到java代码所在的目录,其实进入哪个目录无所谓,这个目录是生成编译后.h的文件目录,然后使用以下命令

javah -jni -classpath 生成的class的目录com.自己的包名.MyTest 

会生成一个.h文件
3、生成cpp文件
在src/main下新建cpp目录,在cpp下新建c++文件,命名为native-lib.cpp将上一步生成的.h文件里的内容拷贝到cpp文件夹里面,调用native方法返回一个字符串,需要这样修改
添加#include
stringFroMJNI方法添加方法体

			std::string hello = "Hello from C++";
            return env->NewStringUTF(hello.c_str());

例:我的cpp修改为如下

//
// Created by 63401 on 2020/12/1.
//
#include <jni.h>
#include <string>


#ifndef _Included_com_life2smile_scrollpicker_library_util_MyTest
#define _Included_com_life2smile_scrollpicker_library_util_MyTest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_life2smile_scrollpicker_library_util_MyTest
 * Method:    stringFroMJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_life2smile_scrollpicker_library_util_MyTest_stringFroMJNI
  (JNIEnv *env, jclass){
                        std::string hello = "Hello from C++";
                        return env->NewStringUTF(hello.c_str());
                    };

#ifdef __cplusplus
}
#endif
#endif

两点需要注意,Java_com_*****这一串需要是自己的,里面的变量JNIEnv *env返回用到了env
4、增加CMakeLists.txt
我的这个文件是直接放到了app目录下,新手一定要放到这个目录,不然不知道会出什么问题,我在这方面是新手,所以我放到这个,目录下了,源码如下

cmake_minimum_required(VERSION 3.4.1)


add_library(
             native-lib

             SHARED

             src/main/cpp/native-lib.cpp)

# Specifies a path to native header files.

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

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

              # Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       native-lib

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

add_library中引入cpp目录下的cpp
find_library和target_link_libraries固定写法
5、修改app下的build.gradle

defaultConfig {
        ****
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters "armeabi-v7a","arm64-v8a","x86","x86_64"
            }
        }
    }
externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

然后build-》make project即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值