apply plugin: 'com.android.application'
apply from: 'quality.gradle'

def externalPrivateVersion = new File(project.rootDir.path + "/app/external_version_code.txt").text.trim().toInteger()
def externalPublicVersion = new File(project.rootDir.path + "/app/external_version_name.txt").text.trim()
def internalPrivateVersion = new File(project.rootDir.path + "/app/internal_version_code.txt").text.trim().toInteger()
def internalPublicVersion = new File(project.rootDir.path + "/app/internal_version_name.txt").text.trim()
def isCircle = "true" == System.getenv("CIRCLECI")

ext.signing = [
    storeFilePath: "path/to/keystore",
    storePassword: "keystore password",
    keyAlias     : "key alias",
    keyPassword  : "key password"
]

if (file('signing.gradle').exists()) {
    apply from: 'signing.gradle'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "com.kickstarter"
        minSdkVersion 17
        targetSdkVersion 25
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        buildConfigField "String", "GIT_SHA", "\"${commitSha()}\""
        buildConfigField "String", "BUILD_DATE", "\"${commitTime()}\""

        // Enable Multidex to support over 65K methods
        multiDexEnabled true

        // allow for multiple language fallbacks
        resConfigs "auto"
    }

    signingConfigs {
        debug {
            storeFile file("debug.keystore")
        }
        release {
            storeFile file(project.signing.storeFilePath)
            storePassword project.signing.storePassword
            keyAlias project.signing.keyAlias
            keyPassword project.signing.keyPassword
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            if (isCircle) {
                // The release build generated on CircleCI doesn't need to be signed with our real
                // keystore - we just need a release build to verify that it compiles. Using the
                // debug keystore means we don't have to expose our keystore.
                signingConfig signingConfigs.debug
            } else {
                signingConfig signingConfigs.release
            }
        }
        debug {
            minifyEnabled false
            debuggable true
            applicationIdSuffix '.debug'
            signingConfig signingConfigs.debug
        }
    }

    flavorDimensions "AUDIENCE", "API"

    productFlavors {
        pre21 {
            dimension "API"
            // Don't override the minSdkVersion, just inherit from defaultConfig
        }
        min21 {
            // minSdkVersion 21 speeds up local development by generating multidex output much faster.
            // It should not be used for release builds. See:
            // https://developer.android.com/tools/building/multidex.html#dev-build
            dimension "API"
            minSdkVersion 21
        }
        internal {
            dimension "AUDIENCE"
            applicationId "com.kickstarter.kickstarter.internal"
            versionCode internalPrivateVersion
            versionName internalPublicVersion
        }
        external {
            dimension "AUDIENCE"
            applicationId "com.kickstarter.kickstarter"
            versionCode externalPrivateVersion
            versionName externalPublicVersion
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // Fixes build error hitting GC overhead limit
    dexOptions {
        javaMaxHeapSize "3072M"
    }

    lintOptions {
        quiet true
        warningsAsErrors true
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'LICENSE.txt'
    }

    sourceSets {
        androidTest {
            java.srcDir file('src/androidTest')
        }
        main.java.srcDirs += 'src/main/kotlin'
    }
}

repositories {
    jcenter()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:cardview-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    compile 'com.android.support:support-annotations:25.1.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.github.frankiesardo:auto-parcel:0.3.1'
    annotationProcessor 'com.github.frankiesardo:auto-parcel-processor:0.3.1'
    compile 'com.google.android.gms:play-services-gcm:10.0.1'
    compile 'com.google.android.gms:play-services-wallet:10.0.1'
    compile 'com.google.android.exoplayer:exoplayer:r1.5.5'
    compile 'com.google.dagger:dagger:2.0'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.jakewharton:process-phoenix:1.0.2'
    compile 'com.jakewharton.timber:timber:3.0.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'com.stripe:stripe-android:1.0.3'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.5.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.trello:rxlifecycle:0.3.0'
    compile 'com.trello:rxlifecycle-components:0.3.0'
    compile 'io.reactivex:rxandroid:1.2.0'
    compile 'io.reactivex:rxjava:1.1.5'
    compile 'com.jakewharton.rxbinding:rxbinding:0.3.0'
    compile 'com.jakewharton.rxbinding:rxbinding-recyclerview-v7:0.3.0'
    compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.3.0'
    compile 'net.danlew:android.joda:2.7.2'
    compile 'net.hockeyapp.android:HockeySDK:3.6.1'
    compile 'org.jsoup:jsoup:1.8.2'
    compile "com.mixpanel.android:mixpanel-android:4.6.4"

    // Testing
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.robolectric:shadows-multidex:3.0'
    testCompile "org.robolectric:shadows-play-services:3.0"
    testCompile "org.robolectric:shadows-support-v4:3.0"
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support:support-annotations:25.1.0'
}

// SHA and timestamp caching courtesy of https://github.com/gdg-x/frisbee/blob/develop/app/build.gradle#L193-L218
def commitSha() {
    def file = new File(project.buildDir, "commit-sha.txt")
    if (file.exists()) {
        return file.text
    }

    def value = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()

    file.parentFile.mkdirs()
    file.text = value
    return value
}

def commitTime() {
    def file = new File(project.buildDir, "commit-timestamp.txt")
    if (file.exists()) {
        return file.text
    }

    def date = new Date()
    def value = date.format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC"))

    file.parentFile.mkdirs()
    file.text = value
    return value
}

buildscript {
    ext.kotlin_version = '1.1.2-5'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

// Copy google-services.json from variant directory to root of app
gradle.taskGraph.beforeTask { Task task ->
    if (task.name ==~ /process.*GoogleServices/) {
        android.applicationVariants.all { variant ->
            if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
                copy {
                    from "src/${variant.name}"
                    into '.'
                    include 'google-services.json'
                }
            }
        }
    }
}

apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
