NDK installation and running the application on eclipse

Tutorials with advanced 'difficulty' and more Lines of Code.

NDK installation and running the application on eclipse

Postby deepthi » Wed Aug 26, 2009 7:52 am

Hi guys ,

I followed the steps for NDK installation in linux and ran an NDK project .

Initially it was a little tough but now that I am aware of the mistakes I made , I want to share them with all of you ..
here is a step by step procedure on how to install NDK and run a program on eclipse :-)


Installing NDK and running an application developed in NDK with Eclipse

Steps for installation
1)Installing NDK is relatively simple compared to SDK. Extract the contents of the NDK folder.

Go to build , and run .host-setup.sh . If you are facing issues , you can try sh build/host-setup.sh


Prior to this do update the path of NDK in /etc/bashrc

( source bashrc)

and in the NDKroot directory

declare -x ANDROID_NDK_ROOT /<NDK-home>


2)After installation , go to eclipse and create a new project helloNDK .
Create an activity as follows .


public class newNDK extends Activity {
TextView txtHello;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//obtain a reference to our textview control
txtHello = (TextView) findViewById(R.id.text);

// To invoke our native method
String hello = sayHello();
//update our textview control
txtHello.setText(hello);
}

// This is our native method. This is needed by the "javah" tool
//for generating our .h include header.
public native String sayHello();

//Our static initializer which will load our shared library
//into our classloader/VM
static {
//your library will typically be called "libnewndk.so"
//on Linux, but by convention the "lib" prefix, and ".so"
//suffix are omitted
System.loadLibrary("newndk");
}
}


3)Save this project .
4)Create the .h file by giving the following command in the project home directory.

javah -o newndk.h -classpath bin com.newNDK.newNDK

5)Go to the NDKroot home and create two folders as follows

$mkdir -p $NDK_HOME/sources/hellondk
$mkdir -p $NDK_HOME/apps/hellondk

1.Go to /sources/hellondk
Copy the created newndk.h file from the project home directory to /sources/hellondk
The generated newndk.h file looks like this

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

#ifndef _Included_com_newNDK_newNDK
#define _Included_com_newNDK_newNDK
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_newNDK_newNDK
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_newNDK_newNDK_sayHello
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif




2.Create a newndk.cpp in as follows

#include <newndk.h>

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


3.Now create a new Android.mk in /sources/hellondk as follows

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#I Like to build my shared libs using C++
LOCAL_DEFAULT_CPP_EXTENSION := cpp

LOCAL_MODULE := newndk
LOCAL_SRC_FILES := newndk.cpp

include $(BUILD_SHARED_LIBRARY)


4.Now go to /apps/newndk and create Application.mk as follows

APP_PROJECT_PATH := ~/workspace.android/newNDK
APP_MODULES := newndk

5. Now make this project in NDK by going to the NDK root and giving make APP=newndk

This will generate the .so library file after compilation
The following message will be displayed

Compile++ thumb: newndk <= sources/newndk/newndk.cpp
SharedLibrary : libnewndk.so
Install : libnewndk.so => ~/workspace.android/newNDK/libs/armeabi

6.Now go to eclipse ddms , push the shared library file libnewndk.so from ~/workspace.android/newNDK/libs/armeabi to data / project package/lib
This can alternatively be done through adb shell prompt in the sdk-home/tools also but the above method is easier.


7. Now run the application in eclipse and it will run like any SDK developed application but now with a loaded shared library created in NDK
deepthi
Freshman
Freshman
 
Posts: 3
Joined: Thu Jul 30, 2009 11:00 am

Top

NDK onWindows

Postby rahulp » Mon Jan 18, 2010 3:07 pm

hi can anyone tell me how to instal NDK on windows..
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Re: NDK onWindows

Postby chadao » Tue Mar 30, 2010 8:45 am

Here's the manual of NDK installation on Windows:

1. Install Cygwin.
2. Install GNU Make 3.81 since the original version is 3.8 in the installed Cygwin.
3. Add the GNU make 3.81's path to PATH.
4. Done.
chadao
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 26, 2009 10:28 am

NDK Installation

Postby rahulp » Tue Mar 30, 2010 9:34 am

Hi,
Thanks for the reply. Can u tell in brief how to set path. I am totally new to this.
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Re: NDK Installation

Postby chadao » Tue Mar 30, 2010 11:03 am

rahulp wrote:Hi,
Thanks for the reply. Can u tell in brief how to set path. I am totally new to this.


Just need to add GNU Make's bin folder path to the end of /etc/profile file as following
################################
export PATH=/cygdriver/c/apps/gnu/bin:$PATH
################################

"/cygdriver/c/apps/gnu/bin" is the installation path of GNU Make 3.81, of course you can install it to another place.
But the path cannot contain space since it is not supported in Cygwin.

Good luck.
chadao
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 26, 2009 10:28 am

Re: NDK installation and running the application on eclipse

Postby helancy » Wed Oct 26, 2011 3:03 am

Machines involving arsenal football shirts today try and produce a number of liverpool football shirts for any followers that will appear to be similar to the original the football shirt the fact that avid gamers use on a lawn, however you may nonetheless look for a part of layouts within the &
helancy
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 26, 2011 2:41 am

Top

Re: NDK installation and running the application on eclipse

Postby nglenister » Fri Nov 18, 2011 4:53 pm

Nice tutorial! I'm stuck on what I hope is my final bug before it works. When running make APP=newndk I get the following terminal output:

Code: Select all
Android NDK: Building for application 'newndk'   
make: *** No rule to make target `/newndk.cpp', needed by `out/apps/newndk/armeabi/objs/newndk/newndk.o'.  Stop.


Any idea what this means? I've copied my entire project to [NDK]/apps/newndk/projects and my Application.mk has APP_PROJECT_PATH := $(call my-dir)/project so I'm a little lost :/

Thanks, N
nglenister
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 18, 2011 4:48 pm

Re: NDK installation and running the application on eclipse

Postby nglenister » Fri Nov 18, 2011 5:09 pm

nglenister wrote:Nice tutorial! I'm stuck on what I hope is my final bug before it works. When running make APP=newndk I get the following terminal output:

Code: Select all
Android NDK: Building for application 'newndk'   
make: *** No rule to make target `/newndk.cpp', needed by `out/apps/newndk/armeabi/objs/newndk/newndk.o'.  Stop.


Any idea what this means? I've copied my entire project to [NDK]/apps/newndk/projects and my Application.mk has APP_PROJECT_PATH := $(call my-dir)/project so I'm a little lost :/

Thanks, N


I figured it out, oddly it was because I had spaces in my Android.mk file. Once removed everything worked nicely :)! So beware of copying and pasting the code from this page as it will often insert unwanted hidden spaces.

Thanks again for the post!
nglenister
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 18, 2011 4:48 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests