androidandroid-ndknative-activity

Build NativeActivity using ndk-build


I am trying to launch native activity after launching it normally from java(I need to load another library that's why I am launching NativeActivity from java).

How would I build the sample using Android.mk? the original sample uses gradle. I tried to build it and at launch the library fails to load.

FATAL EXCEPTION: main Process: sample.simple.com.myapplication, PID: 14917
 java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.simple.com.myapplication/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app/sample.simple.com.myapplication/lib/arm64/libnActivity.so

Here are snippets of the code.

Android.mk Made this by following http://brian.io/android-ndk-r10c-docs/Programmers_Guide/html/md_2__samples_sample--nativeactivity.html

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := nActivity
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)

Java Activity

public class DummyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

    System.loadLibrary("nActivity");
   Intent intent = new Intent(DummyActivity.this, android.app.NativeActivity.class);
   DummyActivity.this.startActivity(intent);

  //  setContentView(R.layout.activity_dummy);
}
}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="display.rendering.simple.com.myapplication">

    <application android:label="@string/app_name" android:hasCode="true">
        <activity android:name="DummyActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="android.app.NativeActivity">
            <meta-data android:name="android.app.lib_name"
                android:value="nActivity" />
        </activity>
    </application>

</manifest>

build.gradle

ndk{
    moduleName ="nActivity"
}
sourceSets.main {
    jni.srcDirs = []
    jni.srcDir "src/main/libs"
}

Thanks


Solution

  • Your build.gradle disables the built-in call to ndk-build here:

    jni.srcDirs = []
    

    This means no native library is actually being built. No library -> no library with a matching name on the device -> loadLibrary error.

    You don't necessarily need to use an Android.mk file to launch native-activity from Java. The other NDK samples document how to build and use native code with build.gradle. The build.gradle file from hello-jni is a great place to start.

    I would advise against using Android.mk unless you have to, but there are NDK samples using Android.mk if you need them.