plugins {
    id "com.android.application"
}

ext {
    // The packageVariant defines the bootstrap variant that will be included in the app APK.
    // This must be supported by com.termux.shared.termux.TermuxBootstrap.PackageVariant or app will
    // crash at startup.
    // Bootstrap of a different variant must not be manually installed by the user after app installation
    // by replacing $PREFIX since app code is dependant on the variant used to build the APK.
    // Currently supported values are: [ "apt-android-7" "apt-android-5" ]
    packageVariant = System.getenv("TERMUX_PACKAGE_VARIANT") ?: "apt-android-7" // Default: "apt-android-7"
}

android {
    compileSdkVersion project.properties.compileSdkVersion.toInteger()
    ndkVersion = System.getenv("JITPACK_NDK_VERSION") ?: project.properties.ndkVersion
    def appVersionName = System.getenv("TERMUX_APP_VERSION_NAME") ?: ""
    def apkVersionTag = System.getenv("TERMUX_APK_VERSION_TAG") ?: ""
    def splitAPKsForDebugBuilds = System.getenv("TERMUX_SPLIT_APKS_FOR_DEBUG_BUILDS") ?: "1"
    def splitAPKsForReleaseBuilds = System.getenv("TERMUX_SPLIT_APKS_FOR_RELEASE_BUILDS") ?: "0" // F-Droid does not support split APKs #1904

    dependencies {
        implementation "androidx.appcompat:appcompat:1.3.1"
        implementation "androidx.annotation:annotation:1.3.0"
        implementation "androidx.core:core:1.6.0"
        implementation "androidx.drawerlayout:drawerlayout:1.1.1"
        implementation "androidx.preference:preference:1.1.1"
        implementation "androidx.viewpager:viewpager:1.0.0"
        implementation "com.google.android.material:material:1.4.0"
        implementation "com.google.guava:guava:24.1-jre"
        implementation "io.noties.markwon:core:$markwonVersion"
        implementation "io.noties.markwon:ext-strikethrough:$markwonVersion"
        implementation "io.noties.markwon:linkify:$markwonVersion"
        implementation "io.noties.markwon:recycler:$markwonVersion"
        implementation "org.lsposed.hiddenapibypass:hiddenapibypass:2.0"
        // Do not increment version higher than 1.0.0-alpha09 since it will break ViewUtils and needs to be looked into
        // noinspection GradleDependency
//        implementation "androidx.window:window:1.0.0-alpha09"

        // Do not increment version higher than 2.5 or there
        // will be runtime exceptions on android < 8
        // due to missing classes like java.nio.file.Path.
        implementation "commons-io:commons-io:2.5"

//        implementation "com.termux:termux-am-library:v2.0.0"
    }


    defaultConfig {
        manifestPlaceholders TERMUX_APP_NAME: 'Termos', TERMUX_PACKAGE_NAME: 'com.termos'
        applicationId 'com.termos'
        minSdkVersion project.properties.minSdkVersion.toInteger()
        targetSdkVersion project.properties.targetSdkVersion.toInteger()
        versionCode 24
        versionName "2.4"
        resourceConfigurations += ['en', 'ru', 'uk']

        buildConfigField "String", "TERMUX_PACKAGE_VARIANT", "\"" + project.ext.packageVariant + "\"" // Used by TermuxApplication class

        ndk {
            abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
        }

        externalNativeBuild {
            ndkBuild {
                cFlags "-std=c11", "-Wall", "-Wextra", "-Werror", "-Os", "-fno-stack-protector", "-Wl,--gc-sections"
            }
        }

        splits {
            abi {
                enable ((gradle.startParameter.taskNames.any { it.contains("Debug") } && splitAPKsForDebugBuilds == "1") ||
                    (gradle.startParameter.taskNames.any { it.contains("Release") } && splitAPKsForReleaseBuilds == "1"))
                reset ()
                include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                universalApk true
            }
        }
    }
	
    externalNativeBuild {
        ndkBuild {
            path "src/main/jni/Android.mk"
        }
    }


    signingConfigs {
        debug {
            storeFile file('testkey_untrusted.jks')
            keyAlias 'alias'
            storePassword 'xrj45yWGLbsO7W0v'
            keyPassword 'xrj45yWGLbsO7W0v'
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true // Reproducible builds
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }

        debug {
            minifyEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true

        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    namespace 'com.termux'
    lint {
        disable 'ProtectedPermissions'
    }


    packagingOptions {
        jniLibs {
            useLegacyPackaging true
            excludes += ['javax/**', 'org/**', 'META-INF/**']
        }
        resources {
            excludes += ['javax/**', 'org/**', 'META-INF/**']
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFileName
            if (variant.buildType.name == "debug") {
                outputFileName = "Termos_v${variant.versionName}_debug.apk"
            } else if (variant.buildType.name == "release") {
                outputFileName = "Termos_v${variant.versionName}.apk"
            }

            // Установка нового имени файла
            output.outputFileName = outputFileName
        }
    }
}

dependencies {
    implementation "androidx.annotation:annotation:1.3.0"
    implementation "androidx.core:core:1.6.0"
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'

}

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier.set("sources")
}

task versionName {
    doLast {
        print android.defaultConfig.versionName
    }
}
