Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Monday, July 4, 2011

Android Kill Self-process

I came across a line of source code written by some others today, and it gives me a behavior I have never thought about. The whole application (simplified version) consists of two activities. A user first launches up the main activity and clicks a button in this activity. This starts the second activity. The user then clicks a button in the second activity, and this line is called:


I thought the whole application will be killed and the system will go back to home screen but it does not. Instead the application goes back to main activity and calls its onCreate() again. I am surprised, and then I did a test. I added finish() in main activity after startActivity(). This time, killing the process just closed the whole application.

I tried to search for the reason of this behavior but I cannot find out the answer. Normally, processes are managed by Android system and one should not handle them himself. I believe this behavior is because some resource is not freed yet when the process is killed, and the system thinks the application should relaunch (or it thinks the application is still launching for the first time?).

Monday, February 28, 2011

Using sysinfo in Android NDK

Sometimes you may need to write a program which needs to query some system information (e.g. system uptime). In this case, one may want to call the sysinfo() with struct sysinfo as defined in <sys/sysinfo.h>. Unfortunately, Android NDK does not provide the sysinfo() definition (definition is included since android-9, but I am still having no luck to link it properly). After some searching, I found this thread provides a solution: adding an assembly source (.S). Build it with your project and it works like a charm. The assembly source is attached below for your reference.

Saturday, February 26, 2011

STLport in Android NDK r5b

Android NDK includes a special version of STLport since r5, and let developers to use it by specifying a flag in Android.mk files. However, if you are developing using Makefile instead (like I do), using STLport may be a little bit complicated.

First, since the build-standalone-toolchain script of NDK (also included since r5) can only include stdc++ library into the toolchain it builds, compiling with STLport headers may lead to a list of build errors. In order to prevent this, using prebuilt compilers in the NDK ("the hard way" as described in readme) may be a better choice. In this case, you will have to take care with sysroot.

Second, developing with this special STLport has some limitations as mentioned in readme: Exception and RTTI are not supported. Remember to build every cpp source files with -fno-exceptions and -fno-rtti.

Friday, June 11, 2010

Access Java Class Variables from NDK

Access Java class variables from NDK (non-static function) in C:


  • env and obj are the first two parameters of the NDK function
  • "Ljava/lang/String;" is called the field descriptor of type String


Field Descriptor of other Variable Types:


  • Remember the semi-colon
  • Replace "Get" by "Set" for Set Field Functions

Field Descriptor Type Get Field Function
Z boolean GetBooleanField
B byte GetByteField
C char GetCharField
S short GetShortField
I int GetIntField
J long GetLongField
F float GetFloatField
D double GetDoubleField
Ljava/lang/String; String GetObjectField
[I int[] GetObjectField, and then cast it to an array
jintArray *arr = reinterpret_cast<jintArray*>(&data);
[Ljava/lang/Object; object[] GetObjectField, and then cast it to an array

Wednesday, June 9, 2010

Android NDK Differences with Linux

  • C++ STL is not included Update: stlport is included since revision 5
  • In <mntent.h>, function setmntent() does not exist
  • In <dirent.h>, functions seekdir(), telldir() does not exist
  • Macro INET_ADDRSTRLEN is not defined in <netinet/in.h> Update: added since revision 5b
  • Macro PTHREAD_EXPLICIT_SCHED is not defined in <pthread.h>
  • Marcos WEXITSTATUS, WTERMSIG, WIFSIGNALED, etc are only defined in <sys/wait.h>  (One can also find them in <stdlib.h>  for Linux)
  • Although function ftruncate64() is included in header <unistd.h>, it is not included in the prebuilt libraries. Hence it causes linking error while this function is used
  • Although you can include <sys/sysinfo.h>, struct sysinfo does not exist (it is disabled in the header file). If you have to use it, try this way.

Monday, June 7, 2010

Android NDK Development

Some remarks I would like to make while developing on Android NDK:
  • Package, class and function names in Java are specified by function name in C/C++
  • For all JNI wrapper functions, remember to add JNIEnv *env and jobject object to the front of the parameter list. They represent the environment pointer and the Java class object which calls this function.
  • Parameters are passed into wrapper functions by value. To the best of my knowledge, there is no way to pass in parameters by reference.
  • If the wrapper source is in cpp, remember to include all function prototypes in
  • Include all source files in subdirectory src: LOCAL_SRC_FILES := $(addprefix src/,$(notdir $(wildcard $(LOCAL_PATH)/src/*.cpp $(LOCAL_PATH)/src/*.c)))
  • Make use of include $(call all-subdir-makefiles) to make a recursive building hierarchy
  • While using variable LOCAL_STATIC_LIBRARIES, try putting the highest level libraries in the front to prevent linking error
  • In order tot check the output of stdout and stderr, you have to set by using adb (included in Android SDK), and then you can view them using ./adb logcat
    ./adb shell stop
    ./adb shell setprop log.redirect-stdio true
    ./adb shell start
    Update: Stopping and starting the shell may cause the whole emulator hangs now. To redirect stdout/stderr, add "log.redirect-stdio=true" to /data/local.prop on the device (create one if the file does not exist) and restart the device
  • STL library is not included in NDK. If you need to use STL library in your NDK code, you have to use stlport
  • DO NOT create thread inside NDK
  • Directory /tmp DOES NOT exist in Android. I have tried to search for it for hours without any luck. According to what I found, applications should manage their own files (temp or non-temp) within the local filesystem sandbox on their own
  • There is a script to let you build a NDK toolchain since NDK revision 5
  • Linking with "-lpthread" is not required, including that will cause error