Tuesday, April 7, 2015

Android Studio 라이브러리 프로젝트에 NDK 설정하기

인터넷에 있는 Android Studio NDK 설정 내용들은 전부다 메인 프로젝트에 NDK 소스파일 들이 있는 내용이고 라이브러리 프로젝트에 있는 내용은 없다. 그래서 라이브러리 프로젝트에서 NDK 빌드를 하는 설정을 해보았다.

그런데 이클립스의 라이브러리 프로젝트와 다르게, so파일이 생성은 되지만 자동으로 메인 프로젝트에 복사가 되지 않는다. 다음 글에서는 gradle로 빌드된 so파일을 복사를 해보겠다.

apply plugin: 'com.android.library' // 라이브러리 프로젝트

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 21

        ndk {
            moduleName "duongame"
            ldLibs "EGL", "log", "GLESv2", "android"// 추가 라이브러리 설정
        }


    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    // 여기서부터 ndk-build를 위한 추가 설정
    sourceSets.main{
        jni.srcDirs = []
        jniLibs.srcDir 'src/main/libs'
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkLibsToJar
    }

    task ndkLibsToJar(type: Zip, dependsOn: 'ndkBuild', description: 'Create a JAR of the native libs') {
        destinationDir new File(buildDir, 'libs')
        baseName 'ndk-libs'
        extension 'jar'
        from(new File(buildDir, 'libs')) { include '**/*.so' }
        into 'lib/'
    }

    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
// 이 부분이 메인 프로젝트에서 설정하는 것과 다른 부분이다.
// com.android.application과 다르기 library를 사용하며
// sdkHandler를 추가하여야 한다.
// 플러그인의 인터페이스가 다르다.
        def ndkDir = project.plugins.findPlugin('com.android.library').sdkHandler.getNdkFolder()
        println(ndkDir)
        commandLine "$ndkDir/ndk-build",
                'NDK_PROJECT_PATH=build',
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }

}

dependencies {
    compile 'com.android.support:support-v4:21.0.3'
}

No comments:

Post a Comment