I am facing a problem in one of my app, I have the following code to load a lib (JNI) the app needs :
static {
// load the JNI library
Log.i("JNI", "loading JNI library...");
System.load("/data/data/com.mypackage.appname/lib/libxxxxxx.so");
Log.i("JNI", "JNI library loaded!");
}
So i get get the warning : "Do note hardcode use Context.getFilesDir().getPath() instead"
which is totally rightful (It's not going to be portable on every devices). The thing is, because I am using static I can't call for Context.getFilesDir().getPath()
.
Do you have any ideas on how I can proceeded to do it ?
your warning is absolutely clear, try the following way instead:
make the following class:
public class MyApplication extends Application {
private static Context c;
@Override
public void onCreate(){
super.onCreate();
this.c = getApplicationContext();
}
public static Context getAppContext() {
return this.c;
}
}
declare the above class in your android manifest:
<application android:name="com.xyz.MyApplication"></application>
And then
static {
// load the JNI library
Log.i("JNI", "loading JNI library...");
System.load(MyApplication.getAppContext().getFilesDir().getParentFile().getPath() + "/lib/libxxxxxx.so");
Log.i("JNI", "JNI library loaded!");
}
P.S not tested
You can get Context from your class derived from Application. take a look into example So you can use your application context everywhere:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With