Android studio 使用Cmake完成C/C++ 的使用以及生成so文件

本文介绍了Android Studio 2.2及以上版本如何使用CMake进行C/C++开发,包括新建项目时如何选择C++支持,项目结构的变化,以及CMakeLists.txt和build.gradle的配置讲解,特别是CMakeLists.txt中的关键指令解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


今天,简单讲讲android中关于Cmake进行NDK编程的相关知识。


Android studio 2.2版本以后对C/C++的支持可以说很方便了,当然官方推荐使用Cmake完成对C/C++的支持

2.2版本以上的同学新建一个项目就知道了,步骤如下:

File -> New -> New Project,如下图:



然后勾选Include C++ support,一直next ,最后Finish以后,项目就出现了,和一般的项目略有不同,其实只要多了几个文件,而已:



1:目录下多了个CmakeLists.txt文件

2:src目录下多了一个cpp目录,里面有个.cpp文件,C++文件都是以.cpp结尾的。

3:就是build.gradle内容中添加了几行配置

一一解读一下,先来看看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.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.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.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
这里有设置Cmake版本啊,引用的cpp文件的路径啊等等,如果自己引用的话 其实值需要修改引用的cpp路径即可,

其他的暂时不需要动

看下native_lib.cpp文件内容:

#include <jni.h>
#include <string>

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



很简单 其实就是输出Hello from C++这段文字:

再看下build.gradle的配置改变:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.process.main.myapplication"
        minSdkVersion 22
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
    testCompile 'junit:junit:4.12'
}


主要多了两个地方的改变:

1:defaultConfig中添加:

externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }

2:在android{}中添加:

externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

其实也就是引用CmakeLists.txt文件。

现在重点讲讲CmakeLists.txt里面的代码的含义。

这里写图片描述


这里写图片描述


以上的CmakeLists.txt对于与图片的目录结构,可以看出主要是add_liberary里写入需要编译的C文件的路径,其余的基本不用修改。

还有就是要是在add_liberary中添加.cpp文件的话记得路径,如果你的CMakeLists和你的.cpp在一个问价夹下就不用管路径。


Android studio 使用Cmake完成C/C++ 的使用以及生成so文件就讲完了。

就这么简单。



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值