apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    def appVersionName = project.properties['APP_VERSION_NAME'] ?: "1.0.999"
    defaultConfig {
        applicationId "org.example.viotester"
        versionCode 1
        versionName appVersionName
        minSdkVersion 24
        targetSdkVersion 29

        def hasCustomVio = new File("custom-vio/viotester-integration").isDirectory()
        def buildGpuExamples = project.hasProperty('USE_GPU_EXAMPLES')

        buildConfigField "boolean", "USE_SLAM", "true"
        buildConfigField "boolean", "USE_CUSTOM_VIO", "${hasCustomVio}"
        buildConfigField "boolean", "USE_GPU_EXAMPLES", "${buildGpuExamples}"
        buildConfigField "boolean", "USE_CAMERA_CALIBRATOR", "true"

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang",
                        "-DANDROID_STL=c++_static",
                        "-DCMAKE_VERBOSE_MAKEFILE=ON",
                        "-DUSE_CUSTOM_VIO=${hasCustomVio ? 'ON' : 'OFF'}",
                        "-DUSE_GPU_EXAMPLES=${buildGpuExamples}"
                // speed up by compiling only for relevant architectures
                abiFilters "arm64-v8a"
                targets "vio_main"
            }
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/jni/CMakeLists.txt"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    flavorDimensions "mode", "type"

    productFlavors {
        // NOTE: the default build variant is either the one that is listed first or the
        // first one alphabetically, depending on Android Studio version. Make sure the
        // intended default is actually the first one when sorted alphabetically too
        a_default {
            dimension "mode"
        }
        noslam {
            dimension "mode"
            buildConfigField "boolean", "USE_SLAM", "false"
            versionNameSuffix "-noslam"
            externalNativeBuild {
                cmake {
                    arguments += "-DUSE_SLAM=OFF"
                }
            }
        }

        arcore {
            dimension "mode"
            versionNameSuffix "-arcore"
        }
        arengine {
            dimension "mode"
            versionNameSuffix "-arengine"
        }
        arcoreandengine {
            dimension "mode"
            versionNameSuffix "-arcore-arengine"
            repositories { // applies to all build variants
                maven {
                    url 'https://developer.huawei.com/repo/' // AREngine from Huawei
                }
            }
        }

        research {
            dimension "type"
            buildConfigField "boolean", "DEMO_MODE", "false"
            externalNativeBuild {
                cmake {
                    arguments += "-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo"
                }
            }
        }
        demo {
            dimension "type"
            buildConfigField "boolean", "DEMO_MODE", "true"
            buildConfigField "boolean", "USE_CAMERA_CALIBRATOR", "false"
            versionNameSuffix "-demo"
            externalNativeBuild {
                cmake {
                    arguments += "-DCMAKE_BUILD_TYPE:STRING=Release"
                    arguments += "-DUSE_CAMERA_CALIBRATOR=OFF"
                }
            }
            //applicationIdSuffix ".demo"
        }

    }

    dependencies {
        // needed by permission helper
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation "com.google.android.material:material:1.2.1"
        implementation 'androidx.preference:preference:1.1.1'
        implementation 'com.google.firebase:firebase-crashlytics:17.2.1'
        implementation 'org.kamranzafar:jtar:2.2'
        implementation 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
        // may be needed or ARCore + Location recording may not work... which is VERY SUSPICIOUS
        implementation 'com.google.android.gms:play-services-location:17.1.0'
        implementation 'com.google.android.gms:play-services-maps:17.0.0'
        implementation 'com.google.maps.android:android-maps-utils:2.1.0'

        arcoreImplementation 'com.google.ar:core:1.21.0'
        arcoreandengineImplementation 'com.google.ar:core:1.21.0'

        arengineImplementation 'com.huawei.hms:arenginesdk:2.13.0.4'
        arcoreandengineImplementation 'com.huawei.hms:arenginesdk:2.13.0.4'
    }

    // delete large DBoW2 file for the non-SLAM build
    // see https://stackoverflow.com/a/39760001/1426569
    // another option would be to symlink the file to each other variant asset folder separately...
    // Like currently done for jniLibs, since it's not possible to exclude them by build variant,
    // a feature that has existed in some Gradle versions but recently broken without workaround
    applicationVariants.all { variant ->
        println "just listing the build variants: ${variant.name}"
        if (variant.name.contains('noslam')) {
            // For the record, I absolutely despise Gradle. This thing, which I found on
            // StackOverflow works, but it is not documented... I mean, at all. Just Google
            // "mergeAssetsProvider". All hits are StackOverflow or Github issues. You can't even
            // find the source code for this DSL. In addition, this these APIs change and break
            // every few months.
            variant.mergeAssetsProvider.configure {
                doLast {
                    println "now trying to delete all .dbow2 files in ${outputDir}"
                    delete(fileTree(dir: outputDir, includes: ['**/*.dbow2']))
                }
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/ASL2.0'
    }
}

apply plugin: 'com.google.gms.google-services'
