apply plugin: 'com.android.application'

// add -Pstrict.release to the gradle command line to enable
if (project.hasProperty('strict.release')) {
    println "Running strict release"
} else {
    apply from: '../config/checkstyle/checkstyle.gradle'
    apply from: '../config/pmd/pmd.gradle'
}

/* gets the version name from the latest Git tag */
def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags', '--always'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

def fullApplicationId = "org.fdroid.fdroid"
def basicApplicationId = "org.fdroid.basic"
// yes, this actually needs both quotes https://stackoverflow.com/a/41391841
def privilegedExtensionApplicationId = '"org.fdroid.fdroid.privileged"'

android {
    compileSdkVersion 31

    defaultConfig {
        versionCode 1016052
        versionName getVersionName()

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        minSdkVersion 23
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 25
        /*
          The Android Testing Support Library collects analytics to continuously improve the testing
          experience. More specifically, it uploads a hash of the package name of the application
          under test for each invocation. If you do not wish to upload this data, you can opt-out by
          passing the following argument to the test runner: disableAnalytics "true".
         */
        testInstrumentationRunnerArguments disableAnalytics: 'true'
        vectorDrawables.useSupportLibrary = true
    }

    ext {
        APP_NAME = "@string/app_name"
        APP_NAME_DEBUG = "@string/app_name_debug"
    }

    buildTypes {
        // use proguard on debug too since we have unknowingly broken
        // release builds before.
        all {
            manifestPlaceholders = [ applicationLabel: APP_NAME ]
            minifyEnabled true
            shrinkResources true
            buildConfigField "String", "PRIVILEGED_EXTENSION_PACKAGE_NAME", privilegedExtensionApplicationId
            buildConfigField "String", "ACRA_REPORT_EMAIL", '"reports@f-droid.org"' // String needs both quotes
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'src/androidTest/proguard-rules.pro'
        }
        debug {
            getIsDefault().set(true)
            manifestPlaceholders = [ applicationLabel: APP_NAME_DEBUG ]
            applicationIdSuffix ".debug"
            resValue "string", "applicationId", fullApplicationId + applicationIdSuffix
            versionNameSuffix "-debug"
            // testProguardFiles gets partially ignored for instrumentation tests
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'src/androidTest/proguard-rules.pro'
            println 'buildTypes.debug defaultConfig.versionCode ' + defaultConfig.versionCode
        }
    }

    flavorDimensions "base"
    productFlavors {
        full {
            getIsDefault().set(true)
            dimension "base"
            applicationId fullApplicationId
            resValue "string", "applicationId", fullApplicationId
        }
        basic {
            dimension "base"
            applicationId basicApplicationId
            resValue "string", "applicationId", basicApplicationId
        }
    }

    compileOptions {
        compileOptions.encoding = "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    aaptOptions {
        cruncherEnabled = false
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            // prevent tests from dying on android.util.Log calls
            returnDefaultValues = true
            all {
                // All the usual Gradle options.
                testLogging {
                    events "skipped", "failed", "standardOut", "standardError"
                    showStandardStreams = true
                }
                systemProperty 'robolectric.dependency.repo.url', 'https://repo1.maven.org/maven2'
            }
        }
    }

    sourceSets {
        test {
            java.srcDirs += "$projectDir/src/testShared/java"
        }

        androidTest {
            java.srcDirs += "$projectDir/src/testShared/java"
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError true

        htmlReport true
        xmlReport false
        textReport false

        lintConfig file("lint.xml")
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/INDEX.LIST'
        exclude '.readme'
    }
}

dependencies {
    implementation project(":libs:download")
    implementation project(":libs:index")
    implementation project(":libs:database")
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.palette:palette:1.0.0'
    implementation 'androidx.work:work-runtime:2.7.1'
    implementation 'com.google.guava:guava:31.0-android' // somehow needed for work-runtime to function

    implementation 'com.google.android.material:material:1.6.1'

    implementation 'com.google.zxing:core:3.3.3' // newer version need minSdk 24 or library desugering
    implementation 'info.guardianproject.netcipher:netcipher:2.2.0-alpha'
    implementation 'info.guardianproject.panic:panic:1.0'
    //noinspection GradleDependency -> Commons IO > 2.5 uses java.nio.file, which requires desugaring
    implementation 'commons-io:commons-io:2.6'
    implementation 'commons-net:commons-net:3.6'
    implementation 'ch.acra:acra-mail:5.9.0'
    implementation 'ch.acra:acra-dialog:5.9.0'
    implementation 'com.hannesdorfmann:adapterdelegates4:4.3.2'
    implementation "org.slf4j:slf4j-android:1.7.36"

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.9'

    implementation "com.github.bumptech.glide:glide:4.12.0"
    annotationProcessor "com.github.bumptech.glide:compiler:4.12.0"

    implementation 'org.bouncycastle:bcprov-jdk15to18:1.71'
    fullImplementation 'org.bouncycastle:bcpkix-jdk15to18:1.71'
    fullImplementation 'org.jmdns:jmdns:3.5.5'
    fullImplementation 'org.nanohttpd:nanohttpd:2.3.1'

    testImplementation 'androidx.test:core:1.4.0'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.robolectric:robolectric:4.8.1'
    testImplementation 'org.mockito:mockito-core:3.9.0'
    testImplementation 'org.hamcrest:hamcrest:2.2'

    androidTestImplementation 'androidx.arch.core:core-testing:2.1.0'
    androidTestImplementation 'androidx.test:core:1.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:monitor:1.5.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'androidx.work:work-testing:2.7.1'
}
