You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android NDK原生应用启动崩溃:undefined symbol: ANativeActivity_onCreate

问题:NativeActivity运行崩溃,提示ANativeActivity_onCreate未定义符号

程序可正常编译,但模拟器运行时立即崩溃,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)

用户提供的相关代码与配置

1. C++实现文件(tryingc.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. cpp目录下的CMakeLists.txt

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)

3. app目录下的CMakeLists.txt(未被使用)

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)

4. 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>

5. build.gradle(app模块)

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")
}

解决方案

崩溃根源是ANativeActivity_onCreate符号未被包含在最终的libtryingc.so中。该符号来自native_app_glue静态库,但由于代码中未直接引用它,链接器会将其优化剔除,导致NativeActivity加载库时找不到该符号。

修复步骤:

  1. 修改cpp目录下的CMakeLists.txt,添加链接器标志强制保留ANativeActivity_onCreate符号:

    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)
    
    # 强制导出ANativeActivity_onCreate符号,避免链接器优化剔除
    set_target_properties(tryingc PROPERTIES
            LINK_FLAGS "-u ANativeActivity_onCreate")
    
    target_link_libraries(tryingc
            android
            native_app_glue
            log
            EGL
            GLESv3)
    
  2. 清理并重建项目:

    • 点击Android Studio菜单栏的Build -> Clean Project
    • 再点击Build -> Rebuild Project
    • 重新运行模拟器测试
  3. 可选:删除app目录下未被使用的CMakeLists.txt,避免混淆。

内容的提问来源于stack exchange,提问作者Adrian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:14:53