通过Ndk来生成so文件,我之前有写过关于此种实现的文章:NDK快速集成秘籍但是自Android Studio 3.0.1之后,使用这种方式会给如下提示,建议使用Cmake或者ndk-build集成;
Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
Consider using CMake or ndk-build integration. For more information, go to:
https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
To get started, you can use the sample ndk-build script the Android
plugin generated for you at:
/Users/aserbao/aserbao/code/code/github/functions/audioAndvideo/ffmpeg/TestNDK/app/build/intermediates/ndk/debug/Android.mk
Alternatively, you can use the experimental plugin:
https://developer.android.com/r/tools/experimental-plugin.html
To continue using the deprecated NDK compile for another 60 days, set
android.deprecatedNdkCompileLease=1541041326840 in gradle.properties
#include <jni.h>
JNIEXPORT jstring JNICALL Java_com_aserbao_aserbaosandroid_functions_how_1create_1so_useNdkBuild_CallUtils_callSimpleInfo
(JNIEnv *env, jclass ojb){//注意这个地方声明env和ojb,不然会报错。
return (*env) -> NewStringUTF(env,"Hello, I'm an info come from use ndk-build");
};
在jni目录下创建Android.mk文件,并输入:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# 导出的so库名字
LOCAL_MODULE := use_ndk_build
# 对应的c代码
LOCAL_SRC_FILES := jni/use_ndk_build.c
include $(BUILD_SHARED_LIBRARY)
#include <jni.h>
JNIEXPORT jstring JNICALL Java_com_aserbao_aserbaosandroid_functions_how_1create_1so_useNdkBuild_CallUtils_callSimpleInfo2
(JNIEnv *env, jclass ojb){
return (*env) -> NewStringUTF(env,"I'm an info come from use libuse_ndk_build2.so");
};
在Android.mk文件下再添加一个模块,修改后的Android.mk代码:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# 导出的so库名字
LOCAL_MODULE := use_ndk_build
# 对应的c代码
LOCAL_SRC_FILES := use_ndk_build.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := use_ndk_build2
LOCAL_SRC_FILES := use_ndk_build2.c
include $(BUILD_SHARED_LIBRARY)
通过CMake编译so文件最简单的例子就是通过Android Studio创建一个InClude C++ support的项目,后面Exceptions Support(-fexception)和Runtime Type Information Support(-frtti)均打上勾;创建完成,编译运行一下项目。找到app/build/intermediates/cmake/debug文件夹,在这里我们可以找到刚刚通过CMake生成的对应so文件;
# 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.
use_cmake_build
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/aserbao-one-lib.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.
use_cmake_build
# Links the target library to the log library
# included in the NDK.
${log-lib} )
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
use_cmake_build
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/num_one/aserbao-one-lib.cpp )
add_library( # Sets the name of the library.
use_cmake_build2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/num_two/aserbao-two-lib.cpp )
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 )
target_link_libraries( # Specifies the target library.
use_cmake_build
# Links the target library to the log library
# included in the NDK.
${log-lib} )
target_link_libraries( # Specifies the target library.
use_cmake_build2
# Links the target library to the log library
# included in the NDK.
${log-lib} )
//只需要修改add_library下的c代码路径:
add_library( # Sets the name of the library.
use_cmake_build
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/num_one/aserbao-one-lib.cpp src/main/cpp/num_two/aserbao-two-lib.cpp)
#include <jni.h>
JNIEXPORT jstring JNICALL Java_com_aserbao_aserbaosandroid_functions_how_1create_1so_useNdkBuild_CallUtils_callSimpleInfo
(JNIEnv *env, jclass ojb){
return (*env) -> NewStringUTF(env,"Hello, I'm an info come from use ndk-build");
};
000000000000062c T Java_com_aserbao_aserbaosandroid_functions_how_1create_1so_useNdkBuild_CallUtils_callSimpleInfo
0000000000000640 T Java_com_aserbao_aserbaosandroid_functions_how_1create_1so_useNdkBuild_CallUtils_callSimpleInfo2
……