apply plugin: 'com.android.application'
apply plugin: 'hu.supercluster.paperwork'
apply from: '../code_quality_tools/jacoco.gradle'
apply plugin: 'com.frogermcs.androiddevmetrics'
apply from: '../code_quality_tools/quality.gradle'
apply plugin: 'com.github.ben-manes.versions'

paperwork {
    set = [
            gitSha   : gitSha(),
            buildDate: buildTime("dd-MM-yyyy HH:00:00 z", "UTC"),
    ]
}

android {
    // Declared in dependencies.gradle in the root of the project.
    compileSdkVersion versions.compileSdk
    buildToolsVersion versions.buildTools

    defaultConfig {
        applicationId 'com.artemzin.qualitymatters'
        minSdkVersion versions.minSdk
        targetSdkVersion versions.targetSdk
        versionCode versions.code // Notice that you may want to use BUILD_NUMBER from CI in real project with own CI.
        versionName versions.name

        // Make generated apks contain version name and code.
        archivesBaseName = "app-${versions.name}-${versions.code}"

        // Notice that we have custom Instrumentation test runner to change application class (to mock dependencies)!
        testInstrumentationRunner 'com.artemzin.qualitymatters.functional_tests.QualityMattersFunctionalTestsRunner'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        qualityMatters {
            storeFile file('keystore.jks')
            storePassword 'qualitymatters'
            keyAlias 'qualitymatters'
            keyPassword 'qualitymatters'
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.qualityMatters
            applicationIdSuffix '.debug'

            // Enables code coverage with Jacoco only for instrumentation tests.
            // Coverage for JVM tests done by code_quality_tools/jacoco.gradle.
            testCoverageEnabled true
        }

        release {
            signingConfig signingConfigs.qualityMatters
            minifyEnabled true
            proguardFiles file('../proguard').listFiles().toList().toArray()
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            // Enables code coverage with Jacoco only for instrumentation tests.
            // Coverage for JVM tests done by code_quality_tools/jacoco.gradle.
            testCoverageEnabled true
        }
    }

    sourceSets {
        // Unit tests live in src/unitTest.
        test.java.srcDir 'src/unitTests/java'

        // Integration tests live in src/integrationTest.
        test.java.srcDir 'src/integrationTests/java'

        // Unit tests for debug build type specific code live in src/debugUnitTest.
        testDebug.java.srcDir 'src/debugUnitTests/java'

        // Unit tests for release build type specific code live in src/releaseUnitTest.
        testRelease.java.srcDir 'src/releaseUnitTests/java'

        // Functional tests live in src/functionalTests.
        androidTest.java.srcDir 'src/functionalTests/java'
    }

    lintOptions {
        warningsAsErrors true
        abortOnError true // Fail early.

        disable 'GoogleAppIndexingWarning' // App does not have deep linking.
        disable 'InvalidPackage' // Okio references java.nio that does not presented in Android SDK.
    }

    packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

    // This allows us see tests execution progress in the output on the CI.
    testOptions.unitTests.all {
        testLogging {
            events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
            exceptionFormat 'full'
        }
    }
}

// Libraries declared in dependencies.gradle in the root of the project
dependencies {
    implementation libraries.dagger
    annotationProcessor libraries.daggerCompiler

    implementation libraries.rxJava
    releaseImplementation libraries.rxJavaProguardRules
    implementation libraries.rxLint

    implementation libraries.okHttp
    implementation libraries.retrofit
    implementation libraries.retrofitGsonConverter
    implementation libraries.retrofitRxJavaAdapter
    implementation libraries.gson

    // Do not compile AutoValue dependencies to the app.
    annotationProcessor libraries.autoValue
    annotationProcessor libraries.autoValueGson

    // Make AutoValue annotation visible to the compiler.
    compileOnly libraries.autoValue
    compileOnly libraries.autoValueGson

    implementation libraries.supportAnnotations
    implementation libraries.supportAppCompat
    implementation libraries.supportDesign
    implementation libraries.supportRecyclerView
    implementation libraries.supportCardView


    implementation (
            libraries.butterKnife,
            libraries.butterKnifeAnnotations
    )

    annotationProcessor libraries.butterKnifeCompiler

    implementation libraries.picasso
    implementation libraries.picassoOkHttpDownloader
    implementation libraries.timber

    // Analytics. Why? To make app more real.
    // We mock analytics for Integration tests under Robolectric and Functional (UI) tests under Instrumentation!
    implementation libraries.yandexAppMetrica

    // Developer tools (Developer Settings)
    debugImplementation libraries.okHttpLoggingInterceptor
    debugImplementation libraries.stetho
    debugImplementation libraries.stethoOkHttp
    debugImplementation libraries.leakCanary
    debugImplementation libraries.tinyDancer
    debugImplementation libraries.paperwork
    debugImplementation libraries.lynx
    debugImplementation libraries.processPhoenix

    testImplementation libraries.junit
    testImplementation libraries.robolectric
    testImplementation libraries.assertJ
    testImplementation libraries.equalsVerifier
    testImplementation libraries.okHttpMockWebServer
    testImplementation libraries.mockitoCore
    testImplementation libraries.devMetricsNoOp

    androidTestImplementation libraries.supportTestRunner
    androidTestImplementation libraries.supportTestRules
    androidTestImplementation libraries.espressoCore
    androidTestImplementation(libraries.espressoContrib) {
        // Causes IncompatibleClassChangeError. As always…
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
    }
    androidTestImplementation libraries.okHttpMockWebServer
}

configurations.all {
    resolutionStrategy {
        // Force our version of support-annotations, we have a conflict between app and androidTest dependency resolution.
        force libraries.supportAnnotations
    }
}

// Check if the build requires dex count
def withDexcount = project.hasProperty('withDexcount')
if (withDexcount) {
    apply plugin: 'com.getkeepsafe.dexcount'

    dexcount {
        format = 'list'
        includeFieldCount = true
        includeTotalMethodCount = true
        verbose = false
        maxTreeDepth = Integer.MAX_VALUE
    }
}
