buildscript {
    ext.safeExtGet = {prop, fallback ->
        rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
    }
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        // Matches recent template from React Native (0.60)
        // https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L16
//        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath("com.android.tools.build:gradle:${safeExtGet('gradlePluginVersion', '3.4.1')}")
    }
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

// Matches values in recent template from React Native (0.59)
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L5-L9
def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
def DEFAULT_MIN_SDK_VERSION = 16
def DEFAULT_TARGET_SDK_VERSION = 28

def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)

android {
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName packageJson.version
        buildConfigField "String", "QM_RN_MODULE_VERSION", "\"${packageJson.version}\""
        consumerProguardFiles "consumer-rules.pro"
    }
    lintOptions {
        abortOnError false
    }
}

repositories {
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        // Matches recent template from React Native (0.59)
        // https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L30
        url "$projectDir/../node_modules/react-native/android"
    }
    mavenCentral()
    google()
}

afterEvaluate { project ->
    publishing {
        repositories {
            maven {
                url = uri("${rootProject.projectDir}/maven-repo")
            }
        }
    }
}

dependencies {
    implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}"
}
