JNI开发篇——报错:Flag android.useDeprecatedNdk is no longer supported and will be removed in the next……

大概意思就是说:

  1. android.useDeprecatedNdk不再支持了
  2. 让使用CMake or ndk-build
  3. 然后还有链接

解决方法:

1、先通过SDKManager下载:CMake和LLDB

2、在build.gradle的defaultConfig节点下加入:

// 使用Cmake工具
    externalNativeBuild {
      cmake {
        cppFlags ""
        //生成多个版本的so文件
        abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
      }
    }

在build.gradle的android节点下加入:

// 配置CMakeLists.txt路径
  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"  // 设置所要编写的c源码位置,以及编译后so文件的名字
    }
  }

3、添加CMakeLists.txt文件到build.gradle文件同级目录下,具体内容如下:

# 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.
#CMakeLists.txt
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.
      # 设置so文件名称.
       NameProvider

       # Sets the library as a shared library.
       SHARED
       # 设置这个so文件为共享.

       # Provides a relative path to your source file(s).
       # 设置这个so文件为共享.
       src/main/jni/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.
            # 制定目标库.
            NameProvider

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

至此,我们所有的流程都做完了,下面来检查一下我们的成果,见证奇迹的时候到了:

猜你喜欢

转载自www.cnblogs.com/Jackie-zhang/p/10092692.html
今日推荐