apply plugin: 'com.android.application'
apply plugin: 'com.github.johnrengelman.shadow'

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
task shadowJar(type: ShadowJar) {
    configurations = [project.configurations.shadow]
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn shadowJar
}

android {
    compileSdkVersion 23
    defaultConfig {
        applicationId "com.vsmartcard.acardemulator"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 8
        versionName "3.5"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions "distribution"
    productFlavors {
        full {
            dimension "distribution"
        }
        fdroid {
            dimension "distribution"
            applicationIdSuffix ".fdroid"
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/external/jcardsim/src/main/java'
        main.java.exclude('com/licel/jcardsim/smartcardio/*')
        main.java.exclude('com/licel/jcardsim/remote/*')
        main.java.exclude('com/licel/jcardsim/utils/APDUScriptTool*')
        main.java.exclude('com/licel/jcardsim/io/JavaxSmartCardInterface*')
        main.java.exclude('com/licel/jcardsim/io/CAD*')
        main.java.exclude('javacard/framework/service/CardRemoteObject*')
        main.java.exclude('javacard/framework/service/RMIService*')
        main.java.srcDirs += 'src/main/external/GidsApplet/src'
        main.java.exclude('com/mysmartlogon/gidsAppletTests/*')
        main.java.srcDirs += 'src/main/external/IsoApplet/src'
        main.java.srcDirs += 'src/main/external/PivApplet/src'
        main.java.srcDirs += 'src/main/external/android-scio/src/main'
        main.java.srcDirs += 'src/main/external/ykneo-openpgp/applet/src'
        main.java.srcDirs += 'src/main/external/ykneo-oath/applet/src'
    }
    shadowJar {
        relocate 'org.bouncycastle', 'com.vsmartcard.acardemulator.org.bouncycastle'
    }
}

android.applicationVariants.all { variant ->
    def appName
    //Check if an applicationName property is supplied; if not use the name of the parent project.
    if (project.hasProperty("applicationName")) {
        appName = applicationName
    } else {
        appName = parent.name
    }

    variant.outputs.each { output ->
        def newApkName
        //If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
        if (output.zipAlign) {
            newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
        } else {
            newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
        }
        output.outputFileName = new File(newApkName)
    }
}

dependencies {
    shadow localGroovy()
    shadow gradleApi()

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:23.0.1'
    implementation 'com.android.support:support-v4:23.0.1'
    implementation 'com.android.support:design:23.2.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    implementation 'com.google.zxing:core:3.2.1'
    implementation 'org.bouncycastle:bcprov-jdk14:1.46'
    fullImplementation files('libs/sdk-v1.0.0.jar')
}
