NDK android studio 3.4 和 Eclipse 详细使用文档

 

  1. Android Studio搭建环境
    • Native c++ 快速生成NDK编译环境
    • 普通项目使用CMake 手动创建编译环境
  2. 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 下载 ndkcmake

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

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值