Tuesday, 23 April 2013

Step By Step Tutorial to Android NDK in Linux Environment



Before we begin coding and setting up the environments, I will give some basic introduction to terminologies we will use here.


Q1. What is Android NDK?

Ans.  Android native development kit is a set set of tools  that helps in implementing native code from languages like C and C++. You should only use NDK if it is really required in your apps, never use NDK if you just prefer programming in C or C++. Doing so will result in complexity.

Q2. Where to use NDK?

Ans. NDK should be used where intensive CPU uses is required in your application, such as physics simulation, signal processing, etc.

Getting Started:

Before we start we will need to download and install a couple of things:-

Eclipse with Android SDK

The SDK comes with pre bundled eclipse and is located at http://developer.android.com/sdk/index.html. Extarct this bundle in your home/root directory. Click the eclipse icon and start developing, thats it.

Android NDK Setup

The NDK can be downloded  from http://developer.android.com/tools/sdk/ndk/index.html. Extarct this bundle in your home/root directory.
Now goto back to Eclipse environment.
Click on window->prefrences->android->NDK.
Choose the extracted NDK folder.



Getting started with project development:

Step 1:

Create a new android project in using that extracted SDK Bundle, for this example I will call it NdkTrial.

Step 2:

Create a new folder called 'jni'(small letters, no quotes) in the root directory of your project.




Step 3:

The next thing we need to do is to setup Eclipse so we can build the JNI code. Click on Run->External 

Tools-> and choose the 'External Tools Configuration...'

Mark the 'Program' and click on the 'new'-icon.

Give it a Name(Can be any name)

Click on Main Tab.

Location is the location of the external tool you want to run, in our case the ndk-build file.

Click on 'Browse file system...' and locate the ndk-build file located under your NDK folder.



Refresh Tab Settings should be like this:


Build Tab Settings should be like this:


Now Click Run and you will get an error *** Android NDK: Aborting...
If you get this error then your setup to the ndk-build works and you are missing the Android.mk file.

STEP 4:

Create a new file called Android.mk in the jni folder.
In the Android.mk file write :
    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_LDLIBS := -llog
    
    LOCAL_MODULE := xyz
    LOCAL_SRC_FILES := native.c
    
    include $(BUILD_SHARED_LIBRARY)

STEP 5:

Create a native.c file in jni folder.
In the native.c file write this:

#include <jni.h>
#include <string.h>
//Prefix Java, package name , class name with c function.  Change the dots to underscore
jstring Java_com_nexg_ndktrial_NDKActivity_display(JNIEnv* env, jobject thiz) {
            return (*env)->NewStringUTF(env, "Sanidhya09 Says: Hello from C");
}

STEP  6:

Now go back to external tools and click run again.
It will generate .so files in libs directory.
Now you should get successfull build.

STEP  7:

Now modify for Activity Class with this code:

public class NDKActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
              TextView  tv = new TextView(this);
              tv.setText( display() );
              setContentView(tv);
            
             // Toast.makeText(this, ""+display2(),                                 Toast.LENGTH_SHORT).show();
          }

          public native String  display();

          /* this is used to load the “.so” library on application
           * startup. The library has already been unpacked into
           * /data/data/com.nexg.NdkTrial/obj/libxyz.so at
           * installation time by the package manager.
           */
          static {
              System.loadLibrary("xyz");
          }
     
}

STEP 7:

Test the program in device or Emulator.

For more information Kindly visit us: - http://www.exuberantsolutions.com/

Thank You






20 comments:

  1. Thanks for post. It really informative stuff. I really like to read. Hope to learn a lot and have a nice experience here! My best regards guys! Tuff. I really like to read. Hope to learn a lot and have a nice experience here! My best regards guys!

    ReplyDelete
  2. Hi your blog is very nice and get more knowledge about this. Thanks for this blog.

    ReplyDelete
  3. This blog is a great combination of suitable and useful information and well-written sentences that will certainly attract your sense and update you about new happening.

    ReplyDelete
  4. Hi you are having a nice Web Blog with good design and content, really I appreciate to your blog. So I would like to say thanks for it.

    ReplyDelete
  5. I want talk some information for "Mobile & Telecom International Congress 2013".

    mBillionth is recognizing and honoring innovators and innovations happening in the world of Mobile & Telecom across South Asian countries through "The International Mobile & Telecom Conference 2013". The platform acknowledges the best identified practices working in the space of mobile & telecom for their developments and services across South Asia annually.

    18th July, 2013 at Hotel Eros, Nehru Place, New Delhi, India.


    mbillionth

    ReplyDelete
  6. Great Article : About Andriod ndk :

    Keep Posting !!
    You can find Latest Linux updates at : http://blog.onedollardata.com/

    ReplyDelete
  7. Hi, what a great web blog. I usually spend hours on the net reading blogs on various subjects. Android Training In Chandigarh

    ReplyDelete
  8. Great post like this must be highly recommended. It is so nice to read such wonderful blog. Thanks for sharing! Have a pleasant day ahead.
    Android Training In Chandigarh

    ReplyDelete
  9. Nice information i really impressed from your blog. regrading
    Six Months Industrial Training In Chandigarh
    Thanks

    ReplyDelete
  10. This is really great explanation of NDK and also providing the good example with programming code.I really impressed this blog.
    Web Design Company | Web Design Companies

    ReplyDelete
  11. Thanks for post. It really useful stuff. I really like to read. Hope to learn a lot and have a nice experience here!
    Website Development in Bangalore | Website Design Company Bangalore

    ReplyDelete
  12. Nice blog f!!!!!!!! thanks for sharing with us.

    Android Training in Chandigarh

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Web Designing Bangalore You must manually run the SDK manager after downloading / extracting the SDK:

    $ /path/to/android-sdk-linux/tools/android sdk

    ...and pull the platform-tools (without which you have no adb) and whatever else you need, e.g. SDK API levels etc.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. We have an institute in Mohali Chandigarh & They provides Best Industrial Training.
    A2IT Courses...
    Best Ethical Hacking Training in Mohali
    Python Training in Mohali
    Industrial Training Company in Mohali
    Digital Marketing Training in Mohali
    CCNA Training in Mohali

    ReplyDelete
  17. Site Galleria is a Web development company in Bangalore . It provides other services such as Mobile App Development Services for consumer-facing and corporate environments. We also provide Ecommerce web development services, Digital Marketing and SEO Services. We have a team of experienced SEO professionals who will understand your business and create a strategy to fulfil your commercial objectives. Site Galleria is also recognized as best Digital Marketing company in Bangalore.

    ReplyDelete