Android NDK - Sizuha/devdog GitHub Wiki

Android Studio์—์„œ NDK ์‚ฌ์šฉ

ํ•„์š” ํ™˜๊ฒฝ

  • Android Studio 1.5 ๊ธฐ์ค€
  • Android NDK r10e ๊ธฐ์ค€
  • Gradle 2.5 ๊ธฐ์ค€
  • SDK Build Tools 19.0.0
  • Java 1.7

์ฐธ๊ณ 

Android Studio ์„ค์ •

gradle-wapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip

build.gradle (Project)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build.gradle (App)

์ด ๋ถ€๋ถ„์ด ๊ธฐ์กด๊ณผ ๋งŽ์€ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•œ๋‹ค.

์ž์„ธํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ๋‹ค์Œ ๋งํฌ๋ฅผ ์ฐธ์กฐ.

http://tools.android.com/tech-docs/new-build-system/gradle-experimental

apply plugin: 'com.android.model.application' 
// com.android.application -> com.android.model.application

model { // android ๋ธ”๋ก ์ „์ฒด๋ฅผ model๋กœ ๊ฐ์‹ผ๋‹ค

    android {
        compileSdkVersion = 23
        buildToolsVersion = "22.0.1"

        defaultConfig.with {
            applicationId = "xx.xx.xxxxx"
            minSdkVersion.apiLevel = 19
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }

        allprojects {
            sourceCompatibility = 1.7 // Java 1.7 ์‚ฌ์šฉ
            targetCompatibility = 1.7  // ์ด ๋ถ€๋ถ„์ด ์—†์œผ๋ฉด Android Studio์—์„œ Run ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ.
        }
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add( file('proguard-rules.txt') )
        }
    }

    android.ndk {
        moduleName = "audiolib" // NDK Module Name
        /*
         * Other ndk flags configurable here are
         * cppFlags += "-fno-rtti"
         * cppFlags += "-fno-exceptions"
         * ldLibs    = ["android", "log"]
         * stl       = "system"
         */

        ldLibs    = [ "log", "android", "GLESv3"]
    }

    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters += "armeabi"
        }
        create("arm7") {
            ndk.abiFilters += "armeabi-v7a"
        }
        create("arm8") {
            ndk.abiFilters += "arm64-v8a"
        }
        create("x86") {
            ndk.abiFilters += "x86"
        }
        create("x86-64") {
            ndk.abiFilters += "x86_64"
        }
        create("mips") {
            ndk.abiFilters += "mips"
        }
        create("mips-64") {
            ndk.abiFilters += "mips64"
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}

JNI

JNI ๋””๋ ‰ํ† ๋ฆฌ ์ถ”๊ฐ€. java > {app} > main > jni

class: Sample
public class ExAudioLib {

    static {
        System.loadLibrary("audiolib"); // NDK Module Name
    }

    private static ExAudioLib __instance;

    public static void init() {
        __instance = new ExAudioLib();
    }

    public static void release() {
        __instance = null;
    }

    public static ExAudioLib getInstance() {
        return __instance;
    }

    public native String testNDK();

}
header: Sample

์ง์ ‘ ์ž‘์„ฑํ•˜๊ธฐ ๋ณด๋‹จ javah ๋ช…๋ น์„ ์ด์šฉํ•ด์„œ ์ž๋™์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class xx_xxx_xxxx_app_lib_ExAudioLib */

#ifndef _Included_xx_xxx_xxxx_app_lib_ExAudioLib
#define _Included_xx_xxx_xxxx_app_lib_ExAudioLib
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     xx_xxx_xxxx_app_lib_ExAudioLib
 * Method:    testNDK
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_xx_xxx_xxxx_app_lib_ExAudioLib_testNDK(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
cpp: Sample
#include "xx_xxx_xxxx_app_lib_ExAudioLib.h"
#include <GLES3/gl3.h>

JNIEXPORT jstring JNICALL Java_xx_xxx_xxxx_app_lib_ExAudioLib_testNDK(JNIEnv *env, jobject) {
    jstring str =  (*env).NewStringUTF("NDK Test OK !!");
    return str;
}

Build

์ด์ œ Android.mk ๋“ฑ์˜ mk ํŒŒ์ผ์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค. App Gradle ํŒŒ์ผ์˜ android.ndk ๋ธ”๋ก์—์„œ ํ•„์š”ํ•œ ์˜ต์…˜๋“ค์„ ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋นŒ๋“œ๋Š” ์ผ๋ฐ˜ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋นŒ๋“œ๋ž‘ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

ํŽธ๋ฆฌ ๊ธฐ๋Šฅ

javah ๊ธฐ๋Šฅ ์ถ”๊ฐ€

Android Studio ์„ค์ •์—์„œ, External Tools์— ๋‹ค์Œ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€.

  • Name: javah
  • Group: NDK
  • Description: javah // ์ด ๋ถ€๋ถ„์€ ์ ๋‹นํžˆ...
  • Options ๋ฐ Show in: ๋ชจ๋‘ ์ฒดํฌ
  • Tool settings
    • Program; /usr/bin/javah
    • Parameters: -classpath $Classpath$ -v -jni $FileClass$
    • Working directory: $SourcepathEntry$/../jni

์‚ฌ์šฉ๋ฒ•: Java ํด๋ž˜์Šค ํŒŒ์ผ์„ ์„ ํƒํ•˜๊ณ , ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฉ”๋‰ด > NDK > javah ์„ ํƒ.

JNI types

Java Type Native Type Description
boolean jboolean 8 bits, unsigned
byte jbyte 8 bits, signed
char jchar 16 bits, unsigned
double jdouble 64 bits
float jfloat 32 bits
int jint 32 bits, signed, ๊ทธ๋ƒฅ int๋กœ ์จ๋„ ๋˜๊ธด ํ•จ.
long jlong 64 bits, signed
short jshort 16 bits, signed
void void
โš ๏ธ **GitHub.com Fallback** โš ๏ธ