apply plugin: 'com.android.library'
apply from: "android_common.gradle"

// The arcore aar library contains the native shared libraries.  These are
// extracted before building to a temporary directory.
def arcore_libpath = "${buildDir}/arcore-native"

def android_arch = System.getenv("ANDROID_ARCH")
def abi_filters = android_arch != null ? [android_arch] : ['armeabi-v7a', 'arm64-v8a']

android {
    defaultConfig {
        ndk {
            abiFilters = []
            abiFilters.addAll(abi_filters)
        }
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_static", "-DARCORE_LIBPATH=${arcore_libpath}/jni", "-DARCORE_INCLUDE=${project.projectDir}/arcore_sdk/include"
            }
        }
    }

    externalNativeBuild {
        cmake {
            version '3.22.1'
            path '../../../CMakeLists.txt'
            buildStagingDirectory "${project.rootDir}/../../build/Android/.cxx"
        }
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'ndk_helper/src/java']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    namespace 'com.diligentengine.android.common'
}

// Create a configuration to mark which aars to extract .so files from
configurations { natives }

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // ARCore library
    implementation 'com.google.ar:core:1.30.0'
    natives 'com.google.ar:core:1.30.0'

    implementation 'com.android.support:appcompat-v7:28.0.0'
}


// Extracts the shared libraries from aars in the natives configuration.
// This is done so that NDK builds can access these libraries.
task extractARCoreLibraries() {
    // Always extract, this insures the native libs are updated if the version changes.
    outputs.upToDateWhen { false }
    doFirst {
        configurations.natives.files.each { f ->
            copy {
                from zipTree(f)
                into arcore_libpath
                include "jni/**/*"
            }
        }
    }
}

tasks.whenTaskAdded {
    task-> if ((task.name.contains("external") || task.name.contains("CMake")) && !task.name.contains("Clean")) {
        task.dependsOn(extractARCoreLibraries)
    }
}
