NDK HelloWorld

- 사이트 : https://codelabs.developers.google.com/codelabs/android-studio-jni/index.html?#0


1) gradle wrapper 사용

Preferences (Settings) -> Build, Execution, Deployment -> Build Tools -> Gradle -> Use Default Gradle wrapper (recommended) 


2) NDK 다운로드

Tools -> Android -> SDK Manager -> SDK Tools -> NDK (Android NDK) 선택 -> Apply


3) gradle 설정

3-1) build.gradle (Project:)

- classpath 'com.android.tools.build:gradle-experimental:0.9.3' 로 교체

   (http://jcenter.bintray.com/com/android/tools/build/gradle-experimental 에서 최신버전 확인)

1
2
3
4
5
6
7
8
9
10
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
//        classpath 'com.android.tools.build:gradle:2.3.2'
 
        classpath 'com.android.tools.build:gradle-experimental:0.9.3'
    }
}
cs


3-2) gradle-wrapper.properties

- distributionUrl 최신버전으로 설정

1
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
cs


3-3) build.gradle (Module:)

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
//apply plugin: 'com.android.application'
apply plugin: 'com.android.model.application'
 
model {
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.3"
 
        defaultConfig {
            applicationId "com.ghj.ndkhelloworld"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
 
        buildTypes {
            release {
                minifyEnabled false
//                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
    }
}
...
cs


4) 소스코딩

4-1) build.gradle 에 ndk moduleName 추가 -> Syn Now 클릭

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
//apply plugin: 'com.android.application'
apply plugin: 'com.android.model.application'
 
model {
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.3"
 
        defaultConfig {
            applicationId "com.ghj.ndkhelloworld"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
 
        buildTypes {
            release {
                minifyEnabled false
//                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
 
        ndk {
            moduleName "hello-world"
        }
    }
}
cs


4-2) MainActivity.java
- System.loadLibrary의 라이브러리 이름과 build.gradle의 moduleName 이 같아야 한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.ghj.ndkhelloworld;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    static {
        System.loadLibrary("hello-world");
    }
 
    public native String getHelloWorld();
}
 
cs


4-3) jni 파일 생성

- getHelloWorld를 길게 누른후 전구를 눌러 팝업메뉴에서 jni 파일생성 클릭


4-4) hello-world.c

1
2
3
4
5
6
#include <jni.h>
 
JNIEXPORT jstring JNICALL
Java_com_ghj_ndkhelloworld_MainActivity_getHelloWorld(JNIEnv *env, jobject instance) {
    return (*env)->NewStringUTF(env, "Hello World! From NDK");
}
cs


4-5) activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ghj.ndkhelloworld.MainActivity">
 
    <TextView
        android:id="@+id/txtNDK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
</LinearLayout>
 
cs


4-6) MainActivity.java

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
package com.ghj.ndkhelloworld;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView txtNDK;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txtNDK = (TextView)findViewById(R.id.txtNDK);
 
        //Hello World 출력
        txtNDK.setText(getHelloWorld());
    }
 
    static {
        System.loadLibrary("hello-world");
    }
 
    public native String getHelloWorld();
}
 
cs



결과


+ Recent posts