端口本机 C++ Android 上的非静态二进制文件
我对 Android 很陌生,我有一些问题想问各位专家! 好吧,我的问题... 我实现了一个基于套接字编程的客户端-服务器应用程序。服务器对一些数据包进行编码,通过套接字将它们发送到客户端,然后客户端对它们进行解码。 我用两台 Linux 机器测试了代码,它工作正常,但在我的实验中,需要包含另一个节点(这将是 Android)。因此服务器(linux机器)将对数据包进行编码并通过套接字发送到client1(linux机器)和client2(Android)。 出于这个原因,我想将我的代码(C++ 中的)的本机二进制文件移植到 Android。 我可以通过什么方式做到这一点?
请给我一些帮助! 我真的完全卡住了!
谢谢,
泽尼娅
Im quite new on Android and I have some question for all of you who are experts!
Ok, my problem...
I implemented a client-server application based on socket programming. The server encode some packets, send them to the client through a socket and the clinet decode them.
I tested the code with two linux machines and it works fine but in my experiment it is required to include another node (this will be the Android). So the server (linux machine) will encode the packets and send through socket to client1(linux machine) and client2(Android).
For this reason I want to port the native binary of my code (which is in C++) to Android.
In which way could I do this?
Please give me some help!
Really im totally stucked!
Thanks,
Zenia
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当你想将本机代码 C/C++ 移植到 android 时,你需要查找 android ndk 和 jni
http://developer.android.com/sdk/ndk/index.html
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
ndk 中有一些关于如何使用的示例来做到这一点。
请注意,C 是完全支持的,但 C++ 支持 api 在 Android 上非常有限(列表位于 ndk 的文档中),因此您在移植代码时可能会遇到问题。
如果可以的话,我建议直接使用 java,因为使用 JNI 很乏味,哈哈
when you want to port native code C/C++ to android you want to look up android ndk and jni
http://developer.android.com/sdk/ndk/index.html
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
There are some examples in the ndk on how to do this.
be warned that C is fully supported but C++ support apis is very limited on android (the list is in the docs of the ndk) so you might have problems porting your code.
I would recommend using directly java if you can, since working with JNI is tedious lol
你还能如何移植这个?开始学习 android 我做了一个快速检查,注意到它的 sdk 使用 java 你可以通过查看
开始http://developer.android.com/reference/java/net/Socket.html
how else can you port this? start learning android i did a quick check noticed it's sdk uses java you can start by looking at
http://developer.android.com/reference/java/net/Socket.html
感谢您的回复,
我首先尝试使用套接字完全用java编写自己的代码,但是我必须将一些优化的库移植到Android,我可以弄清楚如何做到这一点(我可以移植一个简单的小型库,但不能移植我想要的)。我放弃了,现在我正在尝试使用 jni 和 ndk。但是我不知道我是否确实可以移植我的二进制文件,因为它是非静态的(比如 hello world)。这就是我问的原因。如果其他人有这方面的经验,请告诉我。非常感谢,
泽尼娅
Thanks for the reply,
I first tried to write my own code totally in java using sockets, however i had to port some optimized libraries to Android and I could figure out how to do that (i could port a simple small library but not the one that I wanted). I gave up and I right now im trying to play with jni and ndk. however i dont know if indeed i could port my binary as it is non static (like hello world). Thats why im asking. if anyone else have some experince on that please let me know. thanks a lot,
Zenia
您可能应该做的是安装 SDK 和 NDK 并构建 hello-jni ndk 示例。
然后查找如何从 C 访问 android logcat 输出,并为此编写一个类似 printf 的漂亮的小包装器(可能使用底层函数的 vargs 版本),以便您可以轻松地从本机代码生成调试输出。
然后将您的本机可执行文件移植到 hello-jni 示例代码上,这样您将拥有一个 java 包装器,它除了通过调用本机代码来启动之外几乎不执行任何操作。请记住不要在 UI 线程或该线程下调用的本机代码中进行太多处理,否则您将面临应用程序不响应超时的风险。
也可以(ab)使用 ndk 的 gcc 来生成没有 java 包装器的独立本机可执行文件,但不鼓励这样做。在未 root 的手机上很难找到可靠的位置来安装它们,而且 Android 的进程管理对未知的本机进程并不满意。换句话说,这是一条适合在您自己的设备上进行个人实验的路径,但对于部署到其他设备的应用程序来说,这是一条困难且无法面向未来的路径。
What you should probably do is install the SDK and NDK and build the hello-jni ndk example.
Then look up how to access the android logcat output from C, and write yourself a nice little printf-like wrapper for that (probably using the vargs version of the underlying function) so you can easily generate debug output from your native code.
Then graft your native executable onto the hello-jni example code, so you'll have a java wrapper that does very little other than start things with a call to the native code. Just remember not to do much processing in the UI thread or native code called under that thread, or you will risk an application not responding timeout.
It is also possible to (ab)use the ndk's gcc to produce stand alone native executables with no java wrapper, but this is discouraged. It's hard to find a reliable place to install them on a non-rooted phone, and android's process management isn't happy about unknown native processes. In other words, that's a path that's fine for personal experiments on your own device, but a difficult and non-future-proof one for an application deployed to others.