/* Copyright (c) V-Nova International Limited 2025-2026. All rights reserved.
 * This software is licensed under the BSD-3-Clause-Clear License by V-Nova Limited.
 * No patent licenses are granted under this license. For enquiries about patent licenses,
 * please contact legal@v-nova.com.
 * The LCEVCdec software is a stand-alone project and is NOT A CONTRIBUTION to any other project.
 * If the software is incorporated into another project, THE TERMS OF THE BSD-3-CLAUSE-CLEAR LICENSE
 * AND THE ADDITIONAL LICENSING INFORMATION CONTAINED IN THIS FILE MUST BE MAINTAINED, AND THE
 * SOFTWARE DOES NOT AND MUST NOT ADOPT THE LICENSE OF THE INCORPORATING PROJECT. However, the
 * software may be incorporated into a project under a compatible license provided the requirements
 * of the BSD-3-Clause-Clear license are respected, and V-Nova Limited remains
 * licensor of the software ONLY UNDER the BSD-3-Clause-Clear license (not the compatible license).
 * ANY ONWARD DISTRIBUTION, WHETHER STAND-ALONE OR AS PART OF ANY OTHER PROJECT, REMAINS SUBJECT TO
 * THE EXCLUSION OF PATENT LICENSES PROVISION OF THE BSD-3-CLAUSE-CLEAR LICENSE. */

apply plugin: 'com.android.library'

def ANDROID_COMPILE_SDK = Integer.parseInt(project.property('ANDROID_COMPILE_SDK'))
def ANDROID_TARGET_SDK = Integer.parseInt(project.property('ANDROID_TARGET_SDK'))
def ANDROID_NDK_VERSION = project.property('ANDROID_NDK_VERSION')

def BUILD_TYPE = { ->
    if (gradle.getStartParameter().taskNames.find({ it.contains('Debug') }) != null) {
        return 'Debug'
    }
    return 'Release'
}()

ext {
    artifactName = "${PROJECT_ARTIFACT_NAME}"
    artifactGroup = "${PROJECT_ARTIFACT_GROUP}"
    releaseName = "${PROJECT_RELEASE_NAME}"
    releaseDescription = "${PROJECT_RELEASE_DESCRIPTION}"
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath libs.android.gradle.plugin
        classpath libs.gms.strict.version.matcher
        classpath libs.kotlin.gradle.plugin
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

android {
    namespace = artifactGroup

    compileSdkVersion = ANDROID_COMPILE_SDK
    ndkVersion = ANDROID_NDK_VERSION

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    packagingOptions {
        merge '**/LICENSE.txt'
    }

    externalNativeBuild {
        cmake {
            path = file(project.projectDir.getAbsolutePath() + '/src/main/jni/CMakeLists.txt')
            buildStagingDirectory project.cxxDir
        }
    }

    defaultConfig {
        versionName = artifactName
        base.archivesName = versionName
        minSdkVersion = ANDROID_MIN_SDK
        targetSdkVersion = ANDROID_TARGET_SDK
        testInstrumentationRunner = 'androidx.test.runner.AndroidJUnitRunner'
        sourceSets {
            main {
                jni.srcDirs = [] // Disable the automatic ndk-build call by Android Studio.
            }
        }

        externalNativeBuild {
            cmake {
                abiFilters = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
                arguments = ['-DANDROID_ARM_NEON=TRUE',
                             '-DANDROID_TOOLCHAIN=clang',
                             "-DANDROID_PLATFORM=android-${ANDROID_MIN_SDK}",
                             "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}"]
            }
        }
    }

    publishing {
        singleVariant('debug') {
            withSourcesJar()
        }
        singleVariant('release') {
            withSourcesJar()
        }
    }
}

apply from: project.file('publish.gradle')
