Line 20: Line 20:
 
==Using the NDK==
 
==Using the NDK==
 
Now you know the basics of coding for an Android phone by just using Java. Integrating native code written in C or C++ is more complicated, but once you have understood how it works, you will get used to it very quick. I assume you have already installed the NDK. If not, follow the steps described [http://developer.android.com/sdk/ndk/index.html here]. The NDK uses the JNI (Java Native Interface). JNI cannot just be used for Android programming, but also for simple Java coding. I think the [http://en.wikipedia.org/wiki/Java_Native_Interface Wikipedia article] is a good way to start understanding the JNI. Afterwards, read carefully through the NDK documentation and understand what the intent of the NDK is. Also read [http://developer.android.com/sdk/ndk/overview.html#samples this] page. On this page you also find all the tutorials you have just downloaded together with the NDK. Read the chapter ''Exploring the hello-jni Sample''. I do not really like the instructions you find on this page and as I was struggling with them, I try to give you another explanation how to get this application running.<br />
 
Now you know the basics of coding for an Android phone by just using Java. Integrating native code written in C or C++ is more complicated, but once you have understood how it works, you will get used to it very quick. I assume you have already installed the NDK. If not, follow the steps described [http://developer.android.com/sdk/ndk/index.html here]. The NDK uses the JNI (Java Native Interface). JNI cannot just be used for Android programming, but also for simple Java coding. I think the [http://en.wikipedia.org/wiki/Java_Native_Interface Wikipedia article] is a good way to start understanding the JNI. Afterwards, read carefully through the NDK documentation and understand what the intent of the NDK is. Also read [http://developer.android.com/sdk/ndk/overview.html#samples this] page. On this page you also find all the tutorials you have just downloaded together with the NDK. Read the chapter ''Exploring the hello-jni Sample''. I do not really like the instructions you find on this page and as I was struggling with them, I try to give you another explanation how to get this application running.<br />
Create a new Android project just like explained on the mentioned page above and open the project ''hello-jni'' from the sample folder. I guess after reading the NDK documentation you are more or less familiar with the structure of this program.  
+
Create a new Android project just like explained on the mentioned page above and open the project ''hello-jni'' from the sample folder. I guess after reading the NDK documentation you are more or less familiar with the structure of this program. An Android app does not have a Main function, but an ''onCreate'' method. This method is called when the application is started. So we have to say inside this method how the app shall look like in the beginning. As we just want to display text, we have have to create a ''TextView'':
 +
<pre>TextView  tv = new TextView(this);</pre>
 +
We want to receive the content of this TextView from a JNI method:
 +
<pre>tv.setText( stringFromJNI() );</pre>
 +
The method ''stringFromJNI()'' must be declared inside the Java part of the program. It is very important to use the key word ''native''. Otherwise you would become an error. So the declaration looks like this:
 +
<pre>public native String  unimplementedStringFromJNI();</pre>
 +
To tell your application where to look for the native methods, you have to include the library you will create later from your C code:
 +
<pre>static {
 +
    System.loadLibrary("hello-jni");
 +
}</pre>
  
  

Revision as of 07:40, 28 November 2011

PAGE IN PROGRESS

Coding for Android on Windows, by Christian Tendyck

This page shall help you to get started to write applications for an Android phone, using Eclipse with the Android SDK and NDK.


Set up your machine

Before you can start coding, you have to download and install a few programs. First of all there is Eclipse. If you are used to Java, you will probably know how to use Eclipse. Furthermore you need to download the Android SDK (Software Development Kit) and ADT (Android Development Tools). The NDK is just needed if you are planing to use native code. That means to write a part of your program in C or C++ or to use C-libraries. Then you also have to install the C/C++ Development tool for eclipse. You can do this via "help/install new software" in eclise. Follow the instructions on this site. To compile C code in eclipse, you need another tool called Cygwin. With Cygwin you can emulate some kind of a Linux console. This is the only way I know to compile C-Code in Eclipse on a Windows machine. Some really common standard C++ libraries cannot be used when compiling with the NDK. But there is a modified version of it called Crystax that allows you to use these libraries.


Create a virtual device

If you do not have an Android phone on your own or if you do not want to upload your apps on your phone everytime to check if your code is working, you can create a virtual device. A virtual device is an emulator of an Android phone. It is very easy to create it. I assume you have already installed the Android SDK. Start Eclipse and click on "Window/Android SDK and AVD Manager". Install the packages you need. That means, download the package with the correct plattform version you want to code for. Then, click "virtual device/new". Choose a name and a target and if you want to, you can also create a virtual SD Card. This is very helpful if you are planing to use the SD Card on your real phone as well. Eclipse will create a file "sdcard.img" that is as big as you have chosen it to be. To mount it as a virtual hard disk, you need a program like ImDisk Virtual Disk Driver. You can create folders on your virtual SD card and use it as any other SD Card.


Your first projects

The Android Developers page offers some tutorials that helps you to get started. As usual, start with a Hello World. It is not really complicated. The page shows you from scratch how to create a new Android project. This page also shows you again how to create a virtual device. Remember for your following projects, that it is always better to use .xml files for entering text that shall be displayed instead of actually writing it directly into your code. That helps to change the text afterwards and to convert your application to other languages. How to use .xml files is explained in the second part of the article mentioned above.
Another tutorial that helped me a lot is the Notepad Tutorial. To be honest, this tutorial is much more complicated than the other one and I haven't really understood everything when I read it, but it definitely helps you to understand, how an Android project works and how you can connect different parts of your program with each other or even with other applications.


Using the NDK

Now you know the basics of coding for an Android phone by just using Java. Integrating native code written in C or C++ is more complicated, but once you have understood how it works, you will get used to it very quick. I assume you have already installed the NDK. If not, follow the steps described here. The NDK uses the JNI (Java Native Interface). JNI cannot just be used for Android programming, but also for simple Java coding. I think the Wikipedia article is a good way to start understanding the JNI. Afterwards, read carefully through the NDK documentation and understand what the intent of the NDK is. Also read this page. On this page you also find all the tutorials you have just downloaded together with the NDK. Read the chapter Exploring the hello-jni Sample. I do not really like the instructions you find on this page and as I was struggling with them, I try to give you another explanation how to get this application running.
Create a new Android project just like explained on the mentioned page above and open the project hello-jni from the sample folder. I guess after reading the NDK documentation you are more or less familiar with the structure of this program. An Android app does not have a Main function, but an onCreate method. This method is called when the application is started. So we have to say inside this method how the app shall look like in the beginning. As we just want to display text, we have have to create a TextView:

TextView  tv = new TextView(this);

We want to receive the content of this TextView from a JNI method:

tv.setText( stringFromJNI() );

The method stringFromJNI() must be declared inside the Java part of the program. It is very important to use the key word native. Otherwise you would become an error. So the declaration looks like this:

public native String  unimplementedStringFromJNI();

To tell your application where to look for the native methods, you have to include the library you will create later from your C code:

static {
    System.loadLibrary("hello-jni");
}


Starting your own application

After working through the tutorials, you should now be able to give your first own applications a try.


PAGE IN PROGRESS

Alumni Liaison

To all math majors: "Mathematics is a wonderfully rich subject."

Dr. Paul Garrett