copy license files to flutter assets during build

This commit is contained in:
Adam Velebil 2022-09-01 17:42:59 +02:00
parent 30b25ccadb
commit d5beca16fc
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
10 changed files with 42 additions and 14 deletions

View File

@ -2,7 +2,7 @@ import groovy.json.JsonOutput
import java.util.regex.Pattern
def collectLicenses(File ossPluginResDir, File outDir) {
def collectLicenses(File rootDir, File ossPluginResDir, File outDir) {
def pattern = Pattern.compile("^(\\d+:\\d+) (.*)\$")
@ -21,11 +21,13 @@ def collectLicenses(File ossPluginResDir, File outDir) {
throw new GradleException("Cannot find/read ${metadata.absolutePath}")
}
if (!outDir.isDirectory() || !outDir.canExecute()) {
throw new GradleException("Insufficient permissions to ${outDir.absolutePath}")
if (!outDir.exists() || !outDir.canExecute()) {
if (!outDir.mkdir()) {
throw new GradleException("Failed to create ${outDir.absolutePath}")
}
}
def outFile = new File(outDir, "android_licenses.json")
def outFile = new File(outDir, "android.json")
if (outFile.exists()) {
outFile.delete()
}
@ -35,10 +37,12 @@ def collectLicenses(File ossPluginResDir, File outDir) {
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)
@ -56,17 +60,32 @@ def collectLicenses(File ossPluginResDir, File outDir) {
}
// 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 inputDir = new File(project.buildDir, "generated/third_party_licenses/release/res/raw/")
collectLicenses(inputDir, new File(project.rootDir.parent, "assets/licenses/"))
def ossPluginResDir = new File(project.buildDir, "generated/third_party_licenses/release/res/raw/")
collectLicenses(project.rootDir, ossPluginResDir, new File(project.rootDir.parent, "assets/licenses/android/"))
}
}

View File

@ -1,2 +1,2 @@
helper.json
android_licenses.json
android/*

View File

@ -91,21 +91,29 @@ class DismissKeyboard extends StatelessWidget {
}
void _initLicenses() async {
const licenseDir = 'assets/licenses/android';
final androidProjectsToLicenseUrl = await rootBundle.loadStructuredData<List>(
'assets/licenses/android_licenses.json',
'$licenseDir/android.json',
(value) async => jsonDecode(value),
);
// mapping from url to license text
final urlToFile = await rootBundle.loadStructuredData<Map>(
'assets/licenses/license_indices.json',
(value) async => jsonDecode(value),
final fileMap = await rootBundle.loadStructuredData<Map>(
'$licenseDir/map.json',
(value) async => jsonDecode(value),
);
final urlToLicense = <String, String>{};
urlToFile.forEach((url, file) async {
final licenseText = await rootBundle.loadString('assets/licenses/$file');
urlToLicense[url] = licenseText;
fileMap.forEach((url, file) async {
String licenseText = url;
try {
licenseText = await rootBundle.loadString('$licenseDir/$file');
urlToLicense[url] = licenseText;
} catch (_) {
// failed to read license file, will use the url
}
});
if (androidProjectsToLicenseUrl.isNotEmpty) {

View File

@ -91,6 +91,7 @@ flutter:
- assets/product-images/
- assets/graphics/
- assets/licenses/
- assets/licenses/android/
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.