2011년 1월 5일 수요일

안드로이드 실제 디바이스에서 실행 및 디버깅 하기

1. AndroidManifest.xml 파일을 열고 <application> 태그 안에 android:debuggable="true" 를 추가한다.

2. 안드로이드 폰에서 MENU > Application > Development 메뉴에서 USB Debugging을 체크한다.

3. Android-SDK 폴더에서 <sdk>\google-usb_driver\ 에서 윈도우 USB드라이버를
설치한다.

4. 설정이 성공했는지 확인해보려면 android-sdk의 platform-tools폴더에 있는
adb를 "adb devices" 와 같이 실행해본다. device가 리스트로 뜨면 성공이다.

5. 성공했다면 이클립스를 실행하되 디버그 설정에서 Target탭에서 Deployment Target Selection Mode에서 Automatic을 선택한다. 디버깅을 하면 실제 디바이스에 apk파일을 다운로드한후 디바이스에서 실행/디버깅이 이뤄진다.

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.