def generateFdroidYamls = project.tasks.register('generateFdroidYamls') { task ->
    task.description = 'Generates a file with the YAML entry for publishing an APK fo a file'
    task.group = 'publish'
    doLast {
        //must create this folder. So out CI upload-artifacts will not fail on non-existing folder
        rootProject.file('outputs/fdroid').mkdirs()
        task.dependsOn.forEach { Task dependedTask ->
            def taskProject = dependedTask.project
            File outputYamlFile = rootProject.file("outputs/fdroid/${taskProject.android.defaultConfig.applicationId}.yaml")
            outputYamlFile.write("")
            println("Will write fdroid data to ${outputYamlFile.absolutePath}")
            def commit = 'git rev-parse HEAD'.execute().text.trim()

            def lines = new ArrayList<String>()
            lines.add("  - versionName: ${taskProject.android.defaultConfig.versionName}")
            lines.add("    versionCode: ${taskProject.android.defaultConfig.versionCode}")
            lines.add("    commit: ${commit}")
            lines.add("    subdir: ${taskProject.path.substring(1).replace(':', '/')}")
            lines.add("    sudo:")
            lines.add("      - apt-get update || apt-get update")
            lines.add("      - apt-get install -t stretch-backports openjdk-11-jdk-headless openjdk-11-jre-headless")
            lines.add("      - update-alternatives --auto java")
            lines.add("    gradle:")
            lines.add("      - yes")
            lines.add("    output: ${"../".repeat(taskProject.path.count(':'))}outputs/apks/release/${taskProject.path.substring(1).replace(':', '-')}-*.apk")
            lines.add("    prebuild:")
            lines.add("      - JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 \$\$SDK\$\$/tools/bin/sdkmanager 'build-tools;31.0.0'")
            lines.add("        'build-tools;30.0.3' 'platforms;android-31' > /dev/null")
            lines.add("      - cp \$\$SDK\$\$/build-tools/31.0.0/lib/d8.jar \$\$SDK\$\$/build-tools/31.0.0/lib/dx.jar")
            lines.add("      - cp \$\$SDK\$\$/build-tools/31.0.0/d8 \$\$SDK\$\$/build-tools/31.0.0/dx")
            lines.add("    scanignore:")
            lines.add("      - addons/languages/*/pack/dictionary/*.gz")
            if (taskProject.path.contains('ime:app')) {
                lines.add("    ndk: r23")
            }
            lines.add("    gradleprops:")
            lines.add("      - forceVersionBuildCount=${System.getenv('BUILD_COUNT_FOR_VERSION')}")
            lines.add("")
            lines.add("AutoUpdateMode: None")
            lines.add("UpdateCheckMode: RepoManifest")
            lines.add("CurrentVersion: ${taskProject.android.defaultConfig.versionName}")
            lines.add("CurrentVersionCode: ${taskProject.android.defaultConfig.versionCode}")

            lines.forEach { line ->
                outputYamlFile.append(line)
                outputYamlFile.append('\n')
            }
        }
    }
}

subprojects {
    tasks.configureEach { newTask ->
        if (newTask.name == 'assembleRelease') {
            if (newTask.project.plugins.hasPlugin('com.android.application')) {
                newTask.finalizedBy(generateFdroidYamls)
            }
        }
    }
}
