Androidstudio使⽤Cmake完成CC++的使⽤以及⽣成so⽂件Android studio 2.2版本以后对C/C++的⽀持可以说很⽅便了,当然官⽅推荐使⽤Cmake完成对C/C++的⽀持
2.2版本以上的同学新建⼀个项⽬就知道了,步骤如下:
File -> New -> New Project,如下图:
然后勾选Include C++ support,⼀直next ,最后Finish以后,项⽬就出现了,和⼀般的项⽬略有不同,其实只要多了⼏个⽂件,⽽已:
1:⽬录下多了个⽂件
2:src⽬录下多了⼀个cpp⽬录,⾥⾯有个.cpp⽂件,C++⽂件都是以.cpp结尾的。3:就是adle内容中添加了⼏⾏配置
⼀⼀解读⼀下,先来看看 ⽂件⾥⾯的内容是什么:
# For more information about using CMake with Android Studio, read the
# documentation: d.android/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake如何使用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++这段⽂字:
再看下adle的配置改变:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.application"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "st.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(''), 'proguard-rules.pro' }
}
externalNativeBuild {
cmake {
path ""
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.st.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.straint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
}
主要多了两个地⽅的改变:
1:defaultConfig中添加:
externalNativeBuild {
cmake {
cppFlags ""
}
}
2:在android{}中添加:
externalNativeBuild {
cmake {
path ""
}
}
其实也就是引⽤⽂件。
说到这⾥,⼤致知道C++项⽬的⼤致结构了。那么原始项⽬怎么添加C++呢,步骤如下:
1,现在src/main⽬录下新建cpp⽬录和java⽬录同级,然后在cpp⽬录中新建⼀个.cpp⽂件,这⾥以native-lib.cpp为例⼦直接copy进去2,右键项⽬(选中项⽬,然后右键)如下:
选择第⼆个Link C++ Project with Gradle,出现以下界⾯:
关于Project Path是选择⽂件的路径,这⾥先把之前的⽂件直接copy过来,然后相应的修改即可:
选择CmakeLists⽂件然后点击OK按钮。修改⽂件中的cpp⽂件引⽤⽬录即可(add_library {第三⾏})。
接下来,在java代码中调⽤C++代码啦,看下MainActivity中的代码:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论