Android studio ndk environment construction (Android.mk way)

Development environment: windows10, android studio 3.5.2, Android.mk method

1. Install ndk

Method 1: Download from https://developer.android.google.cn/ndk/downloads/
, unzip, and configure the environment

Method 2: Android studio comes with its own download
Insert picture description here
Insert picture description herefor environment variable configuration:
Insert picture description hereInsert picture description here

2.android stuido ndk configuration

1. Configure the ndk path
Insert picture description herein local.properties 2. Add in gradle.properties:

android.useDeprecateNdk=true

Insert picture description here
3. Create the jni jniLibs folder in the main directory at the same level
Insert picture description here

3. Write the code

1. Create the myndk class in com.example.Demo, and call the code in the so library generated by c/c++ by clicking the button in MainActivity:
Insert picture description hereMainActivity.java:

package com.example.Demo;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    
    

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView  = findViewById(R.id.tx);
        Log.d("main","onCreate");
    }
    public void onClick(View view) {
    
       //Buttton 点击会调用
        myndk myndk = new myndk();
        int x = myndk.getint();
        Log.d("main","-----------------: "+x);
        textView.setText(String.valueOf(x));
    }
}

myndk.java

package com.example.Demo;

import android.util.Log;

public class myndk {
    
    
    static {
    
    
        Log.d("main","static  static  static  static  static  static");
        System.loadLibrary("MyLibrary");
    }
    public  native int getint();
}

JNI is divided into static loading and dynamic loading:
static loading requires separate generation of .h files.
Dynamic loading does not require manual generation of .h files.
Here we will first introduce static loading methods and manual generation of .so libraries.

(1) After the two files are written, execute in the myndk.java directory:

javac myndk.java

Generate the myndk.class file.
(2) Execute in the main/java directory:

javah -classpath . -jni com.example.Demo.myndk

Automatically generate com_example_Demo_myndk.h

Then move com_example_Demo_myndk.h to the java/jni directory

(3) Then create the Android.mk Application.mk test.c file in the jni directory.
Insert picture description hereAndroid.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := MyLibrary
LOCAL_SRC_FILES =: test.c
include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_MODULES := MyLibrary
APP_ABI := all

test.c:

#include "com_example_Demo_myndk.h"
JNIEXPORT jint JNICALL Java_com_example_Demo_myndk_getint
  (JNIEnv * env, jobject obj){
   return 500;
  }

Then execute in jni:

ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk NDK_LIBS_OUT=..\jniLibs\

So libraries of different platforms will be automatically generated in jniLibs

NDK commonly used compilation parameters

NDK_PROJECT_PATH
说明:指定工程目录
示例:ndkbuild NDK_PROJECT_PATH=C:\Hello

NDK_LIBS_OUT
说明:指定.so文件输出目录,默认值为"$(NDK_PROJECT_PATH)/libs"
示例:ndkbuild NDK_LIBS_OUT=C:\Hello\libs

NDK_APPLICATION_MK
说明:指定Application.mk文件路径,默认值为"$(NDK_PROJECT_PATH)/jni/Application.mk"
示例:ndkbuild NDK_APPLICATION_MK=C:\Hello\src\Application.mk

NDK_DEBUG
说明:打开或关闭调试模式,debug模式时会生成gdbserver等文件
示例:ndkbuild NDK_DEBUG=1

NDK_LOG
说明:显示内部NDK日志消息(用于调试NDK自身)
示例:ndkbuild NDK_LOG=1

NDK_HOST_32BIT
说明:Always use toolchain in 32-bit mode (see below).
示例:ndkbuild NDK_HOST_32BIT=1

APP_BUILD_SCRIPT
说明:指定Android.mk文件路径,默认值为"$(APP_PROJECT_PATH)/jni/Android.mk"
示例:ndkbuild APP_BUILD_SCRIPT=C:\Hello\src\Android.mk

Clear
说明:清除所有生成的二进制文件
示例:ndkbuild Clear

-B
说明:强制完全重新构建
示例:ndkbuild -B

-C <project>
说明:构建位于<工程目录>的工程的本地代码。当你不想在终端上用cd切换到那个目录时有用
示例:ndkbuild -C C:\Hello

V
说明:打开或关闭显示编译参数
示例:ndkbuild V=1

4. Run

Insert picture description here
Effect: After clicking the button, the native method will be called, and 500 will be returned and displayed on the textView
Insert picture description here

The above is the manual configuration to generate the .so library, and finally the .so library is loaded into the apk.

Guess you like

Origin blog.csdn.net/weixin_41477306/article/details/107732637
Recommended