def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.ydq.android.gradle.native-aar.import'

// uncomment this to upload proguard mapping file and debug symbols to Sentry
// and also set the token (sentry.properties) file
// apply plugin: 'io.sentry.android.gradle'

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
        languageVersion = "1.4"
    }

    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "io.sentry.samples.flutter"
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion 33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName

        externalNativeBuild {
            cmake {
                arguments.add(0, "-DANDROID_STL=c++_static")
            }
        }

        ndk {
            // Flutter does not currently support building for x86 Android (See Issue 9253).
            abiFilters("armeabi-v7a", "x86_64", "arm64-v8a")
        }
    }

    // TODO: we need to fix CI as the version 21.1 (default) is not installed by default on
    // GH Actions.
    ndkVersion "25.1.8937393"

    externalNativeBuild {
        cmake {
            setPath("CMakeLists.txt")
        }
    }

    buildTypes {
        release {
            // looks like Flutter requires minifyEnabled to be enabled or it throws
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

    lintOptions {
        // workaround if using AGP 4.0 on release mode
        // https://github.com/flutter/flutter/issues/58247
        checkReleaseBuilds false
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "androidx.annotation:annotation:1.1.0"
}

// uncomment this to upload debug symbols to Sentry
// sentry {
//     uploadNativeSymbols = true
//     includeNativeSources = true
// }
