plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlinx-serialization'
    id 'com.google.dagger.hilt.android'
    id 'de.mobilej.unmock'
    id 'com.google.protobuf'
    id 'kotlin-kapt'
}

unMock {
    keep "android.net.Uri"
    keep "android.util.Base64"
}

def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.geeksville.mesh"
        minSdkVersion 21 // The oldest emulator image I have tried is 22 (though 21 probably works)
        targetSdkVersion 31
        versionCode 30106 // format is Mmmss (where M is 1+the numeric major number
        versionName "2.1.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // per https://developer.android.com/studio/write/vector-asset-studio
        vectorDrawables.useSupportLibrary = true
    }
    flavorDimensions 'default'
    productFlavors {
        fdroid {
            dimension = 'default'
        }
        google {
            dimension = 'default'
            if (project.findProperty("useCrashlytics") == true) {
                println("useCrashlytics plugins $useCrashlytics")
                apply plugin: 'com.google.gms.google-services'
                apply plugin: 'com.google.firebase.crashlytics'
            }
        }
    }
    buildTypes {
        release {
            if (keystoreProperties['storeFile']) {
                signingConfig signingConfigs.release
            }
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            pseudoLocalesEnabled true
        }
    }
    defaultConfig {
        // We have to list all translated languages here, because some of our libs have bogus languages that google play
        // doesn't like and we need to strip them (gr)
        resConfigs "cs", "de", "el", "en", "es", "fi", "fr", "fr-rHT", "ga", "hu", "is", "it", "ja", "ko", "nl", "nb", "pl", "pt", "pt-rBR", "ro", "ru", "sk", "sl", "sq", "sv", "tr", "zh", "uk"

        ndk {
            // abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
    }
    buildFeatures {
        viewBinding true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.4"
    }
    // Set both the Java and Kotlin compilers to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs += ['-opt-in=kotlin.RequiresOptIn']
    }
    lint {
        abortOnError false
    }
    namespace 'com.geeksville.mesh'
}

// per protobuf-gradle-plugin docs, this is recommended for android
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:$protobuf_version"
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    // turned off for now so I can use json printing in debug panel
                    // use the smaller android version of the library
                    //option "lite"
                }
                kotlin {
                }
            }
        }
    }
}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    def appcompat_version = '1.6.1'
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    // For loading and tinting drawables on older versions of the platform
    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.fragment:fragment-ktx:1.5.6'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0'
    implementation 'androidx.datastore:datastore:1.0.0'

    // Lifecycle
    def lifecycle_version = '2.6.1'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

    // Room
    def room_version = '2.5.1'
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"
    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"

    // Compose
    def composeBom = platform('androidx.compose:compose-bom:2023.01.00')
    implementation composeBom
    androidTestImplementation composeBom

    implementation 'androidx.compose.material:material'
    implementation 'androidx.compose.runtime:runtime-livedata'
    implementation "com.google.accompanist:accompanist-themeadapter-appcompat:0.29.2-rc"

    // Android Studio Preview support
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Osmdroid & Maps
    def osmdroid_version = '6.1.14'
    implementation "org.osmdroid:osmdroid-android:$osmdroid_version"
    implementation "org.osmdroid:osmdroid-wms:$osmdroid_version"
    implementation "org.osmdroid:osmdroid-geopackage:$osmdroid_version"
    implementation 'com.github.MKergall:osmbonuspack:6.9.0'
    implementation('mil.nga.mgrs:mgrs-android:2.2.1') { exclude group: 'com.google.android.gms' }

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    // kotlin serialization
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"

    // rate this app
    googleImplementation 'com.suddenh4x.ratingdialog:awesome-app-rating:2.4.0'

    // Coroutines
    def coroutines_version = '1.6.4'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    // For now I'm not using javalite, because I want JSON printing
    implementation "com.google.protobuf:protobuf-kotlin:$protobuf_version"

    // For UART access
    // implementation 'com.google.android.things:androidthings:1.0'
    implementation 'com.github.mik3y:usb-serial-for-android:3.4.6'

    // location services
    googleImplementation 'com.google.android.gms:play-services-location:19.0.1'

    // For Firebase Crashlytics & Analytics
    googleImplementation 'com.google.firebase:firebase-crashlytics:18.3.6'
    googleImplementation 'com.google.firebase:firebase-analytics:21.2.1'

    // barcode support
    // per https://github.com/journeyapps/zxing-android-embedded#older-sdk-versions for minSdkVersion 21
    implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
    //noinspection GradleDependency
    implementation 'com.google.zxing:core:3.3.0' // <-- don't update

    def work_version = '2.8.1'
    // Work Request - used to delay boot event handling
    implementation "androidx.work:work-runtime-ktx:$work_version"

    implementation "androidx.core:core-splashscreen:1.0.0"

    // CompletableFuture backport for API 14+
    implementation 'net.sourceforge.streamsupport:streamsupport-minifuture:1.7.4'

    // App intro
    implementation 'com.github.AppIntro:AppIntro:6.2.0'
}

kapt {
    correctErrorTypes true
}

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