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


repositories {
    maven {
        url '../../../build/m2repository/'
    }
    google()
    mavenCentral()
}

if (project.hasProperty('sdks')) {
    project.android {
        sdks.split(',').each { sdk ->
            def sdkParts = sdk.split(':')
            if (sdkParts.size() != 3) {
                throw new GradleException(
                    "Invalid SDK format: '${sdk}'. " +
                    "Expected format: 'groupId:artifactId:version' (sdks: '${sdks}')"
                )
            }
            def (groupId, artifactId, version) = sdkParts
            productFlavors.create(artifactId) {
                dimension 'firebase'
            }
            dependencies.add("${artifactId}Implementation", sdk)
        }

        // Ignore debug builds as measurements are not reproducible due to disabled ZipAlign.
        // Ignore per-architecture builds for all products except for firebase-crashlytics-ndk.
        variantFilter { variant ->
            def buildType = variant.buildType.name
            def flavors = variant.flavors*.name
            if (buildType == 'debug'
                    || ('firebase-crashlytics-ndk' in flavors && 'universal' in flavors)
                    || !('firebase-crashlytics-ndk' in flavors || 'universal' in flavors)) {
                setIgnore(true)
            }
        }

        applicationVariants.all { variant ->
            def product = variant.productFlavors[0].name
            def abi = variant.productFlavors[1].name
            def buildType = variant.buildType.name
            variant.outputs.all {
                outputFileName = "${product}::${buildType}::${abi}.apk"
            }
        }
    }
}
