// android/build.gradle

// based on:
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
//   previous location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
//   previous location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle

// These defaults should reflect the SDK versions used by
// the minimum React Native version supported.
def DEFAULT_COMPILE_SDK_VERSION = 31
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 28

def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

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


import groovy.json.JsonSlurper


buildscript {
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    // ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2'
        }
    }
}

android {
    compileSdk safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    namespace 'com.ca.axa.react'
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"
    }
    lintOptions {
        abortOnError false
    }
    publishing {
        singleVariant('release') {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant('debug') {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

repositories {
    google()
    mavenCentral()
    maven {
        url 'https://packages.broadcom.com/apm-agents'
    }
}

dependencies {
    implementation 'com.facebook.react:react-android:0.71.0'
    implementation 'com.ca.dxapm:sdk-core:25.4.1@aar'
}


tasks.register('androidSourcesJar', Jar) {
    from android.sourceSets.main.java.srcDirs
    include '**/*.java'
}


afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                def packageJson = new JsonSlurper().parseText(file('../package.json').text)
                println "Artifact:" + packageJson.name
                groupId "com.ca.axa.react"
                artifactId packageJson.name
                version packageJson.version
                artifact(androidSourcesJar)
            }
        }
        repositories {
            maven {
                url = file("${System.env.HOME}/.m2/repository/")
            }
        }
    }
}
