apply plugin: 'com.android.application'


def getNdkCommandLine(ndkRoot, target) {
    def gstRoot

    if (project.hasProperty('gstAndroidRoot'))
        gstRoot = project.gstAndroidRoot
    else
        gstRoot = System.properties['user.home'] + '/cerbero/dist/android_arm'

    if (ndkRoot == null)
        throw new GradleException('NDK not configured')

    return ["$ndkRoot/ndk-build",
            'NDK_PROJECT_PATH=build',
            'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
            'NDK_APPLICATION_MK=src/main/jni/Application.mk',
            'GSTREAMER_JAVA_SRC_DIR=src/main/java',
            "GSTREAMER_ROOT_ANDROID=$gstRoot",
            target]
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    sourceSets {
        main {
            // Avoid using the built in JNI generation plugin
            jni.srcDirs = []
            jniLibs.srcDirs = ['build/libs']
        }
    }

    defaultConfig {
        applicationId "org.freedesktop.gstreamer.play"
        minSdkVersion 15
        targetSdkVersion 15
        versionCode 1
        versionName "1.0"
    }

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

    // Before compiling our app, prepare NDK code
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

    // Need to call clean on NDK ourselves too
    clean.dependsOn 'ndkClean'

    // Build native code using mk files like on Eclipse
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine getNdkCommandLine(android.ndkDirectory, 'all')
    }

    task ndkClean(type: Exec, description: 'Clean JNI code built via NDK') {
        commandLine getNdkCommandLine(android.ndkDirectory, 'clean')
    }
}

afterEvaluate {
    compileDebugJavaWithJavac.dependsOn 'externalNativeBuildDebug'
    compileReleaseJavaWithJavac.dependsOn 'externalNativeBuildRelease'
}

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