Tuesday, March 20, 2012

Android Styles and Themes

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.

For example, by using a style, you can take this layout XML:

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" />

And turn it into this:
<TextView
style="@style/CodeFont"
android:text="@string/hello" />

All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute. You'll see the definition for this style in the following section.

A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above). When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont style as a theme for an Activity and then all text inside that Activity will have green monospace font.
Defining Styles

To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.

For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the <item> can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>

Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute. This example style can be referenced from an XML layout as @style/CodeFont (as demonstrated in the introduction above).

The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.
Remember, a style that you want to use as an Activity or application theme is defined in XML exactly the same as a style for a View. A style such as the one defined above can be applied as a style for a single View or as a theme for an entire Activity or application. How to apply a style for a single View or as an application theme is discussed later.
Inheritance

The parent attribute in the <style> element lets you specify a style from which your style should inherit properties. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform. (See Using Platform Styles and Themes, below, for information about inheriting from styles defined by the Android platform.) For example, you can inherit the Android platform's default text appearance and then modify it:

<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
Notice that there is no parent attribute in the <style> tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red.
You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>

This inherits from both CodeFont and CodeFont.Red styles, then adds the android:textSize property.
Note: This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference a built-in style, such as TextAppearance, you must use the parent attribute.
Style Properties

Now that you understand how a style is defined, you need to learn what kind of style properties—defined by the <item> element—are available. You're probably familiar with some already, such as layout_width and textColor. Of course, there are many more style properties you can use.
The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributes can be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an <EditText> element, like this:

<EditText
android:inputType="number"
... />
You can instead create a style for the EditText element that includes this property:
<style name="Numbers">
<item name="android:inputType">number</item>
...
</style>
So your XML for the layout can now implement this style:
<EditText
style="@style/Numbers"
... />

This simple example may look like more work, but when you add more style properties and factor-in the ability to re-use the style in various places, the pay-off can be huge.
For a reference of all available style properties, see the R.attr reference. Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specific View class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.

Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View. For example, style properties for a theme can hide the application title, hide the status bar, or change the window's background. These kind of style properties do not belong to any View object. To discover these theme-only style properties, look at the R.attr reference for attributes that begin with window. For instance, windowNoTitle and windowBackground are style properties that are effective only when the style is applied as a theme to an Activity or application. See the next section for information about applying a style as a theme.

Note: Don't forget to prefix the property names in each <item> element with the android: namespace. For example: <item name="android:inputType">.
Applying Styles and Themes to the UI
There are two ways to set a style:
• To an individual View, by adding the style attribute to a View element in the XML for your layout.
• Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.
When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.

To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them. Any View that does not support the properties will ignore them. If a View supports only some of the properties, then it will apply only those properties.
Apply a style to a View
Here's how to set a style for a View in the XML layout:

<TextView
style="@style/CodeFont"
android:text="@string/hello" />
Now this TextView will be styled as defined by the style named CodeFont. (See the sample above, in Defining Styles.)
Note: The style attribute does not use the android: namespace prefix.
Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
<application android:theme="@style/CustomTheme">

If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead.
Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:

<activity android:theme="@android:style/Theme.Dialog">
Or if you want the background to be transparent, use the Translucent theme:
<activity android:theme="@android:style/Theme.Translucent">
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>

(Note that the color needs to supplied as a separate resource here because the android:windowBackground attribute only supports a reference to another resource; unlike android:colorBackground, it can not be given a color literal.)
Now use CustomTheme instead of Theme.Light inside the Android Manifest:
<activity android:theme="@style/CustomTheme">

Select a theme based on platform version
Newer versions of Android have additional themes available to applications, and you might want to use these while running on those platforms while still being compatible with older versions. You can accomplish this through a custom theme that uses resource selection to switch between different parent themes, based on the platform version.

For example, here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under res/values (typically res/values/styles.xml):
<style name="LightThemeSelector" parent="android:Theme.Light">
...
</style>
To have this theme use the newer holographic theme when the application is running on Android 3.0 (API Level 11) or higher, you can place an alternative declaration for the theme in an XML file in res/values-v11, but make the parent theme the holographic theme:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>

