- Android Studio搭建環境
- Native c++ 快速生成NDK編譯環境
- 普通項目使用CMake 手動創建編譯環境
- Eclipse 搭建環境
有些公司環境特殊可能因爲某些原因還在繼續使用着Eclipse 。
3. Android studio 多個c/c++文件配置方式
4. C/C++調用logcat日誌
Android Studio 3.4 NDK搭建環境
在android studio 下對NDK的支持非常的方便,只要在
Studio 3.4.1 進入Android SDK --> SDK Tools 下載 ndk和cmake
Native c++ 一鍵生成NDK編譯環境
Android studio 3.3開始創建NewProgr單獨把include c++ support 提取到新的模板。
選擇 Native c++ ---> next下一步
選擇Default即可
自動生成c++的運行環境 ,直接可以運行在屏幕上顯示 “Hello from c++”說明成功了。
Android Studio 利用CMake手動創建NDK 編譯環境
1. 首先還是先進入進入Android SDK --> SDK Tools 下載 ndk和cmake
2. 創建或者導入一個普通的android項目
3. 在項目裏創建一個類並且寫一個native方法
public class MyNative {
static {
System.loadLibrary("myLib");
}
public static native int add(int number1,int number2);
}
4. 生成 .h 頭文件
進入 Terminal 輸入命令進入java目錄 :cd app/src/main/java
在java目錄下生成 .h頭文件 :javah 包名.MyNative
自動生成如下內容
/* DO NOT EDIT THIS FILE - it is machine generated */
//需要複製到C/C++文件下
#include <jni.h>
/* Header for class com_laiweifeng_myapplication_MyNative */
#ifndef _Included_com_laiweifeng_myapplication_MyNative
#define _Included_com_laiweifeng_myapplication_MyNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_laiweifeng_myapplication_MyNative
* Method: add
* Signature: (II)I
*/
//C/C++文件所需要調用的jni方法
JNIEXPORT jint JNICALL Java_com_laiweifeng_myapplication_MyNative_add
(JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
5. 在app/src/main下創建jni文件夾,把java目錄下的頭文件和把編寫好的c/c++文件剪切或者複製到jni文件夾下
6. 把剛剛生成的 .h 頭文件裏面的方法複製到 .c 文件內,並且改一下變量加上名字和實現方法
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_laiweifeng_myapplication_MyNative_add
(JNIEnv * env, jclass obj, jint number1, jint number2){
//加法運算
return number1+number2;
}
7. 右鍵在app目錄下創建File,命名爲“CMakeLists.txt”
8 . 把以下的內容複製到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.
myLib #.so庫名 可自定義
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/myc.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.
myLib #.so庫名 可自定義
# Links the target library to the log library
# included in the NDK.
${log-lib} )
9. 右鍵 app目錄選擇 “Link C++ project with Gradle” 填寫當前項目下的“CMakeLists.txt” 的絕對路徑後點擊確定自動在app下的build.gradle 生成對應的引入。
android {
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
}
10 . build -> make project 自動生成so文件說明配置成功,最後調用native方法。
Eclipse 搭建NDK開發環境
1. 首先第一部還是下載NDK開發包:https://developer.android.google.cn/ndk/downloads/index.html
我用的是目前最新的 r20 。
2. 解壓android-ndk-r20-windows-x86_64.zip ,並且配置環境變量:我的電腦-> 屬性->高級->環境變量->系統變量->把剛剛解壓的文件路徑添加到path裏。
3. 導入或者新建普通的eclipse項目,創建native本地方法
public class MyNative {
static {
System.loadLibrary("myLib");
}
public static native int add(int number1,int number2);
}
4. 打開cmd命令窗口,cd 當前工程src目錄下,執行javah 生成頭文件:
cd your project/src
javah 包名.MyNative
右鍵刷新一下就可以看到頭文件了。
5. 右鍵當前項目創建jni文件夾,並且把頭文件拖到jni文件夾下。
6. 把編寫好的c/c++文件放置在jni目錄下,並把.h生成的方法拷貝到c/c++文件下,實現方法。
#include <jni.h>
JNIEXPORT jint JNICALL Java_com_laiweifeng_jni_MyNative_add
(JNIEnv * env, jclass obj, jint num1, jint num2){
return num1+num2;
}
7. 在jni下創建Android.mk 文件,把以下內容拷貝到裏面去。
LOCAL_PATH := $(call my-dir)
# --------------------------------------------------------------
include $(CLEAR_VARS)
LOCAL_MODULE := myLib #自定義so庫名稱
LOCAL_SRC_FILES := myc.c #調用的c/c++文件
LOCAL_LDLIBS := -llog #android log日誌聲明使用
include $(BUILD_SHARED_LIBRARY)
8. 在當前工程目錄下執行ndk-build 生成.so庫,右鍵刷新一下就可以看到了。
Android studio 多個c/c++文件配置方式
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
myLib #.so庫名 可自定義
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/myc.c
src/main/jni/myc2.c) #多個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.
myLib #.so庫名 可自定義
# Links the target library to the log library
# included in the NDK.
${log-lib} )
區別只在這裏,其他的調用方式都是一樣的,就不再累述了。
C/C++調用logcat日誌
#include <android/log.h>
#define LOG_TAG "自定義tag名"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG, __VA_ARGS__))
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG, __VA_ARGS__))
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG, __VA_ARGS__))
//調用示例
LOGV("我是打印");
LOGV("我是 %s","小明");
打印Log要拼接字符串需要使用佔位符,詳情瀏覽以下鏈接
https://blog.csdn.net/qq_29350001/article/details/52278308