plugins {
    alias(libs.plugins.android.application)
}

def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags', '--always'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

def getSentryUrl = { ->
	def url = System.env.SENTRY_UPLOAD_URL
	return url == null ? "" : url
}

android {
    namespace 'com.flycast.emulator'
    ndkVersion '23.2.8568313'
    compileSdk 34

    defaultConfig {
        applicationId "com.flycast.emulator"
        minSdk 16
        //noinspection ExpiredTargetSdkVersion
        targetSdk 34
        versionCode 8
        versionName getVersionName()
        vectorDrawables.useSupportLibrary = true

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_ARM_MODE=arm", "-DSENTRY_UPLOAD_URL=" + getSentryUrl()
            }
        }

        ndk {
            moduleName 'flycast'
        }
        lintOptions {
            // avoid Error: Google Play requires that apps target API level 31 or higher.
            abortOnError false
        }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        debug {
            storeFile file("../debug.keystore")
        }
        release {
            storeFile file("../playstore.jks")
            storePassword System.getenv("ANDROID_KEYSTORE_PASSWORD")
            keyAlias 'uploadkey'
            keyPassword System.getenv("ANDROID_KEYSTORE_PASSWORD")
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    externalNativeBuild {
        cmake {
            path file('../../../CMakeLists.txt')
            version '3.22.1'
        }
    }
    packagingOptions {
        resources {
            excludes += ['META-INF/DEPENDENCIES']
        }
    }
    packaging {
        // This is necessary for libadrenotools custom driver loading
        jniLibs.useLegacyPackaging = true
    }
}

dependencies {
    implementation libs.appcompat
    implementation libs.commons.lang3
    implementation libs.httpclient5
    implementation libs.slf4j.android
    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
    implementation libs.documentfile

    androidTestImplementation 'androidx.test:runner:1.5.0'
    androidTestImplementation 'androidx.test:rules:1.5.0'
    androidTestImplementation 'androidx.test:core:1.5.0'
    androidTestImplementation 'junit:junit:4.12'
}
