apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.dropbox.textsort"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            java.srcDirs = ['src', '../../handwritten-src/java', '../../generated-src/java']
            jni.srcDirs = []
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.code.findbugs:jsr305:3.0.2'
}

task ndkBuild(type: Exec) {
    def ndkDir = project.android.ndkDirectory
    def ndkBuildExecutable

    if (ndkDir) {
        ndkBuildExecutable = new File(ndkDir, 'ndk-build')
        def MainDirectory = System.getProperty("user.dir") + '/app/'

        environment("NDK_PROJECT_PATH", MainDirectory)
        environment("GYP_CONFIGURATION", "Release")

        commandLine ndkBuildExecutable
    }

    doFirst {
        if (!ndkDir) {
            def gradle_project_root = project.rootProject.rootDir
            throw new GradleException("NDK is not configured. Make sure there is a local.properties " +
                    "file with an ndk.dir entry in the directory ${gradle_project_root}.")
        }

        if (!ndkBuildExecutable.file) {
            throw new GradleException("Could not find ndk-build. The configured NDK directory ${ndkDir} may not be correct.")
        }
    }
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}
