apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdk 34
    ndkVersion "25.2.9519653"

    defaultConfig {
        applicationId "net.activitywatch.android"
        minSdkVersion 24
        targetSdkVersion 34

        // Set in CI on tagged commit
        // FIXME: should be set before tagging, so that F-droid picks it up correctly
        //        https://gitlab.com/fdroid/fdroiddata/-/merge_requests/11786?commit_id=d2cedcbe3d26db59378d582a8cf952af16b6407f#note_1966750559
        versionName "0.12.1"

        // Set in CI by `bundle exec fastlane update_version`
        versionCode 34

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        // NOTE: This only works for API level 28 and above
        //testInstrumentationRunnerArguments useTestStorageService: 'true'

        // WARNING: Never commit this uncommented! (leads to huge APKs)
        //packagingOptions {
        //    doNotStrip '**/*.so'
        //}
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                debugSymbolLevel 'SYMBOL_TABLE'
            }
        }
        debug {
            applicationIdSuffix ".debug"
            jniDebuggable true
            renderscriptDebuggable true
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
    namespace 'net.activitywatch.android'
    // Never got this to work...
    //if (project.hasProperty("doNotStrip")) {
    //    packagingOptions {
    //        doNotStrip '**/*.so'
    //    }
    //}

    // Creates a resource versionName with the full version
    // https://stackoverflow.com/a/36468650/965332
    applicationVariants.all { variant ->
        variant.resValue "string", "versionName", 'v' + variant.versionName
    }

    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.annotation:annotation:1.5.0'

    implementation 'com.google.android.material:material:1.7.0'
    implementation 'com.jakewharton.threetenabp:threetenabp:1.4.3'

    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit-ktx:$extJUnitVersion"
    androidTestImplementation "androidx.test:runner:$androidXTestVersion"
    androidTestImplementation "androidx.test:rules:$androidXTestVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    androidTestUtil "androidx.test.services:test-services:$servicesVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
}

// Can be used to build with: ./gradlew cargoBuild
// NOTE: Doesn't work, chokes on building openssl-sys
apply plugin: 'org.mozilla.rust-android-gradle.rust-android'

cargo {
    module  = "../aw-server-rust"       // Or whatever directory contains your Cargo.toml
    libname = "aw_server"          // Or whatever matches Cargo.toml's [package] name.
    targets = ["arm", "arm64", "x86", "x86_64"]  // See bellow for a longer list of options
    profile = 'release'  // Selects the Cargo release profile (defaults to 'debug')
}

tasks.whenTaskAdded { task ->
    // TODO: Build aw-server lib here instead of in Makefile?
    // Doesn't work, chokes on building openssl-sys
    //if ((task.name == 'javaPreCompileDebug' || task.name == 'javaPreCompileRelease')) {
    //    task.dependsOn 'cargoBuild'
    //}

    // TODO: Build aw-webui here?
    //if (task.name.contains("assembleRelease")) {
    //    task.getDependsOn().add({
    //        // add your logic here
    //    })
    //}
}
