NDK使用说明

NDK使用说明

方法一:

新建工程时候选择include C++support,后面会自动生成相应的文件,包括cpp

 

 

方法二:

两个工具:CMake(CMakeLists.txt)和ndk-build(Android.mk)

这个方法是要自己配的

 

1、 在build.properties文件中加入这句话

// Remove this line
android.useDeprecatedNdk = true

 

2、 通过SDK管理器下载LLDBCMakeNDK

 

3、 新建一个类,申明native函数

 

public class NdkBuildTest {
    static {
        System.loadLibrary("NdkBuildTest");
    }
    public native String getString();
}

 

4、 新建文件夹,Cmake新建cpp或ndk-build新建jni

5、 通过javah新建头文件

javah -d jni -classpath javabooming.leiming

也可以通过快捷键alt+enter生成源文件

C源文件

#include "sunmoon_ndktest2_NdkBuildTest.h"
/**
 * 上边的引用标签一定是.h的文件名家后缀,方法名一定要和.h文件中的方法名称一样
 */
JNIEXPORT jstringJNICALL Java_sunmoon_ndktest2_NdkBuildTest_getString
        (JNIEnv *env, jobject obj) {
    return (*env)->NewStringUTF(env, "123这是我测试的jni");
}

 

 

6、 Cmake新建CMakeLists.txt,ndk-build新建Android.mk

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
             src/main/cpp/test.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} )

Android.mk

LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := NdkBuildTest.c
LOCAL_MODULE:= NdkBuildTest
include $(BUILD_SHARED_LIBRARY)

7、 添加上面文件到gradle

右键点击您想要关联到原生库的模块(例如 app 模块),并从菜单中选择LinkC++ Project with Gradle


备注:

1、如果编译出错,可以在gradle添加(意义不大)

ndk{
  moduleName "NdkBuildTest"
  abiFilters "armeabi","armeabi-v7a","x86"
   }

}
sourceSets {
    main {
        jni.srcDirs = []
    }
}

2、可以一个对象加载几个so,而且可以几个源文件加载到一个so中

 

Android.mk

LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := NdkBuildTest.c Test.c
LOCAL_MODULE:= NdkBuildTest
include $(BUILD_SHARED_LIBRARY)

public class NdkBuildTest {
    static {
        System.loadLibrary("NdkBuildTest");
        //System.loadLibrary("Test");
    }
    public native String getString();
    public native String print();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值