안드로이드 - 정적 라이브러리 NDK 빌드

Programming/안드로이드 2012.09.07 01:22 Posted by 파란크리스마스

이전 글에서 만든 정적 라이브러리를 이용해서 안드로이드 ndk 컴파일, 
안드로이드에서 실행해보는 것을 정리보았습니다.

testlibjni.h

#include <jni.h>

#ifndef _Included_com_example_testlib_TestLib
#define _Included_com_example_testlib_TestLib
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_com_example_testlib_TestLib_get_1version(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

testlibjni.cpp

#include "testlib.h"
#include <jni.h>
#include "testlibjni.h"

JNIEXPORT jstring JNICALL Java_com_example_testlib_TestLib_get_1version(JNIEnv *env, jobject jobj)
{
    return env->NewStringUTF(get_version());
}

Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := testlib
LOCAL_SRC_FILES := ../../testlib/src/testlib.cpp
LOCAL_C_INCLUDES := \
   /cygdrive/c/Android/workspaceCDT/testlib/inc \
   $(call include-path-for, graphics corecg)
include $(BUILD_STATIC_LIBRARY)

# JNI
include $(CLEAR_VARS)
LOCAL_MODULE    := testlibjni
LOCAL_SRC_FILES := testlibjni.cpp
LOCAL_STATIC_LIBRARIES := testlib
LOCAL_C_INCLUDES := \
   /cygdrive/c/Android/workspaceCDT/testlib/inc \
   $(call include-path-for, graphics corecg)
include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL := stlport_static
STLPORT_FORCE_REBUILD := true 

ndk-build

 

안드로이드 프로젝트 생성 

TestLib.java

package com.example.testlib;

public class TestLib {
	static {
		System.loadLibrary("testlibjni");
	}
	
	public native static String get_version();
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" 
        android:id="@+id/edtTitle" />

</RelativeLayout>

MainActivity.java

package com.example.testlib_android;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.example.testlib.TestLib;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView edtTitle = (TextView) findViewById(R.id.edtTitle);
        edtTitle.setText(TestLib.get_version());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}

실행결과

 

 

신고


 

티스토리 툴바