ext {
    junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
    androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
    androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
}

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.1'
    }
}

apply plugin: 'com.android.library'

android {
    namespace "com.microsoft.capacitor.codepush"
    compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
        targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
    google()
    jcenter()
    mavenCentral()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation 'com.nimbusds:nimbus-jose-jwt:5.1'
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
}

task capCreateAssetManifest {
    def inAssetsDir = rootProject.file("app/src/main/assets")

    def outAssetsDir = inAssetsDir
    def outFile = new File(outAssetsDir, "cdvasset.manifest")

    doLast {
        def contents = new HashMap()
        def sizes = new HashMap()
        contents[""] = inAssetsDir.list()
        def tree = fileTree(dir: inAssetsDir)
        tree.visit { fileDetails ->
            if (fileDetails.isDirectory()) {
                contents[fileDetails.relativePath.toString()] = fileDetails.file.list()
            } else {
                sizes[fileDetails.relativePath.toString()] = fileDetails.file.length()
            }
        }

        outAssetsDir.mkdirs()
        outFile.withObjectOutputStream { oos ->
            oos.writeObject(contents)
            oos.writeObject(sizes)
        }
    }
}

android.buildTypes.each {
  // to prevent incorrect long value restoration from strings.xml we need to wrap it with double quotes
  // https://github.com/Microsoft/cordova-plugin-code-push/issues/264
  it.resValue "string", "CODE_PUSH_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
}

preBuild.dependsOn capCreateAssetManifest
