| 1 | === Android === |
| 2 | |
| 3 | 1. Installing SDK.[[BR]] |
| 4 | You will want to follow these instructions.[[BR]] |
| 5 | http://developer.android.com/sdk/installing.html [[BR]][[BR]] |
| 6 | Note, when it asks you for installation directories associated with the android SDK, you should make the directories without spaces in them. This causes issues later that can be resolved but to save a minor headache, create them without spaces. |
| 7 | 1. Install NDK.[[BR]] |
| 8 | Follow the instructions on this page.[[BR]] |
| 9 | http://developer.android.com/sdk/ndk/index.html [[BR]][[BR]] |
| 10 | Again, install this without spaces in directory names. I installed mine at c:/Android-ndk/ |
| 11 | 1. Code Talk.[[BR]] |
| 12 | a. Downloading a file.[[BR]] |
| 13 | To download a file in Java we were able to use simple stream input and output from a URL object. The code below is pretty straight forward. |
| 14 | {{{ |
| 15 | //Direct url to download the file from |
| 16 | URL u = new URL("http://home.cddot.net/hello-jni"); |
| 17 | URLConnection conn = u.openConnection(); |
| 18 | conn.connect(); |
| 19 | |
| 20 | //File name to save the file. |
| 21 | //Notice the /data/data/com.example.helloandroid/ part of the directory. |
| 22 | //This is the directory of our application to write files into. |
| 23 | //File name saved within this directory could be additional folders. |
| 24 | String filename = "/data/data/com.example.helloandroid/tmpfile.bin"; |
| 25 | InputStream input = new BufferedInputStream(u.openStream()); |
| 26 | OutputStream output = new FileOutputStream(filename); |
| 27 | |
| 28 | byte data[] = new byte[1024]; |
| 29 | int count = 0; |
| 30 | |
| 31 | //step through the input file and write it to the filename opened earlier. |
| 32 | while((count = input.read(data)) != -1) { |
| 33 | output.write(data, 0, count); |
| 34 | } |
| 35 | |
| 36 | //Clean up file handles. |
| 37 | output.flush(); |
| 38 | output.close(); |
| 39 | input.close(); |
| 40 | }}} |
| 41 | a. Change permissions of a file. |
| 42 | Next you will be required to change the newly downloaded file into execute mode within the linux environment on the Android OS. To do this is a simple Runtime execution. |
| 43 | {{{ |
| 44 | //assume process variable was already declared above (Process process = null) |
| 45 | //This line changes permissions of the file to be executable by the owner. |
| 46 | process = Runtime.getRuntime().exec("chmod 744 " + filename); |
| 47 | }}} |
| 48 | a. Run your binary file and read the output from stdout to be printed in the log file caught by Dalvik Debuger. |
| 49 | This step will execute your binary file and catch the output of stdout. For the purpose of this example our binary catches "hello world" from the binary. |
| 50 | {{{ |
| 51 | //process still declared from before |
| 52 | process = Runtime.getRuntime().exec(filename); |
| 53 | |
| 54 | String s = null; |
| 55 | process.waitFor(); |
| 56 | stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 57 | while((s=stdInput.readLine()) != null) { |
| 58 | //For your sanity when viewing logs in Dalvik, I would add tags...like "WCG" so that you can filter on the logs later. |
| 59 | Log.i("WCG", s); |
| 60 | } |
| 61 | }}} |
| 62 | a. Check to see what the contents of the application directory. |
| 63 | This will output the file directory structure to the debugger. From our binary we actually created a new file and wrote data into it. This proves that many of the basics allowed for BOINC exist. |
| 64 | {{{ |
| 65 | process = Runtime.getRuntime().exec("ls -la /data/data/com.example.helloandroid/"); |
| 66 | process.waitFor(); |
| 67 | stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 68 | |
| 69 | while((s=stdInput.readLine()) != null) { |
| 70 | Log.i("WCG", s); |
| 71 | } |
| 72 | }}} |
| 73 | a. View the output of Dalvik Debugger |
| 74 | This should be simple Here is a screen shot of what we are expected to see. |
| 75 | [[Image(Dalvik_debug_manager.png)]] |
| 76 | 1. Compile code for the NDK |
| 77 | a. Pre-req. |
| 78 | You will need to have cygwin installed as well as make version 1.81 from their repository. |
| 79 | a. Copy the hello-jni project from samples within Android ndk. When this is ready you will see a file called hello-jni.c that exists within the jni folder. This will be where your Makefile and source code should exist. |
| 80 | a. Modify c code that will be compiled. For this it is a VERY simple c program with basic c calls. I just deleted the contents of hello-jni.c and replaced with the contents below for this example. |
| 81 | {{{ |
| 82 | #include <stdio.h> |
| 83 | #include <string.h> |
| 84 | |
| 85 | int main() { |
| 86 | FILE *file; |
| 87 | file = fopen("/data/data/com.example.helloandroid/file.txt", "a+"); |
| 88 | |
| 89 | //Write text to file to give it some size |
| 90 | fprintf(file, "HOWDY"); |
| 91 | //Write to stdout. |
| 92 | printf("HELLO WORLD\n"); |
| 93 | |
| 94 | fclose(file); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | }}} |
| 99 | a. Now, since I did not want the application to spawn from a java instance, I had to modify the make file to not build a shared object as this could have caused issues later for boinc.[[br]] |
| 100 | I changed this line within the Android.mk file |
| 101 | {{{ |
| 102 | include $(BUILD_SHARED_LIBRARY) |
| 103 | }}} |
| 104 | to |
| 105 | {{{ |
| 106 | include $(BUILD_EXECUTABLE) |
| 107 | }}} |
| 108 | a. Compiled the code within cygwin. |
| 109 | You will need to navigate to the folder where hello-jni project exists. |
| 110 | {{{ |
| 111 | cd /cygdrive/c/directorys/ |
| 112 | }}} |
| 113 | Once you are in that directory, you will need to call ndk-build. This may vary on where you installed the ndk. |
| 114 | {{{ |
| 115 | $ /cygdrive/c/cygwin/android-ndk-r6/ndk-build |
| 116 | Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver |
| 117 | Gdbsetup : libs/armeabi/gdb.setup |
| 118 | Compile thumb : hello-jni <= hello-jni.c |
| 119 | Executable : hello-jni |
| 120 | Install : hello-jni => libs/armeabi/hello-jni |
| 121 | }}} |
| 122 | a. Once this is complete you should have the file called hello-jni in your libs/armeabi/ directory of your project. I uploaded this to my personal server as shown by the code of the java URL earlier. |
| 123 | 1. Run your application in emulator |
| 124 | You are now ready to run it on your emulator with dalvik debugger running and you should see similar output as I have noted above. |
| 125 | 1. Several notes about this setup. |
| 126 | a. Limitations within c/c++ applications. |
| 127 | i. Several include files that could be considered basic do not work. |
| 128 | We have tested some of the stream includes and those appear to be missing[[br]] |
| 129 | i. What version of the c-std is being used. |
| 130 | This causes an issue because a simple for loop was not recognized and we were required to shift that to a while loop instead. |
| 131 | a. Possible solutions |
| 132 | i. cmake was ported and is able to use boost libraries. I am not sure at this time which libraries are included from boost. |
| 133 | i. convert source code to fit this standard which should still allow for compiles across other standards |
| 134 | a. Hardware thoughts |
| 135 | i. Overheating of devices. These machines are not designed well for 100% cpu usage over many hours (no fans, they use the case to disperse heat) |
| 136 | i. Battery protection. This is an issue where the device pulls more power than is being supplied by the power adapter. For example, a usb connection to a computer provides 150mA. Where a power adapter that uses a usb cable can provide 1.5 A. A device running will need to detect what power source they are using or find out if after running 100% cpu for 5 minutes the battery has actually lost charge. |
| 137 | i. Dynamic interface sizes. Since running on various devices that run the same application, an interface design will have to be fairly flexible to be viewable from a phone size (more height than width) and a tablet screen resolution. Not all screens within classes are even the same size, so an additional challenge. |