Now use this theme like you would any other, and your application will automatically switch to the holographic theme if running on Android 3.0 or higher.
A list of the standard attributes that you can use in themes can be found at R.styleable.Theme.
For more information about providing alternative resources, such as themes and layouts, based on the platform version or other device configurations, see the Providing Resources document.
Using Platform Styles and Themes

The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "@android:style/Theme.NoTitleBar".
The R.style reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code:
• Android Styles (styles.xml)
• Android Themes (themes.xml)

These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework.
For more information about the syntax used to create styles in XML, see Available Resource Types: Style and Themes.

For a reference of available style attributes that you can use to define a style or theme (e.g., "windowBackground" or "textAppearance"), see R.attr or the respective View class for which you are creating a style.


Monday, March 19, 2012

Making the Android UI Fast and Efficient



Efficient android programming Tips


General Tips:
  1. Add Two Numbers: Your first non-tutorial application should be to take two numbers and add them together. It sounds too simple. You will spend some hours getting the layouts, the callbacks, and onPause/onResume to work correctly. Do it.
  2. It is Java: You work in Java for most of your Android programming. Don't spend time praising it. Don't spend time complaining about it. Just work with it.
  3. Love RelativeLayout: Most of the tutorials use LinearLayout, but you will find that RelativeLayout is truly useful. Many layouts, like the GridLayout aren't used much at all. Play around with RelativeLayout. See an example from this question.
  4. Use fill_parent with a top level RelativeLayout: A surprisingly common and hard to find problem is putting a wrap_content in a top level RelativeLayout and then wondering why unrelated fields far down in the hierarchy are rendering strangely.
  5. Use empty layout items: You will often use empty items in your layouts just for positioning other layouts. For example, you might use an empty TextField, of width=0 and height=0 and centerInParent='True' just to anchor things relative to the middle of the screen. Also, you might have an empty TextField or LinearLayout so that you can give a layout_weight=1 to it and have it take up more screen space.
  6. Set a layout background color: If you are having trouble figuring out your layout, try setting the background colors on some objects. It can highlight your mistakes faster than other tools, and shows some surprises that the IDE red box doesn't always help.
  7. Download Apps-For-Android: This is a big chunk of useful source code for a half dozen applications. It can supplement the sample applications nicely and show different coding style solutions. Grab it using svn co http://apps-for-android.googlecode.com/svn/trunk/ apps-for-android-read-only
  8. Download the source: You need the Android source to solve some problems or, more likely, get past holes in the documentation. Your copy does not need to be perfect or kept up to date. You can learn to use the repo command, or just visit http://android.git.kernel.org/ for a snapshot.
  9. Learn to search your source: The fastest solution to many problems is to find where a particular parameter is used in some other source. Put a copy or link to the sample applications, apps-for-android applications, and any other source you have under one directory tree. Use "grep -ir funky_parameter sample_code/" or your favorite searching routine to quickly find some code that uses that parameter.
  10. Use Eclipse: Even you have a favorite editor or IDE you have used for years, use Eclipse for Android development. It is good enough as an IDE and is really part of the development tools suite. Any time you spend trying to jury-rig your IDE to work is time you didn't code.
  11. Learn Eclipse: Learn a few new tricks with Eclipse every day. Some of my favorite commands were found reading this list and this question.
  12. Get help when starting out: The quantity of readable material can be overwhelming. Setting up the environment can be tricky. Going to an Android Meet-up or users group can help get over the initial hump.
  13. Code every day: Android code will be frustrating. Don't allow yourself to stop because you get stuck. Play around with the tools, step through a sample application, read one of the articles, or read a blog to ease the frustration. Then write some more code.
  14. Lurk on IRC: Connect your favorite IRC reader to irc.freenode.net's #android-dev channel. Leave it up in the background. Only ask questions after you have spent ten minutes trying to figure it out on your own.
  15. Use two monitors: Working on your laptop in the coffee shop will slow you down. You need to spread out windows. At very least, you will want a full screen Eclipse session, the emulator, and a browser with the tutorial to all be easily visible. Three screens may work even better.
  16. Reformat XML files: The layout editor makes a hash of the XML files. Use the 'source/format' command to put them in a reasonable form. You will want to check the "Eclipse/Windows/Preferences/XML/XML Files/Editor/Formatting/Split XML attributes each on a new line" check box. Then use shift-ctrl-F to format it.
  17. Edit XML files with the text editor: After the first couple of layout screens, stop using the slow moving 'properties' gui to change properties. Drop the items using the gui. Use the up/down arrows on the far right outline to get the hierarchy of views and layouts correct, then edit the XML file directly and use the ctrl-space shortcut to bring up possible completions and explanations of the properties.
  18. Plan on Piracy in the MarketPlace: Google has not made the MarketPlace a Happy Place. Apps are copied and reposted with changed names to funnel money around. Lots of scammers are trying to game the system in many ways. Don't plan on making a living solely from AppStore revenues nor plan on Google being responsive.
  19. Use LogCat: It can be difficult in Android to figure out 'what went wrong'. Run the application in the debugger and look at the logcat window. I found it worthwhile to add a new perspective with just a maximized logcat window. If you like to have a colored LogCat output in a separate window try this tool: Colored LogCat
  20. Explore the tools directory: There a lot helpful tools in the sdk's tools directory, such as hierarchyviewer and layoutopt. Each is helpful and there is no shortcut to learning about each tool one at a time.
