plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    namespace 'software.amazon.aws.crypto.awslcandroidtestrunner'
    compileSdk 30
    buildToolsVersion "33.0.3"
    ndkVersion "28.2.13676358"

    def cmake_args = ''
    def specific_abis = ''
    def main_apk_tag = ''
    def test_apk_tag = ''
    // FIPS only works with arm64 for Android.
    if (project.hasProperty('FIPS')) {
        cmake_args += '-DFIPS=1,-DCMAKE_BUILD_TYPE=Release'
        // Shared build. If not set, the build is static.
        if(project.hasProperty('Shared')) {
            cmake_args += ',-DBUILD_SHARED_LIBS=1'
        }
        specific_abis = 'arm64-v8a'
        main_apk_tag += '_fips'
        test_apk_tag += '_fips'
    }
    // non-FIPS
    else {
        // Shared build. If not set, the build is static.
        if(project.hasProperty('Shared')) {
            cmake_args += '-DBUILD_SHARED_LIBS=1,'
            main_apk_tag += '_shared'
            test_apk_tag += '_shared'
        }
        else {
            main_apk_tag += '_static'
            test_apk_tag += '_static'
        }

        // Release build. If not set, the build is in debug mode.
        if(project.hasProperty('Release')) {
            cmake_args += '-DCMAKE_BUILD_TYPE=Release'
            main_apk_tag += '_rel'
            test_apk_tag += '_rel'
        }
        else {
            main_apk_tag += '_dbg'
            test_apk_tag += '_dbg'
        }
        specific_abis = 'x86_64,armeabi-v7a,arm64-v8a'
    }

    defaultConfig {
        applicationId "software.amazon.aws.crypto.awslcandroidtestrunner"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                if (!cmake_args.isEmpty()) {
                    arguments cmake_args.split(',')
                }
                targets "all_tests"
                abiFilters specific_abis.split(',')
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.18.1"
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = "awslc${main_apk_tag}.apk"
        }
    }

    testVariants.all { testVariant ->
        testVariant.outputs.all { output ->
            outputFileName = "awslc${test_apk_tag}-androidTest.apk"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test:core:1.4.0'
}
