在Android studio 3.2 版项目中使用cmake调用C/C++

本文操作环境:

win10/Android studio 3.2

1.环境配置

    在SDK Tools里选择 CMAKE/LLDB/NDK点击OK 安装这些插件.

 

2.创建CMakeLists.txt文件

  在Project 目录下,右键app,点击新建File文件,命名为CMakeLists.txt

点击OK,创建完毕!

 

3.配置文件

  在CMakeLists.txt文件里添加如下代码(具体含义自行百度):

# 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.

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.
             test_lib #.so库名 可自定义

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/test_lib.c ) #源文件所在目录

# 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.
                       test_lib #.so库名 可自定义

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

 

4.创建一个新的java类

在此类中添加代码

package com.example.administrator;

public class Test_lib {
    static {
        System.loadLibrary("test_lib");//加载.so库
    }

    public static native String getStr(String str);//调用C/C++接口函数
}

 

5.在main下面创建jni目录,创建test_lib.c文件,名字必须与CMakeLists.txt文件的源文件所在目录一致

 

6.右键app,点击Link C++ Project with Gradle

 显示如下,选择CMakeLists.txt文件所在路径,点击ok,等待构建完成.

构建完成后,build.gradle文件会自动生成一些配置,如下图:

 

7.回到Test_lib.java文件,选中getStr()函数,按下Alt+Enter,点击Create function...,如下图。

  此时会在test_lib.c文件里自动生成C/C++函数。接着就可以在.c文件里编写C/C++接口函数了。

然后Make Project成功后,会在如下目录生成.so文件.此时.so库生成成功,可随时调用了!

 

8.在其他java类中调用C/C++函数

   在要调用的java类中导入Test_lib包:

在需要调用的地方进行调用

 到此处就大功告成了!

初次使用CMake会觉得很繁琐,但是比传统的jni调用方式要方便很多,多用几次就会顺手了。

 

恕本人水平有限,如有错误还请不吝指出!

本文为作者原创,如需转载请注明出处!

 

 

 

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要在Android Studio编写C/C++代码,你需要进行以下步骤: 1. 创建一个新的Android Studio项目。 2. 在你的项目创建一个新的C/C++源文件。你可以使用菜单File->New->C/C++ Source File来创建源文件。 3. 在你的源文件编写C/C++代码。 4. 在你的项目创建一个新的JNI头文件。你可以使用菜单File->New->JNI/C++ Header File来创建头文件。 5. 在你的JNI头文件声明你的C/C++函数和变量。例如,如果你的C/C++源文件有一个函数叫做myFunction,你可以在你的JNI头文件这样声明它: ```c++ JNIEXPORT void JNICALL Java_com_example_myapp_MyClass_myFunction(JNIEnv *env, jobject obj); ``` 请注意,这里的Java_com_example_myapp_MyClass_myFunction是根据你的Java类和函数名称来自动生成的。你需要根据你的实际项目进行修改。 6. 在你的Java代码使用JNI接口调用你的C/C++函数。例如,如果你的Java类是MyClass,你可以在它的某个方法这样调用你的C/C++函数: ```java public void myMethod() { myFunction(); } ``` 7. 在你的项目配置NDK环境。你需要下载NDK并在你的项目配置NDK路径。你可以使用菜单File->Project Structure->SDK Location来配置NDK路径。 8. 在你的项目配置CMake。你需要创建一个CMakeLists.txt文件来告诉Android Studio如何编译你的C/C++代码。你可以使用以下模板来创建CMakeLists.txt文件: ``` cmake_minimum_required(VERSION 3.10.2) project(myproject C CXX) add_library(mylib SHARED mysource.cpp) target_link_libraries(mylib log) ``` 请注意,这里的mysource.cpp和mylib是根据你的实际项目进行修改。 9. 运行你的项目并测试你的C/C++代码。 请注意,以上步骤只是一个基本的示例,你需要根据你的实际项目需求进行修改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值