Important Tips:
  1. Don't forget to free resources after use.
    Lot of resources like Cursors are overlooked. Free them too.
  2. Don't Use magic Numbers.
    values[0] is meaningless. The framework provides very useful accessors like
    values[SensorManager.DATA_X]
  3. "Make use of onPause()/onResume to save or close what does not need to be opened the whole time."
protected void onResume() {
mSensorManager.registerListener(...);
}
protected void onStop() {
  mSensorManager.unregisterListener(...);
  super.onStop();
}

4.       Make your Android UI Fast and Efficient from the Google I/O has a lot of useful UI Performance tips.
  • Optimize Judiciously
  • Avoid Creating Objects
  • Performance Myths
  • Prefer Static Over Virtual
  • Avoid Internal Getters/Setters
  • Use Static Final For Constants
  • Use Enhanced For Loop Syntax
  • Avoid Enums Where You Only Need Ints
  • Use Package Scope with Inner Classes
  • Use Floating-Point Judiciously
  • Know And Use The Libraries
  • Use Native Methods Judiciously
Best Practices for User Interfaces:
·         1 Read the UI guidelines
·         2 Understand and design for touch mode
·         3 But, support multiple interaction modes
·         4 Use notifications and the window shade
·         5 Support interaction between applications
·         6 Keep your UI fast and responsive
·         7 Use widgets and live folders
·         8 Handle screen orientation changes
·         9 Use images wisely
·         10 Use layouts that adapt to multiple devices
·         And from SO:



Thursday, March 1, 2012

Using NDK to Call C code from Android Apps Part-2

Updated for NDK 1.6

While Android SDK is great for application development, every once in a while you may need access to native code. This code is usually done in C. While you were able to access native code via Java Native Interface (JNI) all along, the process was rather hard. You would've typically had to compile everything on your host computer for the target architecture, requiring you to have the entire toolchain on your development machine.

Android NDK (Native Development Kit) simplifies working with native code. It includes the entire toolchain needed to build for your target platform (ARM). It is designed to help you create that shared library.




Note that native code accessible via JNI still runs inside the Dalvik VM, and as such is subject to the same life-cycle rules that any Android application lives by. The advantage of writing parts of your app code in native language is presumably speed in certain cases.

Note: I'm using <NDKHOME> to refer to the root directory in which you installed your NDK. For me that's /Users/marko/WorkArea/android-ndk-1.6_r1. I'm assuming all other directories and files are relative to your Eclipse project root, in my case /Users/marko/Workspace/Android/NDKDemo.

Overview




We are roughly going to do this:

1. Create the Java class representing the native code
2. Create the native code header file
3. Implement the native code by writing your C code
4. Compile everything and build you Shared Library
5. Use your native code inside Android activity


Create Native Library

