6

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 ?

4
  • You get the warning from what? Commented Mar 5, 2013 at 9:18
  • In system.Load I get : Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead Commented Mar 5, 2013 at 9:23
  • Arrived here due to the lint warning saying the same in Eclipse. Spotted this post, which if correct, is a little worrying: code.google.com/p/android/issues/detail?id=43533 Commented Mar 5, 2013 at 20:32
  • You would normally accomplish this using System.loadLibrary() which takes only the name of the library without path, lib prefix, or .so extension (all of which are automatically filled in as appropriate for the platform in use) Commented May 19, 2013 at 18:08

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer but I get : Cannot make a static reference to the non-static method getFilesDir() from the type Context
Just tried it but I get : /data/data/com.mypackage.appname/files instead of what I need : /data/data/com.mypackage.appname/ (no files). Any guesses ?
Works perfectly. Thanks a lot ! Validated + "+1" ;)
1

You can get Context from your class derived from Application. take a look into example So you can use your application context everywhere:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.