import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    defaultConfig {
        applicationId "com.nowsecure.android.vts"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 13
        versionName "v.13"
    }

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

        debug {
            applicationIdSuffix ".debug"
        }
    }

    sourceSets.main {
        jni.srcDirs = [] // This prevents the auto generation of Android.mk
        // This tells gradle to include the objects in libs/ which were generated by running our
        // Android.mk basically by shelling out
        jniLibs.srcDir 'src/main/libs'
    }
}

android {
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.evernote:android-job:1.1.8'
    compile 'com.github.paolorotolo:appintro:4.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'commons-io:commons-io:2.5'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'org.apache.commons:commons-compress:1.13'
    compile 'com.afollestad.material-dialogs:core:0.9.4.2'
}

def ndkDir = project.android.ndkDirectory
if (ndkDir == null) {
    if (project.rootProject.file('local.properties').exists()) {
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        ndkDir = properties.getProperty('ndk.dir')
    }
    else if (System.getenv('ANDROID_NDK_HOME') != null) {
        ndkDir = System.getenv('ANDROID_NDK_HOME')
    }
}
if (ndkDir == null) {
    throw new GradleException("NDK location not found. Define location with ndk.dir in the " +
            "local.properties file or with an ANDROID_NDK_HOME environment variable.")
}
def jniSrcDir = file('src/main/jni').absolutePath // Change src/main/jni the relative path to your jni source

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine "$ndkDir\\ndk-build.cmd", '-C', jniSrcDir,
                '-j', Runtime.runtime.availableProcessors()
    } else {
        commandLine "$ndkDir/ndk-build", '-C', jniSrcDir,
                '-j', Runtime.runtime.availableProcessors()
    }
}

task cleanNative(type: Exec, description: 'Clean JNI object files') {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine "$ndkDir\\ndk-build.cmd", '-C', jniSrcDir,
                'clean'
    } else {
        commandLine "$ndkDir/ndk-build", '-C', jniSrcDir,
                'clean'
    }
}

clean.dependsOn 'cleanNative'

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

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.1'
            }
        }
    }
}
