android studio 2.2 ndk 简例

本文将带你逐步了解如何在Android Studio 2.2中使用NDK进行原生代码开发。从配置环境到创建C/C++项目,再到编译和调试,详述每个步骤,让你轻松入门Android NDK开发。

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

   OS环境: windows 10
   通过android studio 2.2创建一个包含C++的项目,android studio会自动生成一些相关的文件。在app目录下的cpp目录下回有一个native-lib.cpp的文件,在该目录下在创建一个mtest.h和mtest.cpp文件,然后同步下,否则在cpp目录下不会显示你所创建的文件。两个文件里面的内容如下:
//mtest.h
#ifndef MYNATIVE_MTEST_H
#define MYNATIVE_MTEST_H

#include <sstream>

int adds(int a,int b);

#endif //MYNATIVE_MTEST_H



//mtest.cpp
#include "mtest.h"

int adds(int a,int b){
    return a+b;
}



//native-lib.cpp
#include <jni.h>
#include <string>
#include "mtest.h"

extern "C"
jstring
Java_com_anative_soft_mynative_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    int v_c = adds(10,30);
    std::stringstream ss;
    ss << v_c;
    std::string str;
    ss >> str;
    hello=hello+str;
    return env->NewStringUTF(hello.c_str());
}
   代码部分就完成了,下面来修改CMakeLists.txt里面的内容了。生成的CMakeLists.txt内容如下:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

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 it 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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# 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} )
   修改后的如下:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

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 it 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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp src/main/cpp/mtest.cpp )//这个要写相对于src的绝对路径,此处一定要包含自己写的相关的C/C++的代码,否则在编译的时候提示找不到自定义的方法的引用。

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# 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} )
   编译安装。最后的效果图如下:

这里写图片描述

   CMakeLists.txt里面参数具体含义请参考(如果打不开的话自带梯子)
   https://developer.android.com/studio/projects/add-native-code.html?utm_campaign=android%20studio_launch_2.2_091916&utm_source=anddev&utm_medium=blog#create-cmake-script
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值