plugins {
    id 'com.android.application'
}

android {
    def repoRoot = rootProject.rootDir.absoluteFile.parentFile
    def ndkPath = System.getenv("ANDROID_NDK_HOME")

    signingConfigs {
        debug {
            storeFile file('../keystore/public.jks')
            storePassword 'public'
            keyAlias 'public'
            keyPassword 'public'
        }
        release {
            storeFile file('../keystore/public.jks')
            storePassword 'public'
            keyAlias 'public'
            keyPassword 'public'
        }
    }
    namespace "org.libsdl.helloworld"
    compileSdk 35

    defaultConfig {
        applicationId 'com.gknext.renderer'
        minSdk 21
        targetSdk 35
        versionCode 1
        versionName '1.0'

        externalNativeBuild {
            cmake {
                arguments '-DANDROID_STL=c++_shared',
                        //'-DCMAKE_BUILD_TYPE=Release',
                        '-DANDROID_PLATFORM=33',
                        '-DVCPKG_CMAKE_SYSTEM_VERSION=33',
                        '-DVCPKG_TARGET_TRIPLET=arm64-android',
                        "-DCMAKE_TOOLCHAIN_FILE=${repoRoot}/.vcpkg/scripts/buildsystems/vcpkg.cmake",
                        "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${ndkPath}/build/cmake/android.toolchain.cmake"
                        //abiFilters 'arm64-v8a', 'x86_64'
                        abiFilters 'arm64-v8a'
            }
        }
    }

    externalNativeBuild {
        cmake {
            path '../../CMakeLists.txt'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            buildConfigField "boolean", "DEBUG", "false"
        }
        debug {
            buildConfigField "boolean", "DEBUG", "true"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        prefab true
        android.buildFeatures.buildConfig true
    }
}

dependencies {
    implementation files('libs/SDL3-3.2.22.aar')
}

task installAndLaunch(type: Exec) {
    doFirst {
        println "Installing app and launching..."
    }

    commandLine 'adb', 'install', "$buildDir/outputs/apk/debug/app-debug.apk"
    
    doLast {
        exec {
            commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.gknext.renderer/org.libsdl.helloworld.HelloWorldActivity'
        }
        println "App launched, starting logcat..."
        exec {
            commandLine 'adb', 'logcat', '-s', 'gknext'
        }
    }
}
