Question

Update : I managed to pack *.so files with final apk, simply appending this to build.gradle :

    tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
        pkgTask -> pkgTask.jniDir new File(projectDir, 'src/main/libs')
    }

But now the native function is not being called.

code of native.c

    #include <jni.h>
    #include <string.h>
    #include <android/log.h>

    JNIEXPORT jstring JNICALL Java_com_myndkapp_mybullet_MainActivity_helloLog(JNIEnv * env, jobject this) {
        __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "Check string you received in the Activity");
        return (*env)->NewStringUTF(env, "Hello from Native code :)");
    }

Snippet from MainActivity.java :

    .
    ..
    static {
            System.loadLibrary("myfndk");
        }
    private native String helloLog();
    .
    ..

Some traces of ADB log are here :

    11-18 16:53:43.261    3469-3469/? D/PackageAddedReceiver﹕ package added com.myndkapp.mybullet
    11-18 16:53:43.856    2963-3214/? V/SmartFaceService - 3rd party pause﹕ onReceive [android.intent.action.ACTIVITY_STATE/com.myndkapp.mybullet/create]
    11-18 16:53:43.886    2823-2823/? D/dalvikvm﹕ Trying to load lib /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270
    11-18 16:53:43.886    2823-2823/? D/dalvikvm﹕ Added shared lib /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270
    11-18 16:53:43.886    2823-2823/? D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270, skipping init
    11-18 16:53:50.596    2823-2823/? W/dalvikvm﹕ No implementation found for native Lcom/myndkapp/mybullet/MainActivity;.helloLog:()Ljava/lang/String;
    11-18 16:53:50.606    2823-2823/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Native method not found:         com.myndkapp.mybullet.MainActivity.helloLog:()Ljava/lang/String;
        at com.myndkapp.mybullet.MainActivity.helloLog(Native Method)
        at com.myndkapp.mybullet.MainActivity.access$000(MainActivity.java:15)
        at com.myndkapp.mybullet.MainActivity$1.onClick(MainActivity.java:35)
        at android.view.View.performClick(View.java:4475)
        at android.view.View$PerformClick.run(View.java:18786)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
    11-18 16:53:50.691    2963-3214/? V/SmartFaceService - 3rd party pause﹕ onReceive [android.intent.action.ACTIVITY_STATE/com.myndkapp.mybullet/pause]
    11-18 16:53:50.696    2963-3043/? D/CrashAnrDetector﹕ processName: com.myndkapp.mybullet
    11-18 16:53:50.696    2963-3043/? D/CrashAnrDetector﹕ broadcastEvent : com.myndkapp.mybullet data_app_crash
    11-18 16:54:05.781    2963-3449/? I/ActivityManager﹕ Process com.myndkapp.mybullet (pid 2823) (adj 9) has died.

I am trying to build an android NDK app using android-ndk-r9 in Android Studio 0.3.5 132.910074 on Fedora 19 x86_64.

It look's like libmyfndk.so is not added to the final apk file. I confirmed this by viewing contents of apk file. When I run this test app on my Galaxy S4 (Jelly Bean 4.3) it throws error.

    11-17 19:03:28.816  26472-26472/com.myndkapp.mybullet E/AndroidRuntime﹕ 
    FATAL EXCEPTION: main
    java.lang.UnsatisfiedLinkError: Couldn't load myfndk from loader 
    dalvik.system.PathClassLoader[dexPath=/data/app/com.myndkapp.mybullet-1.apk,
    libraryPath=/data/app-lib/com.myndkapp.mybullet-1]: findLibrary returned null

I've looked on to solutions here:

1.) https://groups.google.com/forum/#!msg/adt-dev/xj51eCWwhFw/pfhvCoquPysJ

2.) https://groups.google.com/forum/?fromgroups#!searchin/adt-dev/so/adt-dev/nQobKd2Gl_8/Z5yWAvCh4h4J

3.) Android studio, gradle and NDK

3.1) https://stackoverflow.com/a/16790675/1673000 (This worked for me)

4.) https://github.com/OnlyInAmerica/Android-JNI-Gradle

5.) Include .so library in apk in android studio

None of these seems to work.

This is my build-gradle file's content

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.6.3'
        }
    }
    apply plugin: 'android'

    repositories {
        mavenCentral()
    }

    android {
        compileSdkVersion 18
        buildToolsVersion '19.0.0'

        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 19
        }
    }

    dependencies {
        compile 'com.android.support:appcompat-v7:+'
    }

Any hints, why the Native function is not being called ?

Was it helpful?

Solution

It was not placing the code of native.c in libmyfndk.so, and that because of a missing '=' in my Android.mk in this line :

LOCAL_SRC_FILES : native.c

Changing the above line to LOCAL_SRC_FILES := native.c, resolved this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top