// Loading nymeapp.properties. This file must be generated by the .pro file
Properties properties = new Properties()
properties.load(project.rootProject.file("nymeaapp.properties").newDataInputStream())

def nymeaAppRoot = properties.getProperty('nymeaAppRoot')
def useFirebase = properties.getProperty('useFirebase')

println "Building Android package"
println "Package source root ${nymeaAppRoot}"

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.2'
        classpath 'com.google.gms:google-services:4.3.8'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
        flatDir {
            dirs nymeaAppRoot + "/3rdParty/android/firebase_cpp_sdk/libs/android/"
        }
    }
}

apply plugin: 'com.android.application'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'org.reactivestreams:reactive-streams:1.0.3'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
    implementation 'androidx.core:core:1.3.2'

    if ("${useFirebase}" == "true") {
        implementation 'com.google.android.gms:play-services-base:18.1.0'
        implementation 'com.google.firebase:firebase-messaging:22.0.0'
        implementation 'com.google.firebase.messaging.cpp:firebase_messaging_cpp@aar'
    }
}

if ("${useFirebase}" == "true") {
    apply plugin: 'com.google.gms.google-services'
}

android {
    /*******************************************************
     * The following variables:
     * - androidBuildToolsVersion,
     * - androidCompileSdkVersion
     * - qt5AndroidDir - holds the path to qt android files
     *                   needed to build any Qt application
     *                   on Android.
     *
     * are defined in gradle.properties file. This file is
     * updated by QtCreator and androiddeployqt tools.
     * Changing them manually might break the compilation!
     *******************************************************/

    compileSdkVersion androidCompileSdkVersion.toInteger()
    buildToolsVersion androidBuildToolsVersion

    namespace 'io.guh.nymeaapp'
    buildFeatures.aidl = true

    packagingOptions {
        jniLibs {
            useLegacyPackaging = true
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = [
                qt5AndroidDir + '/src',
                nymeaAppRoot + '/androidservice/java',
                nymeaAppRoot + '/nymea-app/platformintegration/android/java',
                nymeaAppRoot + '/QtZeroConf/android',
                'src',
                'java']
            if ("${useFirebase}" == "true") {
                java.srcDirs += [nymeaAppRoot + '/nymea-app/platformintegration/android/java-firebase']
            }

            aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
            res.srcDirs = [qt5AndroidDir + '/res', 'res']
            resources.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
       }
    }

    lintOptions {
        abortOnError false
    }

    // Do not compress Qt binary resources file
    aaptOptions {
        noCompress 'rcc'
    }

    defaultConfig {
        resConfigs "en", "de", "ko", "it", "nl", "es"
        minSdkVersion = 23
        targetSdkVersion = 34
    }
}
