Android中CMake的使用之二具体的使用细节

Android中CMake的使用之二具体的使用细节
在上一节中提供了一个最简单的使用说明,但是在实际情况中,一般来说,应用程序中对C++的控制代码会安排在一个或几个专门的JAVA类中。同样,接口函数也会有很多,数据交互也比较多。同时在本地的C++代码中也要进行大量的开发,不可能直接使用一个CPP文件来搞定。下面就一个个来说。
首先,把本地代码的控制转移到一个新的JAVA控制类上来。在应用层的包下创建一个Java类:
public class JavaNativeControl {


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String GetStringData();




    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("test-lib");
    }
}
这里需要首先说明一下如何将生成的库改名字,如上面,将native-lib改成test-lib,打开CMakeLists.txt文件:
找到相关的文件名字改掉即可:


# 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.
             test-lib


             # Sets the library as a shared library.
             SHARED


             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp
              src/main/cpp/getname.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.
                       test-lib


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


这里,在这个控制类中写入一个本地接口函数比如上面的
public native String GetStringData();下面再增加一个:
public native void GetData();
鼠标移过去后就会出现一个红色的灯泡,然后点击第一个就会自动创建一个JNI接口的函数。图如下


 
这里还有一个问题,如果你不想使用native-lib.cpp这个本地文件,那么你可以创建一个相关的C或者CPP,然后把这个文件删除,并在CMakelists.txt修改相关名称,同时修改
System.loadLibrary("HelloNDK");
中的名称,记住一定要Clean当前工程,然后再重复上述,可自动创建相关JNI函数,如果仍然不行的话,删除创建的相关程序,在空白新建的HelloNDK.c中加上#include<jni.h>或者把自动创建的拷贝到这个空白文件中再创建一次就OK了。如果有重复删除即可。
第二问题是在本地C++文件中大量调用其它相关的C++文件,这个就简单了,只要写上相关的头文件和源文件,象一般的C++编程一样。但是需要注意的是,要在CMakelists.txt的
add_library( # Sets the name of the library.
             test-lib


             # Sets the library as a shared library.
             SHARED


             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp
              src/main/cpp/getname.cpp)


增加相关文件的编译就可以了,如果导出新的函数话,记得自动生成的JNI接口有时不带 extern “C”,增加即可。否则在调用时会报找不到相关的函数。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值