1. 程式人生 > >AndroidStudio gradle:3.2.0配置NDK開發環境

AndroidStudio gradle:3.2.0配置NDK開發環境

AndroidStudio gradle:3.2.0配置NDK開發環境

最近這段時間在學習Android開發人臉識別,踩了很多坑,也學到挺多東西的,第一篇筆記,記錄一下NDK環境搭建。

首先開啟SDK在這裡插入圖片描述

然後下載這三個東西,是搭建環境需要的包在這裡插入圖片描述正常情況下這樣就可以了,我們開啟File–>Setting Structure,設定NDK路徑
NDK預設下載到SDK目錄之下在這裡插入圖片描述

然後新建一個專案,記得把C++打勾
在這裡插入圖片描述

然後Android會自動生成相應的檔案,配置如下
在這裡插入圖片描述
CMake檔案內容如下:

 # 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.
        native-lib
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        src/main/cpp/native-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.
        native-lib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
 一般local.propertoise檔案裡面的NDK路徑

會自動生成在這裡**插入圖片描述
後面編譯後的.so檔案一般在這裡面
在這裡插入圖片描述
APP的buile.gradle可以這樣配置,ndk下面寫CPU名稱在這裡插入圖片描述

externalNativeBuild {
    cmake {
        cppFlags ""
    }
    ndk{
        abiFilters "arm64-v8a", "armeabi-v7a", "x86", "x86_64"
        //,"mips","mips64","x86_64"
    }
}

然後就可以愉快地呼叫C++檔案啦,在主類裡面載入.
.cpp檔案
注意C++檔案當中的函式名是Java_全類名
最後的類名要和java檔案中宣告的方法名一樣

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ss_myapplication2_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
public class Main{

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Example of a call to a native method
    TextView tv = (TextView) findViewById(R.id.sample_text);
    tv.setText(stringFromJNI());
}

/**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();//這裡用native宣告需要使用C++實現的函式名稱
}

3.2的配置過程大概就是這樣總體還是比較容易~