plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.github.anyportal.anyportal"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.github.anyportal.anyportal"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        // targetSdk = flutter.targetSdkVersion
        // targetSdk = 28
        versionCode = flutter.versionCode
        versionName = flutter.versionName

        /// will not work since android.externalNativeBuild is not enabled
        ndk {
            // abiFilters "armeabi-v7a","arm64-v8a",'x86','x86_64'
        }
        externalNativeBuild {
            ndkBuild {
                // arguments "APP_CFLAGS+=-DPKGNAME=com/github/anyportal/anyportal -ffile-prefix-map=${rootDir}=."
                // arguments "APP_LDFLAGS+=-Wl,--build-id=none"
            }
        }

        project.android.applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.versionCodeOverride = flutter.versionCode
            }
        }
    }

    flavorDimensions "target"
    productFlavors {
        apilatest {
            dimension "target"
            targetSdk = flutter.targetSdkVersion
        }

        api28 {
            dimension "target"
            targetSdk = 28
        }
    }

    def keystorePropertiesFile = rootProject.file('key.properties')
    def keystoreProperties = new Properties()

    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    } else {
        println "No key.properties file found."
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.release
        }
        release {
            signingConfig signingConfigs.release
            // minifyEnabled false
            // shrinkResources false
            // Specify the ProGuard rules file
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    /// this is the original and native method, but would cause "[CXX1400] More than one externalNativeBuild path specified"
    /// see runNdkBuild at the end of the file for current alternative
    // externalNativeBuild {
    //     ndkBuild {
    //         path "src/main/jni/Android.mk"
    //     }
    // }

    lint {
        disable 'ExpiredTargetSdkVersion'
    }

    // tasks.withType(JavaCompile).configureEach {
    //     options.compilerArgs += ["-Xlint:unchecked"]
    // }
}

flutter {
    source = "../.."
}

dependencies {
    implementation (name:'libv2raymobile', ext:'aar')
    // implementation 'dev.rikka.shizuku:api:13.1.5'
}

// https://emunix.org/en/post/android-ndk-cmake-and-ndkbuild/
task runNdkBuild(type: Exec) {
    def os = System.getProperty("os.name").toLowerCase()
    def ndkBuildExec = os.contains("win") ? "ndk-build.cmd" : "ndk-build"
    def ndkDir = android.ndkDirectory
    executable = "$ndkDir/$ndkBuildExec"
    args = [
        'NDK_PROJECT_PATH=build/intermediates/ndk',
        'NDK_LIBS_OUT=src/main/jniLibs',
        'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
        'NDK_APPLICATION_MK=src/main/jni/Application.mk',

        "APP_CFLAGS=-DPKGNAME=com/github/anyportal/anyportal -ffile-prefix-map=${rootDir}=.",
        "APP_LDFLAGS=-Wl,--build-id=none",
        "APP_ABI=all",
    ]
}

preBuild.dependsOn runNdkBuild
