apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.noveogroup.android.check'
apply plugin: 'realm-android'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'com.google.firebase.firebase-perf'

buildscript {
    repositories {
        mavenLocal()
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath('com.noveogroup.android:check:1.2.5') {
            exclude module: 'checkstyle'
            exclude module: 'pmd-java'
        }
        classpath 'com.puppycrawl.tools:checkstyle:7.5'
        classpath 'net.sourceforge.pmd:pmd-java:5.5.3'
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    google()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

    // Material View Pager
    maven { url "https://dl.bintray.com/florent37/maven" }


    maven { url "https://jitpack.io" }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //Networking
    implementation 'com.squareup.okhttp3:okhttp:3.14.6'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.14.6'
    //REST API handling
    implementation('com.squareup.retrofit2:retrofit:2.7.1') {
        exclude module: 'okhttp'
    }
    implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.7.1'
    //Crash Logging
    implementation 'com.google.firebase:firebase-crashlytics:17.2.1'

    //Dependency Injection
    implementation 'com.google.dagger:dagger:2.27'
    kapt 'com.google.dagger:dagger-compiler:2.25.4'
    compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
    compileOnly 'com.github.pengrad:jdk9-deps:1.0'
    //App Compatibility and Material Design
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    // Emojis
    implementation 'com.github.viirus:emoji-lib:0.0.5'
    // Markdown
    implementation "io.noties.markwon:core:4.2.0"
    implementation "io.noties.markwon:ext-strikethrough:4.2.0"
    implementation "io.noties.markwon:image:4.2.0"
    implementation "io.noties.markwon:recycler:4.2.0"
    implementation "io.noties.markwon:html:4.2.0"
    // About View for all dependent Libraries, we are using
    implementation('com.mikepenz:aboutlibraries:5.9.4@aar') {
        transitive = true
    }
    //Eventbus
    implementation 'org.greenrobot:eventbus:3.1.1'
    // IAP Handling / Verification
    implementation 'org.solovyev.android:checkout:1.2.1'
    //Facebook
    implementation('com.facebook.android:facebook-android-sdk:4.40.0') {
        transitive = true
    }
    implementation 'fr.avianey.com.viewpagerindicator:library:2.4.1@aar'
    //RxJava
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.17'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'

    //Analytics
    implementation 'com.amplitude:android-sdk:2.23.2'
    // Fresco Image Management Library
    implementation('com.facebook.fresco:fresco:2.0.0') {
        exclude module: 'bolts-android'
    }
    implementation('com.facebook.fresco:animated-gif:2.0.0') {
        exclude module: 'bolts-android'
    }
    //Tests
    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.test:core:1.3.0'
    testImplementation "com.google.truth:truth:1.0.1"
    testImplementation 'org.assertj:assertj-core:2.6.0'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
    testImplementation 'org.robolectric:robolectric:4.3'
    testImplementation 'org.robolectric:shadows-multidex:3.8'
    testImplementation 'org.mockito:mockito-core:2.25.0'
    testImplementation 'org.powermock:powermock-api-mockito2:1.7.0'
    testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.7.0'
    testImplementation 'org.powermock:powermock-module-junit4-rule:1.7.0'
    testImplementation 'org.powermock:powermock-module-junit4:1.7.0'
    //Leak Detection
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
    //Push Notifications
    implementation 'com.google.firebase:firebase-core:17.5.0'
    implementation 'com.google.firebase:firebase-messaging:20.2.4'
    implementation 'com.google.firebase:firebase-config:19.2.0'
    implementation 'com.google.firebase:firebase-perf:19.0.8'
    implementation 'com.google.android.gms:play-services-auth:18.1.0'
    implementation 'io.realm:android-adapters:3.1.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.nex3z:flow-layout:1.2.2'

    implementation 'androidx.core:core-ktx:1.3.1'
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
    implementation "androidx.paging:paging-runtime-ktx:2.1.2"
    implementation 'com.plattysoft.leonids:LeonidsLib:1.3.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'

    implementation 'com.willowtreeapps:signinwithapplebutton:0.2'

    implementation project(':shared')
}

android {
    compileSdkVersion 30
    buildToolsVersion '29.0.3'
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }

    defaultConfig {
        minSdkVersion 21
        applicationId "com.habitrpg.android.habitica"
        vectorDrawables.useSupportLibrary = true
        buildConfigField "String", "STORE", "\"google\""
        buildConfigField "String", "TESTING_LEVEL", "\"production\""
        multiDexEnabled true
        resConfigs "en", "bg", "de", "en-rGB", "es", "fr", "hr-rHR", "in", "it", "iw", "ja", "ko", "lt", "nl", "pl", "pt-rBR", "pt-rPT", "ru", "tr", "zh", "zh-rTW"

        versionCode 2519
        versionName "2.8"
    }

    viewBinding {
        enabled = true
    }

    lintOptions {
        abortOnError false
    }

    viewBinding {
        enabled true
    }

    signingConfigs {
        release
    }
    flavorDimensions "buildType"

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            debuggable true
            multiDexEnabled true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // Disable fabric build ID generation for debug builds
            ext.enableCrashlytics = false
            ext.alwaysUpdateBuildId = false
            resValue "string", "content_provider", "com.habitrpg.android.habitica.debug.fileprovider"
            resValue "string", "app_name", "Habitica Debug"
        }
        release {
            signingConfig signingConfigs.release
            debuggable false
            multiDexEnabled true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            resValue "string", "content_provider", "com.habitrpg.android.habitica.fileprovider"
            resValue "string", "app_name", "Habitica"
        }
    }

    productFlavors {
        dev {
            dimension "buildType"
        }

        staff {
            dimension "buildType"
            buildConfigField "String", "TESTING_LEVEL", "\"staff\""
            resValue "string", "app_name", "Habitica Staff"
        }

        alpha {
            dimension "buildType"
            buildConfigField "String", "TESTING_LEVEL", "\"alpha\""
            resValue "string", "app_name", "Habitica Alpha"
        }

        beta {
            buildConfigField "String", "TESTING_LEVEL", "\"beta\""
            dimension "buildType"
        }

        prod {
            buildConfigField "String", "TESTING_LEVEL", "\"production\""
            dimension "buildType"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src/main/java']
            renderscript.srcDirs = ['src/main/java']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        /*robolectric {
            manifest.srcFile 'AndroidManifestTesting.xml'
            java.srcDir file('src/test/java/')
            res.srcDirs = ['res']
        }*/
        //instrumentTest.setRoot('tests')
        release { java.srcDirs = ['src/release/java'] }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    dexOptions {
        preDexLibraries false
        javaMaxHeapSize "6g" // Use gig increments depending on needs
    }

    lintOptions {
        disable 'MissingTranslation','InvalidPackage'
        enable 'LogConditional','IconExpectedSize','MissingRegistered','TypographyQuotes'
    }
}

Properties props = new Properties()
def propFile = new File('signingrelease.properties')
if (propFile.canRead()) {
    props.load(new FileInputStream(propFile))

    if (props != null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        println 'signing.properties found but some entries are missing'
        android.buildTypes.release.signingConfig = null
    }
} else {
    println 'signing.properties not found'
    android.buildTypes.release.signingConfig = null
}

// Add Habitica Properties to buildConfigField
final File HRPG_PROPS_FILE = new File('habitica.properties')
if (HRPG_PROPS_FILE.canRead()) {
    Properties HRPG_PROPS = new Properties()
    HRPG_PROPS.load(new FileInputStream(HRPG_PROPS_FILE))

    if (HRPG_PROPS != null) {
        android.buildTypes.all { buildType ->
            HRPG_PROPS.any { property ->
                buildType.buildConfigField "String", property.key, "\"${property.value}\""
            }
        }
    } else {
        throw new InvalidUserDataException('habitica.properties found but some entries are missing')
    }
} else {
    throw new MissingResourceException('habitica.properties not found')
}

// Add Habitica Resources to resources
final File HRPG_RES_FILE = new File('habitica.resources')
if (HRPG_RES_FILE.canRead()) {
    Properties HRPG_RES = new Properties()
    HRPG_RES.load(new FileInputStream(HRPG_RES_FILE))

    if (HRPG_RES != null) {
        android.buildTypes.all { buildType ->
            HRPG_RES.any { property ->
                buildType.resValue "string", property.key, "\"${property.value}\""
            }
        }
    } else {
        throw new InvalidUserDataException('habitica.resources found but some entries are missing')
    }
} else {
    throw new MissingResourceException('habitica.resources not found')
}

tasks.whenTaskAdded { task ->
    if (task.name == "lint") {
        task.enabled = false
    }
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "500"
    }
}

check { findbugs { skip true } }

apply plugin: 'com.google.gms.google-services'
