[置顶] 泰晓 RISC-V 实验箱,配套 30+ 讲嵌入式 Linux 系统开发公开课
C Programming on Android
by falcon of TinyLab.org 2013/12/13
Background
There are lots of powerful C/C++ applications in the FLOSS world, it is invaluable if we can reuse them in Android system.
We have introduced the installation of Android NDK in Install ARM Toolchain from Android NDK and we have shown how to cross compile a C program with its cross toolchain.
The cross compiled executable can be run through
$ adb shell
command, but it is not convenient for users for they often have no USB connected and even don’t know what adb is! so, a better method should be: package the program into an APK file and allow users to install it and run it through a generic Android application.
There are two ways to package a C program into an APK:
- Package a standalone C executable into the APK, install it on a directory of the Android device(E.g. /data/) and execute this executable from the Java program
- Call the ‘main’ entry of the C program from Java with the JNI support.
The basic principle of the first way is: If put an executable in the assets/ directory of the application source code, the executable will be packaged into the APK and decompressed into data/data/PACKAGE_NAME/ while installation, please read: Android: Package executable and call it in APK, of course, the executable can also be downloaded, see: Run native executable in Android App.
And as we know, Java is the mother language of Android, to call functions of the native C programs from Java, JNI: Java Native Interface must be applied, to simplify C programming with JNI, Android NDK and some samples are provided by Google, Let’s use hell-jni as our example.
Prepare ndk-build
ndk-build is a tool provided by Android NDK, which simplifies the whole building of the C programs wrapped with JNI symbols.
Let’s download Android NDK, decompress it and set the PATH variable for the ndk-build tool, here my desktop system is Ubuntu.
$ mkdir ~/workspace && cd ~/workspace/
$ wget -c http://dl.google.com/android/ndk/android-ndk-r9b-linux-`uname -m`.tar.bz2
$ tar jxf android-ndk-r9b-linux-`uname -m`.tar.bz2
$ echo "export PATH=\$PATH:~/workspace/android-ndk-r9b" >> ~/.bashrc
$ source ~/.bashrc
Update the project: hello-jni
$ cd android-ndk-r9b/samples/hello-jni
$ android update project -p ./ -s
Build native code into shared libraries
The native C code and the related Makefile are put in jni/, let’s use ndk-build to build the C code and generate a shared library.
$ ls jni/
Android.mk hello-jni.c
$ cat jni/hello-jni.c
cat jni/hello-jni.c | tail -6
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
$ ndk-build
[armeabi] Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
[armeabi] Gdbsetup : libs/armeabi/gdb.setup
[armeabi] Install : libhello-jni.so => libs/armeabi/libhello-jni.so
Build and install a normal Android application
The C function: stringFromJNI() is called from the onCreate() function of src/com/example/hellojni/HelloJni.java:
$ cat src/com/example/hellojni/HelloJni.java | grep -A 10 -B 4 onCreate
public class HelloJni extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
Before start the building, please make sure both Android SDK and Oracle JDK6 are installed, if not, please refer to Java Programming on Android and install them.
Let’s build the Android application:
$ ant debug
$ ant install bin/HelloJni-debug.apk
Conclusion
After installation, it should be able to print ‘Hello from JNI!’.
To do more complicated work, please learn the other samples and the JNI tips.
If want to call existing C executables or not want to add extra our jni/ codes, the way of calling the executables directly from Java code is more convenient.
猜你喜欢:
- 我要投稿:发表原创技术文章,收获福利、挚友与行业影响力
- 知识星球:独家 Linux 实战经验与技巧,订阅「Linux知识星球」
- 视频频道:泰晓学院,B 站,发布各类 Linux 视频课
- 开源小店:欢迎光临泰晓科技自营店,购物支持泰晓原创
- 技术交流:Linux 用户技术交流微信群,联系微信号:tinylab
支付宝打赏 ¥9.68元 | 微信打赏 ¥9.68元 | |
请作者喝杯咖啡吧 |