This is just a Java file that lives in standard src directory in your Eclipse project. It serves as the glue to the native code that we'll write later.

/src/com.marakana/NativeLib.java
Code:

package com.marakana;

public class NativeLib {

  static {
    System.loadLibrary("ndk_demo");
  }
  
  /** 
   * Adds two integers, returning their sum
   */
  public native int add( int v1, int v2 );
  
  /**
   * Returns Hello World string
   */
  public native String hello();
}



Create C Header File
In your project bin directory (in my case, <EclipseWorkspace>/NDKDemo/bin), run javah tool to create the JNI header file.

Next, create a jni directory in your project directory (in my case, <EclipseWorkspace>/NDKDemo/jni).

Next, copy the JNI header from <EclipseWorkspace>/NDKDemo/bin to <EclipseWorkspace>/NDKDemo/jni

Here's my command line:

Code:

NDKDemo/bin$ javah -jni com.marakana.NativeLib
NDKDemo/bin$ mv com_marakana_NativeLib.h ../jni/


Write the C Code

In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndk_demo.c file. This is where we'll implement the native code. To start, copy the function signatures from the header file, and provide the implementation for those functions. In this example, the header file looks like this:

<EclipseWorkspace>/NDKDemo/jni/com_marakana_NativeLib.h
Code:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_marakana_NativeLib */

#ifndef _Included_com_marakana_NativeLib
#define _Included_com_marakana_NativeLib
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_marakana_NativeLib
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     com_marakana_NativeLib
 * Method:    hello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


And the corresponding implementation looks like this:

<EclipseWorkspace>/NDKDemo/jni/ndk_demo.c
Code:

#include "com_marakana_NativeLib.h"

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv * env, jobject obj) {
  return (*env)->NewStringUTF(env, "Hello World!");
}

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv * env, jobject obj, jint value1, jint value2) {
  return (value1 + value2);
}



Build The Library

To build the library, first we need to create a makefile for how to compile the C code:

<EclipseWorkspace>/NDKDemo/jni/Android.mk
Code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c

include $(BUILD_SHARED_LIBRARY)


Next, we need to tell NDK how to build the shared library and put it in the correct place inside the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndk_demo/ and inside this folder create the Application file:

<NDKHOME>/apps/ndk_demo/Application
Code:

APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES      := ndk_demo


Next, create a symbolic link <NDKHOME>/apps/ndk_demo/project to your Eclipse project:

ln -s ~/Workspace/Android/NDKDemo <NDKHOME>/apps/ndk_demo/project

If you are on Windows, or another OS that doesn't support symbolic links, you may have to copy entire Eclipse project into <NDKHOME>/apps/ndk_demo/project directory, then copy back to Eclipse. I'm running all this on Mac OS X 10.6 and I assume Linux-type shell.

You can now to to your <NDKHOME> and run make APP=ndk_demo

The output should look lie this:

Code:

android-ndk-1.5_r1$ make APP=ndk_demo
Android NDK: Building for application 'ndk_demo'    
Compile thumb  : ndk_demo <= sources/ndk_demo/ndk_demo.c
SharedLibrary  : libndk_demo.so
Install        : libndk_demo.so => apps/ndk_demo/project/libs/armeabi


You can now refresh your Eclipse project and you should /lib/ directory containing your libndk_demo.so file.

Calling Native Code from Java

So now that we have the native C library implemented, compiled, and placed in the right place, let's see how we can call it from our Activity. It's actually rather simple - you just have to instantiate the instance of your NativeLib class and from there on, it's just a regular Java object.

/src/com.marakana/NDKDemo.java
Code:

package com.marakana;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NDKDemo extends Activity {
  NativeLib nativeLib;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nativeLib = new NativeLib();
    String helloText = nativeLib.hello();

    // Update the UI
    TextView outText = (TextView) findViewById(R.id.textOut);
    outText.setText(helloText);

    // Setup the UI
    Button buttonCalc = (Button) findViewById(R.id.buttonCalc);

    buttonCalc.setOnClickListener(new OnClickListener() {
      TextView result = (TextView) findViewById(R.id.result);
      EditText value1 = (EditText) findViewById(R.id.value1);
      EditText value2 = (EditText) findViewById(R.id.value2);

      public void onClick(View v) {
        int v1, v2, res = -1;
        v1 = Integer.parseInt(value1.getText().toString());
        v2 = Integer.parseInt(value2.getText().toString());

        res = nativeLib.add(v1, v2);
        result.setText(new Integer(res).toString());
      }
    });
  }
}



