5

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?

Aleksandr
  • 4,906
  • 4
  • 32
  • 47

5 Answers5

1

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

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Thank you for your question, but it didn't help me, and I have no problem with adding my libraries to project. App can't load library 'main'. And I need to fix loading 'main' library, but not lib 'droidblaster'. I will vote up your question – Aleksandr Jun 05 '15 at 14:08
0

You can try to install new gradle with ndk-support integration(experemental). http://ph0b.com/new-android-studio-ndk-support/#more-236 There would be android.ndk box (in your gradle.build) where you should add native_android_glue :

android.ndk{

    moduleName = "droidblaster"
    CFlags.add("-I${file("src/main/jni/android_native_app_glue")}".toString())
    ldLibs.addAll(["log", "android", "EGL", "GLESv1_CM"])

}
0
 externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared" //add this in the build.gradle

            }
        }
Pascal Nitcheu
  • 667
  • 7
  • 8
0

I think you have to remove your second activity and add this tag <meta-data android:name="android.app.lib_name" android:value="droidblaster" /> to your first activity

kochol
  • 43
  • 6
-4

change NativeActivity to AppCompatActivity.

Vikash
  • 1