2010년 4월 7일 수요일

Android NDK 를 이용한 lib 만들기

  1. NDK 디렉토리의 구성
/android-ndk-r3/apps/dc-config/Application.mk
/android-ndk-r3/apps/dc-config/project/
/android-ndk-r3/apps/dc-config/project/jni/
/android-ndk-r3/apps/dc-config/project/jni/Android.mk
/android-ndk-r3/apps/dc-config/project/jni/dc-config.c
/android-ndk-r3/apps/dc-config/project/libs/
/android-ndk-r3/apps/dc-config/project/libs/armeabi/
/android-ndk-r3/apps/dc-config/project/libs/armeabi/libdc-config.so
    • /android-ndk-r3/apps/dc-config/Application.mk
      APP_PROJECT_PATH := $(call my-dir)/project
      APP_MODULES      := dc-config

    • /android-ndk-r3/apps/dc-config/project/jni/Android.mk
      LOCAL_PATH := $(call my-dir)

      include $(CLEAR_VARS)

      LOCAL_MODULE    := dc-config
      LOCAL_SRC_FILES := dc-config.c
      LOCAL_LDLIBS := -llog

      include $(BUILD_SHARED_LIBRARY)

    • /android-ndk-r3/apps/dc-config/project/jni/dc-config.c
      #include <string.h>
      #include <jni.h>
      #include <stdlib.h>
      #include <unistd.h>

      #include <android/log.h>
      #define LOG_TAG "DC-CONFIG"
      #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);

      /*
       * LIB를 사용하는 JAVA CLASS 에  정의된 package 명과 동일한 구조로 함수명을 작성한다(?)
       * Java_[PACKAGE]_[CLASS NAME]_[FUNCTION NAME](JNIEnv* env, jobject thiz)
       * /Eclair/packages/apps/Phone/src/com/android/phone/CdmaDataCallCheckBoxPreference.java
       * package com.android.phone;
       * public class CdmaDataCallCheckBoxPreference extends CheckBoxPreference {
       * public native String  stringFromJNI();
       */
      jstring
      Java_com_android_phone_CdmaDataCallCheckBoxPreference_stringFromJNI( JNIEnv* env,
                                                        jobject thiz )
      {
          int rtn = 0;

          LOGD("PPPD_RUNNER START");
          rtn = system("/system/bin/pppd_runner");
          LOGD("PPPD_RUNNER START [%d]", rtn);

          return (*env)->NewStringUTF(env, "TEST Hello from JNI !");
      }

    • /android-ndk-r3/apps/dc-config/project/libs/armeabi/libdc-config.so
      컴파일 후 생성되는 lib 파일

  1. 컴파일


    # cd /android-ndk-r3/
    # make APP=dc-config
  2. 사용



    /Eclair/packages/apps/Phone/src/com/android/phone/CdmaDataCallCheckBoxPreference.java

    package com.android.phone;

    ( ... )

    public class CdmaDataCallCheckBoxPreference extends CheckBoxPreference {
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            TextView  tv = new TextView(   this );
            tv.setText( stringFromJNI() );
            setContentView(tv);
        }
        public native String  stringFromJNI();
        static {
            System.loadLibrary("dc-config");
        }
    }
  3. 파일시스템에 라이브러리 추가


    /system/lib/libdc-config.so

댓글 없음:

댓글 쓰기