Android NDK limitations?

2019-03-15 01:06发布

问题:

I have a question about the limitations of what you can do in native code on the Android platform.

Basically I have developed a library in native C code that uses UDP sockets for SIP/RTP and uses OpenAL for audio recording/playback - basically the whole application. The idea is to have as much as possible in native C code rather than Java code. I want to do this because I am going to use it on other platforms as well.

My question then is simply - is it possible to just use the Java for the GUI and then all processing in native code? What will happen when my native code tries to create a socket, bind it, record audio, play it, etc - since it is in native code, do I need to setup permissions for it (such as application accessing microphone and whatnot) or will it just bypass this stuff since its native code? Can native code do pretty much anything it wants on Android like on PCs?

Sorry if its unclear; just tell and I'll try to improve it

Thanks

回答1:

You can do pretty much anything you want in native code, but the only OS-level thing really supported is OpenGL, OpenSL, and some number-crunching libraries (compression, math, etc).

However, at any time you're free to use the JNI to call a Java method, so you could use the standard Android API for networking (classes like Socket, etc). Obviously, since the call is going through the Java API, all the normal Android permissions apply (like android.permission.INTERNET).

EDIT: As elaborated in the comments, the standard libraries that are part of the NDK do have support for sockets.



回答2:

You still need your application to have permissions. For example, your native sockets will not work without android.permission.INTERNET in the manifest.

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

Another option is to create the socket at the Java layer and pass it down. Here's an example of interacting with the socket in native land, see the method org_..._OpenSSLSocketImpl_connect():

http://www.netmite.com/android/mydroid/dalvik/libcore/x-net/src/main/native/org_apache_harmony_xnet_provider_jsse_OpenSSLSocketImpl.cpp



回答3:

The Native Android API is a nice article for NDK.

is it possible to just use the Java for the GUI and then all processing in native code?

Yes. And you need to set appropriate permissions to your AndroidManifest.

record audio, play it,

You need to use OpenSL ES API for recording and playing audio in the native side. It means your application should be for Android 2.3 or later.

Or, NVIDIA provides a framework that allow we to be able to develop using C++ for Android events, sensors, audio and so on even though for Android 2.2 or earlier.

Tegra Resources - Android SDK & NDK sample applications and documentation



回答4:

You may like to check out csipsimple which is a example of using the pjsip sip library (which is written in C) in a java android application.

I haven't looked into how it does the sockets communication but it should give you a more complete example of what you are trying to do.