无法加载本机库/未定义符号:ANativeActivity_onCreate

问题描述 投票:0回答:1

我正在使用 Android Studio 制作原生 Android 应用程序。我可以构建该程序,但当我将其运行到模拟器时它会立即崩溃。 在位置

${ANDROID_NDK}/sources/android/native_app_glue
,我确实有文件
android_native_app_glue.c & android_native_app_glue.h
。 我的项目的名称是“tryingc”。在 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)

这是我的.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);
}
}

我有 2 个 CMakeLists.txt 文件,其中 1 个位于此 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)

还有我的“app”文件夹中的另一个: 另外,我应该提到,我已经尝试从这里删除 set 函数或也在这里链接 android_native_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)

这是我的 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>

最后,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")
}

正在这些位置创建

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

我尝试提供尽可能多的信息,如有其他问题,请随时询问。

android android-ndk native-activity
1个回答
0
投票

在 android_main 函数中添加

app_dummy();
以确保 native_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();
    ...
}

检查:

https://irrlicht.sourceforge.io/forum/viewtopic.php?t=52182

© www.soinside.com 2019 - 2024. All rights reserved.