🤖
Android NDK Debugging
  • Introduction
  • 저자소개
  • Android NDK Debugging
  • 안드로이드 스튜디오 업데이트 하기
  • 프로젝트 생성하기
  • Gradle 설정하기
  • 모듈의 build.gradle 설정하기
  • NDK 빌드 설정하기
  • 네이티브 메서드 만들기
  • 네이티브 메서드 호출하기
  • 네이티브 코드 디버깅하기
  • 더 봐야할 자료들
Powered by GitBook
On this page

Was this helpful?

네이티브 메서드 호출하기

구현한 네이티브 메서드를 액티비티에서 호출해 보자. 네이티브 메서드를 호출하기 위해서는 우선 네이티브 라이브러리를 메모리에 적재해야 한다.

MainActivity.java 파일에 라이브러리 적재 코드를 작성해 준다.

package kr.pe.burt.hellojni;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    ...

    static {
        System.loadLibrary("hello-jni");
    }
}

라이브러리의 이름은 build.gradle파일에서 적어준 ndk 모듈의 이름과 같다.

...
    android.ndk {
        moduleName = "hello-jni"
    }
...

이제 프로젝트를 빌드해본다. 터미널창을 열어서 해도 되고 Build > Rebuild Project 를 실행해도 된다.

$ ./gradlew
:help

Welcome to Gradle 2.5.

To run a build, run gradlew <task> ...

To see a list of available tasks, run gradlew tasks

To see a list of command-line options, run gradlew --help

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL

Total time: 9.599 secs

This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.5/userguide/gradle_daemon.html
$

이제 앱을 실행해 보자. Run > Run app 메뉴를 실행해서 앱이 안드로이드 디바이스에 잘 올라가는지 확인한다.

액티비티에 버튼을 배치하고 버튼을 누르면 네이티브 메서드 getMessage()에서 받은 문자열을 텍스트뷰에 설정하는 코드를 작성해 보자.

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Message"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="buttonClicked"/>

</RelativeLayout>

레이아웃을 파일 작성하고 아래와 같이 구현 코드를 작성한다.

package kr.pe.burt.hellojni;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView = null;

    public native String getMessage();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void buttonClicked(View view) {
        String msg = getMessage();
        textView.setText(msg);
    }

    static {
        System.loadLibrary("hello-jni");
    }
}

실행하면 아래와 같다.

버튼을 누르면 네이티브 메서드를 호출해 가져온 문자열을 텍스트뷰에 설정하는 것을 확인할 수 있다.

Previous네이티브 메서드 만들기Next네이티브 코드 디버깅하기

Last updated 5 years ago

Was this helpful?