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 JNICALLjava_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文件。說到這里,大致知道C++項目的大致結構了。那么原始項目怎么添加C++呢,步驟如下:
1,現在src/main目錄下新建cpp目錄和java目錄同級,然后在cpp目錄中新建一個.cpp文件,這里以native-lib.cpp為例子直接copy進去
2,右鍵項目(選中項目,然后右鍵)如下:
選擇第二個Link C++ Project with Gradle,出現以下界面:
關于Project Path是選擇CmakeLists.txt文件的路徑,這里先把之前的CmakeLists.txt文件直接copy過來,然后相應的修改即可:
選擇CmakeLists文件然后點擊OK按鈕。修改CmakeLists.txt文件中的cpp文件引用目錄即可(add_library {第三行})。
接下來,在java代碼中調用C++代碼啦,看下MainActivity中的代碼:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Example of a call to a native method TextView tv = (TextView) findViewById(R.id.sample_text); tv.setText(stringFromJNI()); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public native String stringFromJNI(); // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); }}這里注意System.loadLibrary("native-lib"),加載(native-lib)Library,這里是CmakeLists文件中配置好的Library名字要對應。然后就是native調用stringFromJNI ()方法啦》
最后編譯一下app,Make Project build一下,然后run到手機上就可以運行了。
這里有同學想問那.so文件在哪里找啊 ,如下圖:
這里的so文件就可以用到其他項目中啦;
至此一個簡單的使用C++項目就完成啦
新聞熱點
疑難解答
圖片精選