android Studio installation process NDK + CMake

Jni project requires development, computer never had the tools before installation, now let's start from scratch.

1, and installation NDK CMake, Android Studio interface Ctrl + Alt + S into the settings interface, select CMake and NDK, click Apply, will be automatically installed

 

2, the new file in the project myJNI.java, and MainActivity the same directory on the line, as

3, edit myJNI.java file, the code is as follows:

package com.example.myapplication;

public class myJNI {

    static {
        System.loadLibrary("JniTest");
    }
    
    public static native String sayHello();
}

 

4. Click on the Android Studio Terminal, enter the terminal, execute the command javac myJNI.java, will generate a directory in myJNI.class

 

5, then execute javac -h myJNI.java, will generate a header file

6, create a new folder jni, and create a new c file main.c

7, after the file just generated com_example_myapplication_myJNI.h implemented method, copy to main.c code is as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_myapplication_myJNI */

#ifndef _Included_com_example_myapplication_myJNI
#define _Included_com_example_myapplication_myJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_myapplication_myJNI
 * Method:    sayHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_myapplication_myJNI_sayHello
  (JNIEnv *env, jclass jobject){
    return (*env)->NewStringUTF(env,"Hello Maybe");
  }

#ifdef __cplusplus
}
#endif
#endif

8, at the new app CMakeLists.txt file path, and the configuration file,

CMakeLists.txt file code is as follows:

# 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.  设置生成so文件名
            JniTest

            # Sets the library as a shared library.
            SHARED

            # Provides a relative path to your source file(s).  要编译的C/C++文件
            src/main/jni/main.c)

# 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. 指定连接的目标库
                      JniTest

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

9, the configuration of the build.gradle app, add the following code in defaultConfig:

Outside defaultConfig, android the following code:

10, Rebuild Project, rebuild the project

11, so find the file in the app / build / intermediates / cmake / debug / obj / armeabi-v7a path

12, in the main path to the new folder jniLibs, so just generated copy files to the path

 

13, the method by calling jni code

Completed a ~ ~ ~

 

Published 65 original articles · won praise 17 · views 50000 +

Guess you like

Origin blog.csdn.net/lancelots/article/details/98092494