2011년 1월 4일 화요일

Configuring Android NDK development environment in Windows

=> Install Cygwin

LINK : http://cygwin.com/setup.exe

Install Devel group.

=> Install JDK

LINK : http://www.oracle.com/technetwork/java/javase/downloads/index.html
FILE NAME : jdk-6u23-windows-i586.exe

=> Install Eclipse

eclipse IDE LINK :
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.1-201009090800/eclipse-SDK-3.6.1-win32.zip

eclipse java plug-in LINK :
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-java-helios-SR1-win32.zip

eclipse c++ plug-in LINK:
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-cpp-helios-SR1-win32.zip

Download those three and unzip and copy java/c++ plug-in to eclipse IDE folder,
overwrite existing folders.

=> Install Android SDK

LINK : http://dl.google.com/android/installer_r08-windows.exe

=> Install Android NDK

LINK : http://dl.google.com/android/ndk/android-ndk-r5-windows.zip

Unzip and Rename&Copy to C:/android-ndk for convenience.

=> Install ADT-8.0.1

LINK : http://dl.google.com/android/ADT-8.0.1.zip

Unzip and copy to eclipse, overwrite existing folders.



After installing above programs, run eclipse and configure Android SDK to eclipse by
Setting eclipse menu Window->Preferences->Android->SDK Location a Folder you
installed Android SDK , this typically is "C:\Program Files\Android\android-sdk-windows".

Then create android project named TestJNI.

Create jni folder in your project root folder

In jni folder create file Android.mk and copy&paste and save below codes

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c test.cpp

include $(BUILD_SHARED_LIBRARY)
if your project uses c++ and STL then create file Application.mk and copy&paste and save below codes

APP_STL := stlport_shared

create file hello-jni.c with codes below

#include <string.h>
#include <jni.h>


const char *my_cpp_func();
jstring Java_a_b_TestJNIActivity_stringFromJNI( JNIEnv* env,jobject thiz )
{
    return (*env)->NewStringUTF(env, my_cpp_func());
}


create file test.cpp with codes below

#include <string>
class MyC
{
public:
 MyC()
 {}

 ~MyC()
 {}

 virtual const char * f()
 {
  return "MyC";
 }
};

class MyB:public MyC
{
 static std::string s;
public:
 MyB()
 {
  s="MyB ";
 }
 virtual const char * f()
 {
  s=s+"it works!!";
  return s.c_str();
 }
};

std::string MyB::s;
extern "C" const char *my_cpp_func()
{
 MyC *c=new MyB();
 const char *p=c->f();
 delete c;
 return p;
}


In the Property dialog of a created android project, click Builders
menu and click New... button to add new c++ compiling builder.

In Main tab set as below
Location = C:\cygwin\bin\bash.exe
Working Directory = C:\cygwin\bin
Arguments = --login -c "cd /cygdrive/c/users/workspace/TestJNI && /cygdrive/c/android-ndk/ndk-build"

modify "c/users/workspace" to your workspace folder.

In Refresh tab
check Refresh resources upon completion.
check Specific resources radio and Click Specify Resources... button and select "libs" folder in your project.
and Check Recursively include sub-folders.

In Build Options tab
check Launch In Background
check During Auto Builds
check Specify working set of relevant resources and click Specify Resources... Button and "jni" folder in
your project. jni folder is the source folder where your c/c++ sources will be located , if it's not exist then
create folder first.

Create TestJNIActivity.java source in the project with Eclipse IDE and paste this code

package a.b;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TestJNIActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();

    public native String  unimplementedStringFromJNI();
    static {
     System.loadLibrary("stlport_shared");
        System.loadLibrary("hello-jni");
    }
}


run it in AVD emulator or Android Device. if text is not visible, and there's no libstlport_shared.so
file in libs folder then copy that file from \TestJNI\obj\local\armeabi folder to libs folder.

댓글 없음:

댓글 쓰기