UnityがAndroidaarを呼び出してネイティブクラッシュを実現
知識記録
- AndroidStudioを使用してaarを生成します
- Androidでネイティブレイヤーのクラッシュを実現
- UnityのAndroidaarでメソッドを呼び出す
AndroidStudio生成aar
1.新しいネイティブC ++プロジェクトを作成します
2.新しいAndroidモジュール
オブジェクトモードプロジェクトで、新しいモジュールを作成します-Androidライブラリ
3.対応するモジュールの下に新しいJavaクラスを作成します
Android Studioのプロンプトを使用して、対応するJNIコードを生成します(次の手順で参照できます。JNIを記述できない初心者に適しています)。
ネイティブクラッシュを実現
1. Android Studioを使用して、JNI関連のコードを自動的に生成します
メインディレクトリに新しいJNIフォルダを作成
し、JNIディレクトリに新しいCMakeLists.txtを作成し、次のコンテンツをコピーします(プロジェクトプロジェクト( "crashlibrary")のコンテンツを変更することを忘れないでください)
# 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} )
JNIディレクトリに新しいNative-lib.cppを作成します。システムによって生成されたJNIコードを参照して、独自のJNIコードを定義し、Jni.hヘッダーファイルをインポートできます。
#include "XXX/XXX/jni.h"//这里修改成自己的jni地址
extern "C"
JNIEXPORT jint JNICALL
Java_com_florence_mylibrary_MyClass_MyCrash(JNIEnv *env, jclass clazz) {
// TODO: implement MyCrash()
}
ポインタアドレスエラーを使用してクラッシュさせ
、build.gradleのコンテンツを変更します
2.Aarをコンパイルします
Unityのaarでメソッドを呼び出す
Unityにaarを追加し、スクリプトでaarを呼び出します。
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");
}
}
Android APKを生成し、電話の[クラッシュさせる]ボタンをクリックして、ネイティブクラッシュをトリガーします。
参照リンク
- build.gradleファイルを変更する方法について:https://blog.csdn.net/a673544319/article/details/103530401
- UnityでJavaを呼び出す方法:https://www.cnblogs.com/ezhar/p/12883771.html
- Androidクラッシュをトリガーする:https://blog.csdn.net/ta893115871/article/details/108991277
- AndroidJavaObjectの使用について:https://blog.csdn.net/final5788/article/details/96596679