I\'ve built the Assimp library as a shared library. I\'ve included it in my Android ndk project and it builds fine but when I load it I get the following error: Unable to lo
You want to start the NativeActivity with a java activity. This way you can load the shared libraries before NativeActivity.
AndroidManifest.xml:
<application android:label="@string/app_name" android:hasCode="true">
<activity android:name="DummyActivity"
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"
android:configChanges="orientation|keyboardHidden">
<meta-data android:name="android.app.lib_name"
android:value="native-activity" />
</activity>
</application>
DummyActivity.java:
package com.example.native_activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class DummyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
System.loadLibrary("some_shared_lib");
System.loadLibrary("native-activity");
super.onCreate(savedInstanceState);
Intent intent = new Intent(DummyActivity.this, android.app.NativeActivity.class);
DummyActivity.this.startActivity(intent);
}
}
Using System.loadLibrary
is the way to go.
Android won't automatically load dependent shared libraries for you. So you need to do something like this:
static {
System.loadLibrary("assimp"); // dependency .so first
System.loadLibrary("native-activity"); // dependent .so second
}
This code usually goes in the class which contains the native Java methods (i.e. methods defined with keyword native
, which map through to native code). Because this code is executed in a static
block it is executed when the Java classloader loads the class -- i.e. before any code in the class actually gets executed.
You shouldn't have to add any reference to assimp
to LOCAL_LDLIBS
because you're already referencing assimp
via the LOCAL_SHARED_LIBRARIES
declaration.
This question may be relevant.
1: U could not use dlopen, since System.loadLibrary is the only method u load a native library from Java layer. 2: Ur library path looks not right, the location should be something like /data/data/urapp/lib/
U need to zip ur library to ur apk file, and while installing, android will unzip it and put it to /data/data/urapp/lib/ automatically.
Hope above information is useful for u.
Subclassing android.app.NativeActivity is the simplest way to solve this problem.
package com.you;
public class MyNativeActivity extends android.app.NativeActivity {
static {
System.loadLibrary("assimp");
}
}
Then change your AndroidManifest.xml
. Replace android.app.NativeActivity
with MyNativeActivity
and remove the tag hasCode="false"
.
As a side note, Android does search for dependencies when loading a shared library. But the scope of the search is limited to /system/lib
.