import groovy.json.JsonOutput import java.util.regex.Pattern def collectLicenses(File rootDir, File ossPluginResDir, File outDir) { def pattern = Pattern.compile("^(\\d+:\\d+) (.*)\$") // third_party_licenses file generated by oss-licenses-plugin def licenses = new File(ossPluginResDir, "third_party_licenses") // third_party_license_metadata file generated by oss-licenses-plugin def metadata = new File(ossPluginResDir, "third_party_license_metadata") // check that files exist, are readable and output directory is writable if (!licenses.canRead()) { throw new GradleException("Cannot find/read ${licenses.absolutePath}") } if (!metadata.canRead()) { throw new GradleException("Cannot find/read ${metadata.absolutePath}") } if (!outDir.exists() || !outDir.canExecute()) { if (!outDir.mkdir()) { throw new GradleException("Failed to create ${outDir.absolutePath}") } } def outFile = new File(outDir, "android.json") if (outFile.exists()) { outFile.delete() } println "Used licenses:" var licenseMap = [:] var index = 0 licenses.eachLine { line -> def indices = "$index:${line.length()}" println line licenseMap[indices] = line index += line.length() + 1 } println "Modules:" def licenseList = [] metadata.eachLine { line -> def matcher = pattern.matcher(line) if (matcher.find()) { def indices = matcher.group(1) def packageName = matcher.group(2) if (licenseMap.get(indices) == null) { throw new GradleException("Unexpected license indexes: $indices for $packageName") } println packageName + " license: ${licenseMap[indices]}" licenseList.add(PackageName: packageName, PackageLicense: licenseMap[indices]) } else { throw new GradleException("Unexpected project license line: $line") } } // add zxing_licenses which are not detected println "adding zxing licenses" licenseList.add(PackageName: "ZXing Core (3.3.0)", PackageLicense: "https://www.apache.org/licenses/LICENSE-2.0.txt") licenseList.add(PackageName: "ZXing Android Core (3.3.0)", PackageLicense: "https://www.apache.org/licenses/LICENSE-2.0.txt") outFile.write(new JsonOutput().toJson(licenseList)) println "Created ${outFile.absolutePath}" // copy license assets to flutter resources def licensesDir = new File(rootDir, "licenses/"); copy { from(licensesDir.absolutePath) { include "**/*txt" include "**/*json" } into outDir } // remove not needed oss-licenses-plugin files licenses.delete() metadata.delete() } task collectLicenses() { dependsOn(":app:releaseOssLicensesTask") doLast { def ossPluginResDir = new File(project.buildDir, "generated/third_party_licenses/release/res/raw/") collectLicenses(project.rootDir, ossPluginResDir, new File(project.rootDir.parent, "assets/licenses/raw/")) } }