티스토리 뷰

Programming/Java

Ubuntu 12.04 - Android NDK 설치

파란크리스마스 2012. 5. 20. 22:51
728x90

출처 : http://inmist.tistory.com/entry/%EC%8A%A4%ED%81%AC%EB%9E%A9Android-NDK-%EC%82%AC%EC%9A%A9%EB%B2%95
http://www.kkaneko.com/rinkou/js/andk.html
http://darkryu.egloos.com/m/3299369

NDK 다운로드

http://developer.android.com/sdk/ndk/index.html

NDK 설치

cd 다운로드
tar xvjof android-ndk-r8-linux-x86.tar.bz2
mv android-ndk-r8 ~/dev

NDK 환경 설정

echo 'export ANDROID_NDK_ROOT=/home/user1/dev/android-ndk-r8' | tee -a ~/.bashrc
echo 'export ANDROID_SDK_ROOT=/home/user1/dev/android-sdks' | tee -a ~/.bashrc
echo 'export PATH=$PATH:$ANDROID_NDK_ROOT:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools' | tee -a ~/.bashrc

ToolChain 만들기

sudo ~/dev/android-ndk-r8/build/tools/make-standalone-toolchain.sh --ndk-dir=/home/user1/dev/android-ndk-r8 --platform=android-8 --install-dir=/opt/android-8-toolchain
sudo chown user1:user1 -hR /opt/android-8-toolchain

echo 'export TOOLCHAIN=/opt/android-8-toolchain' | tee -a ~/.bashrc
echo 'export PATH=$TOOLCHAIN/bin:$PATH' | tee -a ~/.bashrc
echo 'export CC=arm-linux-androideabi-gcc' | tee -a ~/.bashrc

cygwin (참조용)
./make-standalone-toolchain.sh --ndk-dir=/opt/android-ndk --platform=android-8 --install-dir=/opt/android-8-toolchain

NDK 테스트 빌드

cd ~/dev/android-ndk-r8/samples/hello-jni/jni
ndk-build

컴파일 확인

NDK Sample 만들기 

기본 Sample을 사용하는 것이라서 HelloJni 입력하고, [Next] 버튼 선택

Target SDK를 Android 2.1로 선택하고, [Next] 버튼 선택

Package Name : com.example.hellojni
Create Activity : HelloJni

HelloJni.java파일를 NDK Sample 폴더에서 복사하거나, 아래와 같이 입력 한다

package com.example.hellojni;

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


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /** 
         * Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        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();

    /**
     * This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();

    /**
     * this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.HelloJni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
}

jni 폴더를 만들고, Android.mk, hello-jni.c NDK Sample 폴더에서 복사하거나, 아래와 같이 입력 한다.

Android.mk 파일

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

hello-jni.c 파일

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include 
#include 

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

NDK 빌드

cd ~/dev/workspace/HelloJni/jni
ndk-build

HelloJni 프로젝트를 refresh 해보면 libs/armeabi/libhello-jni.so 파일이 생성 된것을 확인 할 수 있다. 

실행

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함