Android Studio 2.2 在已有项目中添加NDK

1、加载库与编写native方法



2、在Terminal命令窗口下进入到java文件夹,执行如下命令:javah  包名.类名,生成头文件。

3、在项目的main文件夹下新建一个cpp文件夹,然后新建一个cpp文件,这个文件名要与代码System.loadLibrary("patch")中的参数名称相同。在这里我把文件名定为patch

4、把在第二步中生成的头文件中的方法复制到,patch.cpp中。在头文件中的方法名如下:
      JNIEXPORT jstring JNICALL Java_com_example_eric_filepatch_MainActivity_getStringFromNative(JNIEnv *, jobject); 
    以上的写法在调用的时候出现问题,我把它改成以下的写法
    extern "C" jstring Java_com_example_eric_filepatch_MainActivity_getStringFromNative(JNIEnv *env, jobject);
    这样就可以正常使用了。
     其中extern “C” 是表示当代码使用C与C++混编时,指示编译器按照C语言进行编译。在extern “C” 中include进去的文件不能再有extern “C”, 因为extern “C”不能再有
     extern “C”。
    当include文件时,相当于把代码拷贝了一份放到该文件中
5、在local.properties文件下添加如下代码:
      ndk.dir=D\:\\Android\\android_sdk\\android_sdk\\ndk-bundle
6、在app文件夹下新建一个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.
             patch

             # 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/patch.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.
                       patch

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

    其中带有#号的代码是注释,说明如下

   # 指定cmake最低版本,如果不知道就复制吧
   cmake_minimum_required(VERSION 3.4.1)

   # 第一个native-lib是供java调用的文件, SHARED是啥没看明白但是要写上
   # 最后是所有cpp的名字,这里在一个文件夹中就直接写名字了,文件有多少写多少,假如有 patch.cpp和 Max.cpp,记得写上路径
  add_library(patch SHARED src/main/cpp/patch.cpp src/main/cpp/Max.cpp)

  # 我的理解是java要连接到patch,和一些其它的东西,不懂
  target_link_libraries(patch)

7、在app的build.gradle中添加如下代码
  
     

    8、生成so文件
          首先在CMakeLists文件中添加如下的代码:
          set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
          该行代码的作用是把生成的so文件输出到指定的目录,在这里是输出到jniLibs文件夹下,改行代码的位置要在以下代码的下面
          cmake_minimum_required(VERSION 3.4.1)
          然后再build.gradle中添加需要输出的so文件类型,如下图所示。
1
   
然后就可以运行了。当然还有另外一种在原有的项目中添加NDK开发的方式,使用的是android.mk文件。在这里就不介绍了。

参考:http://blog.csdn.net/lmw21848/article/details/52183584
CMake:http://www.hahack.com/codes/cmake/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值