apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    google()
}

configurations {
    compile.exclude module: 'support-v4'
}

configurations.all {
    exclude group: 'com.google.firebase', module: 'firebase-core'
    exclude group: 'androidx.recyclerview', module: 'recyclerview'
}

dependencies {
    implementation 'androidx.core:core:1.10.0'
    implementation 'androidx.palette:palette:1.0.0'
    implementation 'androidx.exifinterface:exifinterface:1.3.6'
    implementation 'androidx.dynamicanimation:dynamicanimation:1.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation "androidx.sharetarget:sharetarget:1.2.0"
    implementation 'com.google.android.gms:play-services-measurement:21.3.0'

    compileOnly 'org.checkerframework:checker-qual:2.5.2'
    compileOnly 'org.checkerframework:checker-compat-qual:2.5.5'

    implementation platform('com.google.firebase:firebase-bom:32.1.0')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging:23.1.2'
    implementation 'com.google.firebase:firebase-config:21.4.0'
    implementation 'com.google.firebase:firebase-datatransport:18.1.8'
    implementation 'com.google.firebase:firebase-appindexing:20.0.0'
    implementation 'com.google.android.gms:play-services-maps:18.1.0'
    implementation 'com.google.android.gms:play-services-auth:20.5.0'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation 'com.google.android.gms:play-services-wearable:18.0.0'
    implementation 'com.google.android.gms:play-services-location:21.0.1'
    implementation 'com.google.android.gms:play-services-wallet:19.1.0'
    implementation 'com.googlecode.mp4parser:isoparser:1.0.6'
    implementation 'com.stripe:stripe-android:2.0.2'
    implementation 'com.google.mlkit:language-id:16.1.1'
    implementation 'com.android.billingclient:billing:5.2.0'
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation 'com.google.guava:guava:32.0.0-jre'

    def camerax_version = '1.3.0-alpha07'
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-extensions:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-view:${camerax_version}"
    implementation "androidx.camera:camera-video:${camerax_version}"
    implementation 'androidx.interpolator:interpolator:1.0.0'

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'

    implementation files('libs/libgsaverification-client.aar')
}

def isPmBuild = "false"
def abis = [9: 'universal', 1: 'armeabi-v7a', 5: 'arm64-v8a', 2: 'x86', 6: 'x86_64']

android {
    compileSdkVersion 33
    buildToolsVersion '33.0.1'
    ndkVersion "21.4.7075529"
    namespace 'org.telegram.messenger'

    defaultConfig.applicationId = APP_PACKAGE
    defaultConfig.versionCode = Integer.parseInt(APP_VERSION_CODE)

    Properties localProperties = new Properties()
    if (project.rootProject.file('API_KEYS').exists()) {
        localProperties.load(project.rootProject.file('API_KEYS').newDataInputStream())
    }

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        versionName APP_VERSION_NAME

        // Obtain your own keys at: https://core.telegram.org/api/obtaining_api_id
        buildConfigField 'int', 'APP_ID', localProperties.getProperty("APP_ID")
        buildConfigField 'String', 'APP_HASH', localProperties.getProperty("APP_HASH")

        // Google Console: https://console.cloud.google.com/google/maps-apis/credentials
        resValue 'string', 'MAPS_V2_API', localProperties.getProperty("MAPS_V2_API")

        externalNativeBuild {
            cmake {
                version '3.10.2'
                arguments '-DANDROID_STL=c++_static', '-DANDROID_PLATFORM=android-21'
            }
        }
    }

    externalNativeBuild.cmake.path 'jni/CMakeLists.txt'

    sourceSets.main {
        jniLibs.srcDirs = ['jni']
        //jniLibs.srcDir 'libs'
        //jni.srcDirs = []
        res.srcDirs = ['src/main/res', 'src/main/res-solar', 'src/main/res-tabs']
    }

    lint {
        checkReleaseBuilds false
        disable 'MissingTranslation', 'ExtraTranslation', 'BlockedPrivateApi'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11

        coreLibraryDesugaringEnabled true
    }

    signingConfigs {
        release {
            storeFile file("config/extera.jks")
            storePassword localProperties.getProperty("SIGNING_KEY_STORE_PASSWORD")
            keyAlias localProperties.getProperty("SIGNING_KEY_ALIAS")
            keyPassword localProperties.getProperty("SIGNING_KEY_PASSWORD")
        }
    }

    packagingOptions.exclude 'META-INF/DEPENDENCIES'

    buildTypes {
        debug {
            debuggable false
            jniDebuggable false
            signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources true
            multiDexEnabled true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk.debugSymbolLevel = 'FULL'
            buildConfigField "boolean", "DEBUG_VERSION", "true"
            buildConfigField "boolean", "IS_PM_BUILD", isPmBuild
        }

        release {
            debuggable false
            jniDebuggable false
            signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources true
            multiDexEnabled true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk.debugSymbolLevel = 'FULL'
            buildConfigField "boolean", "DEBUG_VERSION", "false"
            buildConfigField "boolean", "IS_PM_BUILD", isPmBuild
        }
    }

    flavorDimensions "minApi"

    productFlavors {
        armv7 {
            ndk.abiFilters "armeabi-v7a"
            ext.abiVersionCode = 1
        }
        x86 {
            ndk.abiFilters "x86"
            ext.abiVersionCode = 2
        }
        arm64 {
            ndk.abiFilters "arm64-v8a"
            ext.abiVersionCode = 5
        }
        x64 {
            ndk.abiFilters "x86_64"
            ext.abiVersionCode = 6
        }
        afat {
            ndk.abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
            sourceSets.release.manifest.srcFile 'config/release/AndroidManifest.xml'
            ext.abiVersionCode = 9
        }
        beta {
            ndk.abiFilters "armeabi-v7a", "arm64-v8a"
            sourceSets.debug.manifest.srcFile 'config/debug/AndroidManifest.xml'
            ext.abiVersionCode = 9
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def buildType = variant.productFlavors[0].name == "beta" ? "beta-" : ""
            def date = new Date()
            def formattedDate = date.format('ddMMyyyy')
            def abi = abis.get(variant.productFlavors.get(0).abiVersionCode)

            outputFileName = "exteraGram-${buildType}${abi}-${formattedDate}.apk"
            output.versionCodeOverride = defaultConfig.versionCode * (isPmBuild == "true" ? 100 : 10) + variant.productFlavors.get(0).abiVersionCode
        }
    }

    variantFilter { variant ->
        def names = variant.flavors*.name
        setIgnore(variant.buildType.name != "debug" && names.contains("beta") || variant.buildType.name != "release" && !names.contains("beta"))
    }

    bundle.language.enableSplit false
    dependenciesInfo.includeInApk false
}

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