// Copyright 2018 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.

plugins {
    id 'firebase-library'
    id 'LicenseResolverPlugin'
}

firebaseLibrary {
    libraryGroup = "crashlytics"
    testLab {
        enabled = true
        device 'model=panther,version=33' // Pixel7
    }
    releaseNotes {
        name.set("{{crashlytics}} NDK")
        versionName.set("crashlytics-ndk")
    }
    testLab.enabled = true
    publishJavadoc = false
}

android {
    adbOptions {
        timeOutInMs 60 * 1000
    }
    namespace "com.google.firebase.crashlytics.ndk"
    ndkVersion "27.2.12479018"
    compileSdkVersion project.compileSdkVersion
    defaultConfig {
        minSdkVersion project.minSdkVersion
        targetSdkVersion project.targetSdkVersion
        versionName version

        externalNativeBuild {
            ndkBuild {
                arguments '-j4'
                arguments '--output-sync=none'
            }
        }

        consumerProguardFiles 'firebase-crashlytics-ndk-proguard.txt'
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    sourceSets {
        androidTest {
            java {
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude './src/third_party/**'
    }

    externalNativeBuild {
        ndkBuild {
            path "./src/main/jni/Android.mk"
        }
    }

    // There is not any normal way to package native executables in an Android APK.
    // It is normal to package native code as a loadable module but Android's APK
    // installer will ignore files not named like a shared object, so give the
    // handler executable an acceptable name
    libraryVariants.all { variant ->
        def fixTasks = ["x86", "x86_64", "armeabi-v7a", "arm64-v8a"].collect { arch ->
            tasks.register("fixTrampolineFilenames${variant.baseName}${arch}", com.google.firebase.gradle.NdkBinaryFixTask) {
                it.inputFile =
                        file("${buildDir}/intermediates/ndkBuild/${variant.baseName}/obj/local/${arch}/crashlytics-trampoline")
            }
        }

        tasks.withType(com.android.build.gradle.tasks.BundleAar) {
            if (it.variantName != variant.baseName) return
            fixTasks.each { fix ->
                it.dependsOn fix
                it.from(fix.map { it.outputFile }) {
                    into fix.map { it.into }
                }
            }
        }
        tasks.findAll {
            it.name.startsWith("externalNativeBuild") && !it.name.contains("Clean")
        }.each { task ->
            fixTasks.each { fix -> fix.configure { it.dependsOn task } }
        }
    }
}

thirdPartyLicenses {
    add 'Apache-2.0', "${rootDir}/third_party/licenses/apache-2.0.txt"
    add 'Crashpad', "${rootDir}/third_party/licenses/crashpad.txt"
}

dependencies {
    api project(':firebase-crashlytics')
    api libs.firebase.common
    api libs.firebase.components

    implementation libs.playservices.basement

    testImplementation libs.androidx.test.runner
    testImplementation libs.junit
    testImplementation libs.mockito.core
    testImplementation libs.robolectric

    androidTestImplementation libs.androidx.test.core
    androidTestImplementation libs.androidx.test.runner
    androidTestImplementation libs.protobuf.java.lite
    androidTestImplementation libs.dexmaker
    androidTestImplementation libs.mockito.dexmaker
    androidTestImplementation libs.mockito.core
}
