I am using Android Studio to make a native Android app. I can build the program, but it crashes instantly when I run it to the emulator.
In the location ${ANDROID_NDK}/sources/android/native_app_glue
, I DO have the files android_native_app_glue.c & android_native_app_glue.h
.
The name of my project is 'tryingc'. In Logcat I get these every time:
我正在使用Android Studio制作一个原生Android应用程序。我可以构建程序,但当我将其运行到仿真器时,它立即崩溃。在位置${ANDROID_NDK}/sources/android/native_app_glue,中,我确实有文件android_native_app_gle.c和android_ative_app_gle.h。我的项目名为“尝试”。在Logcat中,我每次都会得到这样的信息:
FATAL EXCEPTION: main
Process: com.example.tryingc, PID: 7972
java.lang.UnsatisfiedLinkError: Unable to load native library "/data/app/com.example.tryingc-nwg41sp15Je1xG1ue9Pgvw==/lib/x86/libtryingc.so": undefined symbol: ANativeActivity_onCreate
at android.app.NativeActivity.onCreate(NativeActivity.java:175)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is my .cpp file:
这是我的.cpp文件:
#include <jni.h>
#include <android_native_app_glue.h>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
extern "C" {
void handle_cmd(android_app *pApp, int32_t cmd) {
}
void android_main(struct android_app *pApp) {
pApp->onAppCmd = handle_cmd;
int events;
android_poll_source *pSource;
do {
if (ALooper_pollAll(0, nullptr, &events, (void **) &pSource) >= 0) {
if (pSource) {
pSource->process(pApp, pSource);
}
}
} while (!pApp->destroyRequested);
}
}
I have 2 CMakeLists.txt files, 1 next to this cpp:
我有两个CMakeLists.txt文件,其中一个在此CPP旁边:
cmake_minimum_required(VERSION 3.6.0)
add_library(tryingc SHARED
tryingc.cpp)
add_library(native_app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
target_include_directories(native_app_glue PUBLIC
${ANDROID_NDK}/sources/android/native_app_glue)
target_include_directories(tryingc PRIVATE
${CMAKE_SOURCE_DIR}/src/main/cpp/include)
target_link_libraries(tryingc
android
native_app_glue
log
EGL
GLESv3)
and another one in my 'app' folder:
Also, I should mention that I have tried removing the set function from here or linking android_native_app_glue here too, both with the same error showing up.
还有一个在我的‘app’文件夹中:另外,我应该提一下,我已经尝试从这里删除set函数,或者在这里也链接android_ative_app_glue,但两者都出现了相同的错误。
cmake_minimum_required(VERSION 3.4.1)
find_library(log-lib
log)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
add_library(tryingc SHARED
src/main/cpp/tryingc.cpp)
target_link_libraries(tryingc
android
native_app_glue
${log-lib}
EGL
GLESv3)
This is my AndroidManifest.xml:
这是我的androidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tryingc">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TryingC">
<activity
android:exported="true"
android:name="android.app.NativeActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
<meta-data
android:name="android.app.lib_name"
android:value="tryingc" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And finally, the build.gradle:
最后,build.gradle:
plugins {
id("com.android.application")
}
android {
namespace = "com.example.tryingc"
compileSdk = 33
defaultConfig {
applicationId = "com.example.tryingc"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags += ""
}
}
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
The libtryingc.so
file is being created at these locations:
将在以下位置创建libtryingc.so文件:
tryingC\app\build\intermediates\cxx\Debug\n6l545p2\obj\x86_64
tryingC\app\build\intermediates\cxx\Debug\n6l545p2\obj\x86
tryingC\app\build\intermediates\cxx\Debug\n6l545p2\obj\armeabi-v7a
tryingC\app\build\intermediates\cxx\Debug\n6l545p2\obj\arm64-v8a
tryingC\app\build\intermediates\stripped_native_libs\debug\out\lib\x86
I tried giving as much informations as I could, do not hesitate to ask for anything else.
我尽我所能地提供了更多的信息,请不要犹豫地询问其他任何信息。
更多回答
In the android_main function add app_dummy();
to make sure the native_app_glue doesn't get stripped out.
在android_main函数中添加app_ummy();,以确保本地app_glue不会被剥离。
void android_main(struct android_app* app) {
ALOGV("----------------------------------------------------------------");
ALOGV("android_app_entry()");
ALOGV(" android_main()");
// Make sure glue isn't stripped.
app_dummy();
...
}
check:
检查:
https://irrlicht.sourceforge.io/forum/viewtopic.php?t=52182
Https://irrlicht.sourceforge.io/forum/viewtopic.php?t=52182
更多回答
我是一名优秀的程序员,十分优秀!