The UI for this example is not that significant, but I'm going to include it here for the sake of completeness.

/res/layout/main.xml
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="NDK Demo"
    android:textSize="22sp" />
  <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/textOut"
    android:text="output"></TextView>
  <EditText android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/value1"
    android:hint="Value 1"></EditText>
  <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="+"
    android:textSize="36sp"></TextView>
  <EditText android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/value2"
    android:hint="Value 2"></EditText>
  <Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/buttonCalc"
    android:text="="></Button>
  <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="result"
    android:textSize="36sp" android:id="@+id/result"></TextView>
</LinearLayout>



Output


Source Code
http://marakana.com/static/tutorials/NDKDemo.zip
http://marakana.com/static/tutorials/NDKHOME.zip



Using NDK to Call C code from Android Apps Part-1

The Android NDK is a set of tools that allows Android application developers to embed native machine code compiled from C and/or C++ source files into their application packages.
While you were able to access native code via Java Native Interface (JNI) all along, You would’ve typically had to compile everything on your host computer for the target architecture, requiring you to have the entire toolchain on your development machine.
Android NDK (Native Development Kit) simplifies working with native code. It includes the entire toolchain needed to build for your target platform (ARM). It is designed to help you to create that shared library.
To do’s:
1. Create the Java class that represents the native code
2. Create header file for the native code.
3. Implement the native code by writing your C code
4. Compile everything and build you Shared Library
5. Use your native code inside Android activity
1. Create Native Library
Create a Native Library in src directorary in your Eclipse project.
/src/com.mobisoftinfotech/NativeLib.java
2. Create C Header File
In your project bin directory (in my case, /<workspace>/NDKDemo/bin), run javah tool to create the JNI header file.
NDKDemo/bin$ javah -jni com.mobisoftinfotech.NativeLib
Next, create a jni directory in your project directory ( in my case , <EclipseWorkspace>/NDKDemo/jni).
Next, copy the JNI header from <EclipseWorkspace>/NDKDemo/bin to <EclipseWorkspace>/NDKDemo/jni

3. Write the C Code
In your <EclipseWorkspace>/NDKDemo/jni/ folder, create ndkMathdemo.c file. This is where we’ll implement the native code. To start, copy the function signatures from the header file, and provide the implementation for those functions. In this example, the header file looks like this:
<EclipseWorkspace>/NDKDemo/jni/com_mobisoftinfotech_NativeLib.h
4. Build The Library
To build the library, first we need to create a makefile for how to compile the C code:
<EclipseWorkspace>/NDKDemo/jni/Android.mk
Next, we need to tell NDK how to build the shared library and put it in the correct place inside the Eclipse project. To do this, create a folder <NDKHOME>/apps/ndkMathsdemo/ and inside this folder create the Application file:
<NDKHOME>/apps/ndkMathsdemo/Application
You can now to to your <NDKHOME> and run make APP=ndkMathsdemo
The output should look like this on terminal:-
android-ndk-r4$ make APP=ndkMathsdemo
Android NDK: Building for application ‘ndkMathsdemo’
Compile thumb : ndkMathsdemo <= sources/ndkMathdemo/ndkMathdemo.c
SharedLibrary : libndkMathdemo.so
Install : libndkMathsdemo.so => apps/ndk_demo/project/libs/armeabi
You can now refresh your Eclipse project and you should /lib/ directory containing your libndkMathsdemo.sofile.
5. Calling Native Code from Java
So now that we have the native C library implemented, compiled, and placed in the right place, let’s see how we can call it from our Activity. It’s actually rather simple – you just have to instantiate the instance of your NativeLib class and from there on, it’s just a regular Java object.

Source Code: NDKDemo.tar