apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

private String getNativeLibraryName() {
  def libraryName = System.getenv('LIBABP_SHARED_LIBRARY_NAME')
  return libraryName != null ? libraryName : 'adblockplus-jni'
}

def getProcessesCountArg() {
  def isClean = getGradle().getStartParameter().getTaskNames().find { it ==~ /.*clean.*/ }
  // The clean shouldn't be parallel, because it causes problems on Mac OS X for NDK below 18
  def procCount = isClean ? 1 : Runtime.runtime.availableProcessors()
  return "-j" + procCount
}

android {
  compileSdkVersion Config.compileSdkVersion
  buildToolsVersion Config.buildToolsVersion

  defaultConfig {
    minSdkVersion Config.minSdkVersion
    targetSdkVersion Config.targetSdkVersion
    buildConfigField "String", "nativeLibraryName", '"' + getNativeLibraryName() + '"'
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    consumerProguardFiles 'proguard-rules-adblock.txt'
    externalNativeBuild {
      ndkBuild {
        arguments getProcessesCountArg()
      }
    }
  }

  buildTypes {
    debug {
      multiDexEnabled true
    }
  }

  flavorDimensions "abi"

  productFlavors {
    abi_arm {
      dimension = "abi"
      ndk {
        abiFilters "armeabi-v7a"
      }

      externalNativeBuild {
        ndkBuild {
          arguments "APP_ABI=armeabi-v7a"
        }
      }
    }

    abi_arm64 {
      dimension = "abi"
      ndk {
        abiFilters "arm64-v8a"
      }

      externalNativeBuild {
        ndkBuild {
          arguments "APP_ABI=arm64-v8a"
        }
      }
    }

    abi_x86 {
      dimension = "abi"
      ndk {
        abiFilters "x86"
      }

      externalNativeBuild {
        ndkBuild {
          arguments "APP_ABI=x86"
        }
      }
    }

    abi_x86_64 {
      dimension = "abi"
      ndk {
        abiFilters "x86_64"
      }

      externalNativeBuild {
        ndkBuild {
          arguments "APP_ABI=x86_64"
        }
      }
    }

    abi_all {
      dimension = "abi"
      ndk {
        abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
      }
    }
  }


  if (System.getenv('SKIP_JNI_COMPILATION') != null) {
    println "[Configuration] Skipping JNI compilation"
    sourceSets.main.jni.srcDirs = []
  } else {
    externalNativeBuild {
      ndkBuild {
        path 'src/main/jni/Android.mk'
      }
    }
  }

  def sharedV8LibFiles = System.getenv('SHARED_V8_LIB_FILENAMES')
  if (sharedV8LibFiles != null) {
    def sharedV8LibFilesSet = []
    def sharedV8LibFilesArray = sharedV8LibFiles.split(' ')
    sharedV8LibFilesArray.each { eachFileName ->
      sharedV8LibFilesSet.add("**/" + eachFileName)
      println "[Configuration] Excluding shared v8 library " + eachFileName + " from AAR"
    }

    packagingOptions {
      excludes = sharedV8LibFilesSet
    }
  } else {
    println "[Configuration] No shared v8 libraries excluded from AAR"
  }

  sourceSets {
    test.java.srcDirs += 'src/test/kotlin'
    androidTest.java.srcDirs += 'src/androidTest/kotlin'
  }
}

bintray {
  pkg {
    desc = "An Android library that provides the core functionality of Adblock Plus."
  }
}

dependencies {
  api Deps.timber
  testImplementation Deps.junit4
  testImplementation Deps.kotlin_stdlib
  androidTestImplementation Deps.kotlin_stdlib
  androidTestImplementation Deps.androidx_test_core
  androidTestImplementation Deps.androidx_test_runner
  androidTestImplementation Deps.androidx_test_ext_junit
  androidTestImplementation Deps.mockito_core
  androidTestImplementation Deps.mockito_android
  androidTestImplementation Deps.wiremock_standalone
  androidTestImplementation Deps.androidx_multidex
}

afterEvaluate {
  tasks.withType(PublishToMavenLocal) {
    it.enabled = it.name.contains("Abi_allRelease")
    def androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    tasks.withType(Javadoc).all {
      classpath += project.files(androidJar)
      failOnError false
    }
  }
}
