Joz*_*oze 6 java-native-interface android warnings
我在我的一个应用程序中遇到问题,我有以下代码加载应用程序所需的lib(JNI):
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!");
}
Run Code Online (Sandbox Code Playgroud)
所以我得到了警告:"Do note hardcode use Context.getFilesDir().getPath() instead"
这是完全正确的(它不会在每台设备上都可移植).问题是,因为我使用静态我无法呼唤Context.getFilesDir().getPath()
.
你有什么想法我可以继续这样做吗?
Nul*_*ter 12
您的警告绝对清楚,请尝试以下方式:
制作以下课程:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的android清单中声明上面的类:
<application android:name="com.xyz.MyApplication"></application>
Run Code Online (Sandbox Code Playgroud)
然后
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!");
}
Run Code Online (Sandbox Code Playgroud)
PS未经测试