Java – Android java.lang.IllegalArgumentException: Unable to find native library: main

androidandroid-ndkjava

Today I found some projects, which demonstrates android ndk programming. Here is this projects

I'm trying to start project DroidBlaster. I have compiled all native libraries without errors. Here is result:

compiled droidblaster libraries

I just made some small changes:

1). Added to application tag in AndroidManifest.xml option android:hasCode="true"

2). I extended from native activity and load all libraries in static block of activity.

I did this, because, without this changes, android can't find library libdroidblaster.so which was declared in AndroidManifest.xml(Here is code from author):

<activity
            android:name="android.app.NativeActivity"
            android:label="@string/app_name" >
            <meta-data
                android:name="android.app.lib_name"
                android:value="droidblaster" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Now my AndroidManifest.xml looks like:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.droidblaster"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="22" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hasCode="true">

        <activity android:name=".MainActivity"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden">
            <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"
            android:label="@string/app_name" >
            <meta-data
                android:name="android.app.lib_name"
                android:value="droidblaster" />
        </activity>
    </application>

    <uses-permission android:name="android.permission.RECORD_AUDIO" />

</manifest>

And my new activity:

public class MainActivity extends NativeActivity {
    static {
        System.loadLibrary("irrlicht");
        System.loadLibrary("droidblaster");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

When I try to start app, I get this error:

06-05 12:56:26.523 11528-11528/com.demo.droidblaster
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.demo.droidblaster, PID: 11528
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.droidblaster/com.demo.droidblaster.MainActivity}:
java.lang.IllegalArgumentException: Unable to find native library:
main
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalArgumentException: Unable to find native library: main
at android.app.NativeActivity.onCreate(NativeActivity.java:170)
at com.demo.droidblaster.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

So, the cause is:

java.lang.IllegalArgumentException: Unable to find native library: main

I can't find a solution to fix this problem.

Does anyone know how to fix it?

Best Solution

.so files are not automatically included by Android Studio. Follow the answer given here to include them: https://stackoverflow.com/a/17131418/1395437