buildscript {
    ext {
        buildToolsVersion = "35.0.0"
        minSdkVersion = 24
        compileSdkVersion = 35
        targetSdkVersion = 35
        VisionCameraCodeScanner_targetSdkVersion = 31
        VisionCameraCodeScanner_compileSdkVersion = 31
        ndkVersion = "27.1.12297006"
        kotlinVersion = "2.0.21"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
    }

    allprojects {
    repositories {
        // ... local react native repos
        maven { url "https://cdn.veriff.me/android/" } //veriff
        google()
        jcenter()
    }
    }
}

// Global fix for externalNativeBuildClean tasks that fail due to malformed JSON
// This handles corrupted CMake cache files in all modules (react-native-screens, etc.)
allprojects {
    afterEvaluate { project ->
        tasks.matching { it.name.contains("externalNativeBuildClean") }.configureEach { task ->
            task.doFirst {
                // Clean .cxx directories that may contain corrupted JSON files
                def cxxDir = file("${project.projectDir}/.cxx")
                if (cxxDir.exists()) {
                    try {
                        delete cxxDir
                    } catch (Exception e) {
                        // If deletion fails, try to delete individual files
                        cxxDir.listFiles()?.each { file ->
                            try {
                                if (file.isDirectory()) {
                                    delete file
                                } else {
                                    file.delete()
                                }
                            } catch (Exception ex) {
                                // Ignore individual file deletion errors
                            }
                        }
                    }
                }
                
                // Also clean CMake cache directories that may contain corrupted JSON
                def buildDir = file("${project.projectDir}/build")
                if (buildDir.exists()) {
                    def cmakeCacheDirs = [
                        "${buildDir}/intermediates/cmake",
                        "${buildDir}/intermediates/ndkBuild",
                        "${buildDir}/.cxx"
                    ]
                    cmakeCacheDirs.each { dirPath ->
                        def dir = file(dirPath)
                        if (dir.exists()) {
                            try {
                                delete dir
                            } catch (Exception e) {
                                // Ignore if can't delete
                            }
                        }
                    }
                }
            }
            
            // Skip task if we can't clean the directories (prevents JSON parsing errors)
            task.onlyIf {
                // Always try to run, but we've cleaned the problematic directories in doFirst
                true
            }
        }
    }
}

apply plugin: "com.facebook.react.rootproject"