buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.ajoberstar:gradle-git:0.12+'
    }
}

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

dependencies {
    implementation project(':ABVJE_UI_Android')
    implementation 'androidx.multidex:multidex:2.0.0'
}

android {
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 35
        compileSdk 34
        multiDexEnabled true

        // from gradle.properties
        applicationId app_custompackagename
        versionCode app_versioncode as int;
        versionName version_name

        buildConfigField("boolean", "isStoreProduct", "${isStoreProduct}")
        manifestPlaceholders = [HOME_APP: "android.intent.category.DEFAULT"]
    }

    signingConfigs {
        release {
            storeFile file(keystore)
            storePassword keystorepassword
            keyAlias keyalias
            keyPassword keyaliaspassword
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['res']
            aidl.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    lintOptions {
        quiet true
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'values/attrs.xml'
        exclude 'values/dimens.xml'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    flavorDimensions "default" // OK
    productFlavors {

        if (isStoreProduct.toBoolean()) {
            armv7 {
                versionCode defaultConfig.versionCode
                ndk {
                    abiFilters "armeabi-v7a", ""
                }
            }

            armv8 {
                versionCode defaultConfig.versionCode + 1
                ndk {
                    abiFilters "arm64-v8a", ""
                }
            }

            x86 {
                versionCode defaultConfig.versionCode + 2
                ndk {
                    abiFilters "x86", ""
                }
            }

            x86_64 {
                versionCode defaultConfig.versionCode + 3
                ndk {
                    abiFilters "x86_64", ""
                }
            }
        } else {
            armv7 {
                versionCode defaultConfig.versionCode
                ndk {
                    abiFilters "armeabi-v7a", ""
                }
            }

            armv8 {
                versionCode defaultConfig.versionCode + 1
                ndk {
                    abiFilters "arm64-v8a", ""
                }
            }
        }
    }
}

import org.ajoberstar.grgit.*
task gitInfo {
    doLast {
        description 'update githash.txt with current hash'
        def grgit = Grgit.open(project.file('..'))
        def gitfile = file('assets/githash.txt')
        gitfile.write grgit.head().id
    }
}
preBuild.dependsOn gitInfo

task convertToPackage {
    doLast {
        description 'convert to package for google-services.json'
        def appModuleRootFolder = '.'
        def googleServicesJson = 'google-services.json'
        copy {
            from "fcm"
            include "$googleServicesJson"
            into "$appModuleRootFolder"
            expand PACKAGE_NAME : app_custompackagename
        }
    }
}
preBuild.dependsOn convertToPackage

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
        options.compilerArgs << "-Xlint:unchecked" <<  "-Xlint:deprecation"
    }
}
