> For the complete documentation index, see [llms.txt](https://sungcheol-kim.gitbook.io/android-ndk-debugging/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sungcheol-kim.gitbook.io/android-ndk-debugging/chapter7.md).

# 네이티브 메서드 만들기

이제 코드를 작성해 보자. 우선 네이티브 코드를 저장할 폴더를 생성한다.

![](/files/-M3x1zHgZMIzn-feMnSl)

위 그림을 참고해 main 폴더 아래에 JNI폴더를 생성한다.

![](/files/-M3x1zHiw1YXXgDjBoHv)

`jni` 폴더가 생성된 것을 확인할 수 있다. 이제 `MainActivity.java` 로 이동해서 네이티브 메서드를 작성해 보자.

```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 {

    public native String getMessage();

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

    @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);
    }
}
```

위 코드를 작성하면 getMessage() 네이티브 메서드에 밑줄이 그려진다.

![](/files/-M3x1zHk15UjGmKJVBo7)

여기에 커서를 놓으면 빨간 전구 아이콘이 표시된다.

![](/files/-M3x1zHmlU27tTAhNCPN)

이 전구를 클릭하면 대화상자가 나온다.

![](/files/-M3x1zHoeuiuzmR2aBG-)

네이티브 메서드를 위한 JNI를 만들어 주는 메뉴가 나오는데 이 메뉴를 선택하면 위에서 만든 `jni`폴더에 `mainactivity.c` 파일 생성된다.

![](/files/-M3x1zHqEiCJAII4xgQG)

이 파일을 선택해 보자.

```cpp
#include <jni.h>

JNIEXPORT jstring JNICALL
Java_kr_pe_burt_hellojni_MainActivity_getMessage(JNIEnv *env, jobject instance) {

    // TODO


    return (*env)->NewStringUTF(env, returnValue);
}
```

위처럼 자동으로 JNI코드가 작성되어 있다. 함수를 구현만 해주면 된다. `returnValue`를 `Hello JNI`로 수정한다.

```cpp
#include <jni.h>

JNIEXPORT jstring JNICALL
Java_kr_pe_burt_hellojni_MainActivity_getMessage
(
    JNIEnv *env, 
    jobject instance
) 
{

    return (*env)->NewStringUTF(env, "Hello JNI");

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sungcheol-kim.gitbook.io/android-ndk-debugging/chapter7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
