Unity calls Android arr to achieve Native crash

Unity calls Android aar to achieve Native crash

Knowledge record

  1. Use Android Studio to generate aar
  2. Realize the native layer crash in Android
  3. Call the method in Android aar in Unity

Android Studio 生成 aar

1. Create a new Native C++ project

Insert picture description here

2. New Android Module

In the Porject mode project, create a new Module-Android Library
Insert picture description here
Insert picture description here

3. Create a new Java Class under the corresponding Module

Insert picture description here
Insert picture description here
Use Android Studio's prompts to generate a corresponding JNI code (you can refer to it in the following steps, suitable for novices who can't write JNI)
Insert picture description here

Achieve Native crash

1. Use Android Studio to automatically generate JNI related code

Create a new JNI Folder in the main directory, create a
Insert picture description here
new CMakeLists.txt in the JNI directory, copy the following content (remember to modify the content of the Project project("crashlibrary"))
Insert picture description here

# 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.10.2)

# Declares and names the project.

project("crashlibrary")

# 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).
        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} )

Create a new Native-lib.cpp in the JNI directory, you can refer to the JNI code generated by the system to define your own JNI code, and import the Jni.h header file.
Insert picture description here


#include "XXX/XXX/jni.h"//这里修改成自己的jni地址

extern "C"
JNIEXPORT jint JNICALL
Java_com_florence_mylibrary_MyClass_MyCrash(JNIEnv *env, jclass clazz) {
    // TODO: implement MyCrash()
}

Use pointer address error to make Crash
Insert picture description here
modify the content of build.gradle
Insert picture description here

2. Compile Aar

Insert picture description here

Call the method in aar in Unity

Add aar in Unity and call aar in the script.
Insert picture description here

void OnGUI()
	{
		if (GUI.Button(new Rect(100,300, 300, 80), "<size=30>Make Crash</size>"))
		{
			androidJavaObject = new AndroidJavaObject("com.florence.mylibrary.MyClass");
			androidJavaObject.CallStatic<int>("MyCrash");

		}	
	}

Generate an Android APK, and click the Make Crash button on the phone to trigger a native crash.
Insert picture description here

Reference link

  • On how to modify the build.gradle file: https://blog.csdn.net/a673544319/article/details/103530401
  • The method of calling Java in Unity: https://www.cnblogs.com/ezhar/p/12883771.html
  • Trigger Android Crash: https://blog.csdn.net/ta893115871/article/details/108991277
  • About the use of AndroidJavaObject: https://blog.csdn.net/final5788/article/details/96596679

Guess you like

Origin blog.csdn.net/weixin_45266845/article/details/115271634