Loading 3rd Party Shared Libraries From An Android Native Activity
Solution 1:
Subclassing android.app.NativeActivity is the simplest way to solve this problem.
package com.you;
publicclassMyNativeActivityextendsandroid.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
.
Solution 2:
You want to start the NativeActivity with a java activity. This way you can load the shared libraries before NativeActivity.
AndroidManifest.xml:
<applicationandroid:label="@string/app_name"android:hasCode="true"><activityandroid:name="DummyActivity"android:label="@string/app_name"android:configChanges="orientation|keyboardHidden"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="android.app.NativeActivity"android:label="@string/app_name"android:configChanges="orientation|keyboardHidden"><meta-dataandroid: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;
publicclassDummyActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
System.loadLibrary("some_shared_lib");
System.loadLibrary("native-activity");
super.onCreate(savedInstanceState);
Intentintent=newIntent(DummyActivity.this, android.app.NativeActivity.class);
DummyActivity.this.startActivity(intent);
}
}
Solution 3:
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.
Solution 4:
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.
Post a Comment for "Loading 3rd Party Shared Libraries From An Android Native Activity"