/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.library'
apply plugin: 'com.google.protobuf'

/*
 * This library has dependencies upon a few other pieces of code, most notably NanoPB and
 * BoringSSL. In order to compile this project, you must run the following bash scripts:
 * $ bash third_party/nanopb/download.sh
 * $ bash third_party/boringssl/build-android.sh
 */
def nanoPbRoot
if (Os.isFamily(Os.FAMILY_MAC)) {
    nanoPbRoot = file("${rootDir}/third_party/nanopb/macosx").absolutePath
} else if (Os.isFamily(Os.FAMILY_UNIX)) {
    nanoPbRoot = file("${rootDir}/third_party/nanopb/linux").absolutePath
} else if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    nanoPbRoot = file("${rootDir}/third_party/nanopb/windows").absolutePath
}
def boringSslRoot = file("${rootDir}/third_party/boringssl/").absolutePath

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.0"
    ndkVersion "21.3.6528147"

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"

        /*
         * - BORING_SSL_ROOT: location of the BoringSSL project root from
         *     https://boringssl.googlesource.com/boringssl/
         * - NANOPB_ROOT: location of NanoPB downloaded from
         *     https://jpa.kapsi.fi/nanopb/download/
         */
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
                arguments "-DBORING_SSL_ROOT=${boringSslRoot}", "-DNANOPB_ROOT=${nanoPbRoot}"
            }
        }
        ndk {
            // Explicitly set the supported ABI flavours
            abiFilters "armeabi-v7a", "arm64-v8a", "x86"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

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

    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.11.4'
    }
    dependencies {
        protobuf files("${nanoPbRoot}/generator/proto/")
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option "lite"
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    // Java 8 backports
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'

    // AutoValue Dependencies
    api "com.google.auto.value:auto-value-annotations:1.7.3"
    annotationProcessor "com.google.auto.value:auto-value:1.7.3"

    // Protobuf dependency
    implementation 'com.google.protobuf:protobuf-javalite:3.11.4'

    // Guava dependency
    api "com.google.guava:guava:29.0-android"

    // JODA
    api 'joda-time:joda-time:2.9.4'

    // Play Core library, so we don't have to reimplement the basics such as Task
    implementation 'com.google.android.play:core:1.8.0'

    api "androidx.core:core:1.3.1"
    implementation "androidx.annotation:annotation:1.1.0"

    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'org.robolectric:robolectric:4.3.1'
}
