apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

Properties properties = new Properties()
if (project.rootProject.file('local.properties').canRead()) {
    properties.load(project.rootProject.file("local.properties").newDataInputStream())
}

// Reads the `.env` file in the root of the project and creates the file `app/android/kotlin/ShipFast/app/src/main/cpp/.jni.env.h`
//  with the environment variables that will be injected inside native C code when it compiles the
//  mobile app. This variables will be made available to the Kotlin code via the JNI interface.
preBuild.doFirst {
    println("Creating JNI ENV")
    String path = "src/main/cpp"
    String workingDir = file(path).absolutePath
    def proc = "$workingDir/gradle-build-jni-env-h.bash".execute()
    proc.waitForProcessOutput(System.out, System.err)
}

android {
    compileSdkVersion 29
    buildToolsVersion '29.0.3'
    ndkVersion "21.0.6113669"
    defaultConfig {
        applicationId "com.criticalblue.shipfast"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // Required by the Auth0 Android Manifest
        manifestPlaceholders = [
          auth0Domain: "$System.env.AUTH0_DOMAIN",
          auth0Scheme: "$System.env.AUTH0_SCHEME",
          ANDROID_GEO_API_KEY: "$System.env.ANDROID_GEO_API_KEY",
        ]

        resValue "string", "app_name", "ShipFast"
        resValue "string", "app_launcher_icon_name", "ShipFast"
        resValue "string", "APPROOV_INITIAL_CONFIG", ""
        resValue "string", "ANDROID_GEO_API_KEY", "$System.env.ANDROID_GEO_API_KEY"
        resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#aaaaaa"
        resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#666666"
        buildConfigField "String", "DEMO_STAGE", "\"API_KEY_PROTECTION\""
    }
    signingConfigs {
        release {
            storeFile file("${properties.getProperty('android.keystore.path')}")
            keyAlias "${properties.getProperty('android.private.key.alias')}"
            storePassword "${properties.getProperty('android.keystore.password')}"
            keyPassword "${properties.getProperty('android.private.key.password')}"
        }
    }
    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        debug {
            debuggable true
            minifyEnabled false
            shrinkResources false
        }
    }
    flavorDimensions "demo_stages"
    productFlavors {
        api_key {
            dimension "demo_stages"
            applicationIdSuffix ".api_key"
            versionNameSuffix "-api_key"
            buildConfigField "String", "DEMO_STAGE", "\"API_KEY_PROTECTION\""
            resValue "string", "app_launcher_icon_name", "SF-API-Key"
            resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#3f51b5"
            resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#303f9f"
        }

        static_hmac {
            dimension "demo_stages"
            applicationIdSuffix ".static_hmac"
            versionNameSuffix "-static_hmac"
            buildConfigField "String", "DEMO_STAGE", "\"HMAC_STATIC_SECRET_PROTECTION\""
            resValue "string", "app_launcher_icon_name", "SF-SHMAC"
            resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#ec971f"
            resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#d58512"
        }

        dynamic_hmac {
            dimension "demo_stages"
            applicationIdSuffix ".dynamic_hmac"
            versionNameSuffix "-dynamic_hmac"
            buildConfigField "String", "DEMO_STAGE", "\"HMAC_DYNAMIC_SECRET_PROTECTION\""
            resValue "string", "app_launcher_icon_name", "SF-DHMAC"
            resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#d9534f"
            resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#d43f3a"
        }

        certificate_pinning {
            dimension "demo_stages"
            applicationIdSuffix ".certificate_pinning"
            versionNameSuffix "-certificate_pinning"
            buildConfigField "String", "DEMO_STAGE", "\"CERTIFICATE_PINNING_PROTECTION\""
            resValue "string", "app_launcher_icon_name", "SF-Pinning"
            resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#868646"
            resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#646435"
        }

        approov {
            dimension "demo_stages"
            applicationIdSuffix ".approov"
            versionNameSuffix "-approov"
            buildConfigField "String", "DEMO_STAGE", "\"APPROOV_APP_AUTH_PROTECTION\""
            resValue "string", "app_launcher_icon_name", "SF-Approov"
            resValue "color", "DEMO_STAGE_PRIMARY_COLOR", "#449d44"
            resValue "color", "DEMO_STAGE_PRIMARY_DARK_COLOR", "#398439"
            resValue "string", "APPROOV_INITIAL_CONFIG", "$System.env.APPROOV_INITIAL_CONFIG"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('./src/main/cpp/CMakeLists.txt')
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // Include Google Play Services for Google Maps
    implementation 'com.google.android.gms:play-services:12.0.1'
    // Include Auth0 for user authentication
    implementation 'com.auth0.android:auth0:1.14.1'
    // Include OkHttp3 for REST API requests
    implementation("com.squareup.okhttp3:okhttp:4.4.0")
    implementation 'com.google.android.material:material:1.1.0'

    // *** APPROOV IMPLEMENTATION ***
    // Requires the Approov SDK
    // @link https://approov.io/docs/latest/approov-usage-documentation/#importing-the-approov-sdk-into-android-studio
     implementation project(':approov')
}
