apply plugin: 'com.android.application'

android {
    sourceSets.main {
        jni.srcDirs = [] // This prevents the auto generation of Android.mk
        jniLibs.srcDir 'jni/jniLibsCurl' // This is not necessary unless you have precompiled libraries in your project.
    }

    defaultConfig {
        ndk {
            moduleName "curl-prebuilt"
        }
    }

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

    productFlavors
	{
		arm
		{
			ndk
			{
				abiFilters "armeabi"
			}
		}
		x86
		{
			ndk
			{
				abiFilter "x86"
			}
		}
    }

    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = System.getenv('ANDROID_NDK_HOME')
        if (ndkDir == null) {
            Properties properties = new Properties()
            properties.load(project.rootProject.file('local.properties').newDataInputStream())
            ndkDir = properties.getProperty('ndk.dir')?:null
        }

        commandLine "$ndkDir/ndk-build",
                '-C', file('jni').absolutePath, // Change src/jni the relative path to your jni source
                '-j', Runtime.runtime.availableProcessors(),
                'all',
                'NDK_DEBUG=1'
    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = System.getenv('ANDROID_NDK_HOME')
        if (ndkDir == null) {
            Properties properties = new Properties()
            properties.load(project.rootProject.file('local.properties').newDataInputStream())
            ndkDir = properties.getProperty('ndk.dir')?:null
        }
        commandLine "$ndkDir/ndk-build",
                '-C', file('jni').absolutePath, // Change src/jni the relative path to your jni source
                'clean'
    }

    clean.dependsOn 'cleanNative'

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