【发布时间】:2017-09-08 10:44:41
【问题描述】:
我成功地从/system/libs/my_lib.so 目录加载了一个库。如何使用该库中定义的 C/C++ 函数?
public class MainFrom extends Activity {
private static final String LOG_TAG = "MainFrom";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// How to use the functions of test_lib.so?
/*
java.lang.UnsatisfiedLinkError: stringFromC
String s1 = stringFromC(), s2 = stringFromCpp();
Log.w(LOG_TAG, stringFromC());
Log.w(LOG_TAG, stringFromCpp()); */
}
public native String stringFromC();
public native String stringFromCpp();
static {
try {
System.load("/system/lib/test_lib.so");
Log.i(LOG_TAG, "MainFrom. Success!");
} catch (UnsatisfiedLinkError e) {
Log.e(LOG_TAG, "MainFrom. UnsatisfiedLinkError");
}
}
}
stringFromC 和 stringFromCpp 存在于编译为 test_lib.so 的 .c 和 .cpp 文件中
【问题讨论】:
-
你能分享你的 Android.mk 文件以及 .c 和 .cpp 吗?
-
@NISHAnT 如何分享?我正在构建 Cyanogen,我的测试库在“external”文件夹中,c 和 cpp 函数的用法在“framesworks”文件夹中。
-
@NISHAnT 我可以在六个小时内分享它,因为我无法回答自己的问题(声誉低于 100),而且 cmets 中没有完整代码的位置。
-
您好,您能告诉我如何将 test_lib.so 编译到 /system/libs/ 中吗?我已经尝试了一个星期。我的帖子在这里 [link]stackoverflow.com/questions/15910015/…,非常感谢!
标签: android load java-native-interface