apply plugin: 'com.android.@(LIBRARY:defined:test('library', 'application'))'
#if @(KOTLIN:defined)
apply plugin: 'kotlin-android'
#endif

configurations { native_implementation }

dependencies {
    implementation fileTree(dir: 'src/main/libs', include: ['*.jar'])
    @(gradle.dependency.implementation:join('\n    ', 'implementation \'', '\''))
    @(gradle.dependency.debugImplementation:join('\n    ', 'debugImplementation \'', '\''))
    @(gradle.dependency.releaseImplementation:join('\n    ', 'releaseImplementation \'', '\''))
    @(gradle.dependency.nativeImplementation:join('\n    ', 'native_implementation \'', '\''))
    @(gradle.dependency:join('\n    '))
}

// Extracts native libraries from AARs in the native_implementation configuration.
// This is done so that the NDK can access these libraries.
task extractNativeLibraries() {
    doFirst {
        configurations.native_implementation.files.each { f ->
            copy {
                from zipTree(f)
                into 'build/native'
                include 'jni/**/*'
            }
        }
    }
}

tasks.whenTaskAdded {
    task-> if (task.name.contains('external') && !task.name.contains('Clean')) {
#if @(gradle.dependency.nativeImplementation:isRequired)
        task.dependsOn(extractNativeLibraries)
#endif
    }
}

repositories {
    @(gradle.repository:join('\n    '))
}

android {
    compileSdk @(sdk.compileVersion)
    ndkVersion '@(ndk.version)'

    namespace '@(activity.package)'

    defaultConfig {
#if !@(LIBRARY:defined)
        applicationId = '@(activity.package)'
#endif
        minSdk @(sdk.minVersion)
        targetSdk @(sdk.targetVersion)
        versionCode = @(project.android.versionCode)
        versionName = '@(project.android.versionName)'
        multiDexEnabled @(project.android.multiDexEnabled:isSet:test(@(project.android.multiDexEnabled:bool),true))

        ndk {
#if @(ANDROID_EMU:defined)
            abiFilters = ["@(config.android.emulator.architecture || 'x86_64')"]
#elif @(DEBUG:defined)
            abiFilters = [@(project.android.architectures.debug:join(', ', '\'', '\''))]
#else
            abiFilters = [@(project.android.architectures.release:join(', ', '\'', '\''))]
#endif
        }

        externalNativeBuild {
            cmake {
                // The build configuration of the native binary might differ from the build
                // configuration of the APK. For performance reasons, we don't want to use the
                // Fuse Simulator on the significantly slower debug binaries, so building debug by
                // default means we are giving users a bad experience by default. Native debug
                // binaries are only really useful when actually debugging generated C++ code.
                // Running 'uno build android --debug' will produce debuggable C++ code.
                arguments '-DCMAKE_BUILD_TYPE=@(native.configuration)',
                          '-DANDROID_PLATFORM=android-@(ndk.platformVersion)',
                          '-DANDROID_STL=@(STL)',
                          '-DANDROID_TOOLCHAIN=clang'
            }
        }

#if @(project.android.supportedLanguages:isSet)
        resConfigs @(project.android.supportedLanguages:join(', ', '\"', '\"'))
#endif

    }

#if @(project.android.bundle.language.enableSplit:isSet) || @(project.android.bundle.density.enableSplit:isSet) || @(project.android.bundle.abi.enableSplit:isSet)
    bundle {
        #if @(project.android.bundle.language.enableSplit:isSet)
        language {
            // Specifies that the app bundle should not support
            // configuration APKs for language resources. These
            // resources are instead packaged with each base and
            // dynamic feature APK.
            enableSplit = @(project.android.bundle.language.enableSplit:toLower)
        }
        #endif
        #if @(project.android.bundle.density.enableSplit:isSet)
        density {
            // This property is set to true by default.
            enableSplit = @(project.android.bundle.density.enableSplit:toLower)
        }
        #endif
        #if @(project.android.bundle.abi.enableSplit:isSet)
        abi {
            // This property is set to true by default.
            enableSplit = @(project.android.bundle.abi.enableSplit:toLower)
        }
        #endif
    }
#endif

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

#if @(project.android.key.store:isSet)
    signingConfigs {
        release {
            keyAlias '@(project.android.key.alias)'
            keyPassword '@(project.android.key.aliasPassword)'
            storeFile file('@(project.android.key.store:path)')
            storePassword '@(project.android.key.storePassword)'
        }
    }
#endif

    buildTypes {
        release {
#if @(DEBUG:defined)
            debuggable = true
#elif @(project.android.key.store:isSet)
            signingConfig = signingConfigs.release
#endif
            minifyEnabled = false
            proguardFiles 'proguard-rules.pro'
        }
    }

    aaptOptions {
        cruncherEnabled = false
    }

    lintOptions {
        checkReleaseBuilds = false
    }

#if @(KOTLIN:defined)
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
#endif

    @(gradle.android.end:join('\n'))
}

@(gradle.buildFile.end:join('\n'))
