Adrian
Adrian

Reputation: 169

Unable to load native library / undefined symbol: ANativeActivity_onCreate

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:

 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:

#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:

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.

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:

<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:

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:

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.

Upvotes: 0

Views: 611

Answers (1)

Amr
Amr

Reputation: 2280

In the android_main function add app_dummy(); to make sure the native_app_glue doesn't get stripped out.

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

Upvotes: 1

Related Questions