# HG changeset patch # User Da Risk # Date 1741823398 14400 # Node ID f07de07b90c4bc9ec94415d7204229b26b197ce0 # Parent 5bba9369df0f80ac7ecd4656fde712ccb82688d3 core: port OssLicenseParser to multiplatform diff -r 5bba9369df0f -r f07de07b90c4 core/build.gradle.kts --- a/core/build.gradle.kts Wed Mar 12 19:00:20 2025 -0400 +++ b/core/build.gradle.kts Wed Mar 12 19:49:58 2025 -0400 @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + /* * AboutOss is an utility library to retrieve and display * opensource licenses in Android applications. @@ -21,7 +23,7 @@ */ plugins { id("com.android.library") - kotlin("android") + kotlin("multiplatform") id("com.geekorum.build.source-license-checker") `maven-publish` } @@ -29,6 +31,34 @@ group = "com.geekorum.aboutoss" version = "0.1.0" +kotlin { + androidTarget { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_17) + } + } + + jvm("desktop") + + listOf( + iosX64(), + iosArm64(), + iosSimulatorArm64(), + ).forEach { iosTarget -> + iosTarget.binaries.framework { + baseName = "aboutoss-core" + isStatic = true + } + } + + sourceSets { + commonMain.dependencies { + implementation(libs.okio) + implementation(libs.kotlinx.coroutines) + } + } +} + android { namespace = "com.geekorum.aboutoss.core" compileSdk = 35 @@ -53,11 +83,8 @@ } } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 - } - kotlinOptions { - jvmTarget = "1.8" + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } publishing { @@ -69,8 +96,6 @@ } dependencies { - implementation(libs.okio) - implementation(libs.kotlinx.coroutines) testImplementation(libs.junit) androidTestImplementation(libs.androidx.test.ext.junit) diff -r 5bba9369df0f -r f07de07b90c4 core/src/commonMain/kotlin/gms/OssLicenseParser.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/commonMain/kotlin/gms/OssLicenseParser.kt Wed Mar 12 19:49:58 2025 -0400 @@ -0,0 +1,105 @@ +/* + * AboutOss is an utility library to retrieve and display + * opensource licenses in Android applications. + * + * Copyright (C) 2023 by Frederic-Charles Barthelery. + * + * This file is part of AboutOss. + * + * AboutOss is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AboutOss is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AboutOss. If not, see . + */ +package com.geekorum.aboutoss.core.gms + +import okio.ByteString +import okio.Source +import okio.buffer +import okio.use + +/** + * Parse licences data generated by the "com.google.android.gms.oss-licenses-plugin" gradle plugin. + */ +class OssLicenseParser { + + /** + * Parse licenses + * @param [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file + * @param [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file + */ + fun parseLicenses( + thirdPartyLicensesInput: Source, thirdPartyLicensesMetadataInput: Source + ): Map { + val licenses = readLicensesFile(thirdPartyLicensesInput) + return buildLicenseInfo(licenses, thirdPartyLicensesMetadataInput) + } + + private fun readLicensesFile(thirdPartyLicensesInput: Source): ByteString { + return thirdPartyLicensesInput.buffer().use { + it.readByteString() + } + } + + private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: Source): Map { + return thirdPartyLicensesMetadataInput.buffer().use { + buildMap { + while (true) { + val line = it.readUtf8Line() ?: break + if (line.isNotBlank()) { + with(line.toLineParser()) { + val start = readStartIdx() + val length = readLength() + val dependency = readName() + val licenseTxt = license.substring( + beginIndex = start, + endIndex = start + length + 1 + ).utf8() + put(dependency, licenseTxt) + } + } + } + } + } + } + + companion object +} + +private class LicenseMetadataLineParser( + private val line: String +) { + + private var idx = 0 + + fun readStartIdx(): Int { + val end = line.indexOf(':', startIndex = idx) + val result = line.substring(idx, end).toInt() + idx = end + 1 + return result + } + + fun readLength(): Int { + val end = line.indexOf(' ', startIndex = idx) + val result = line.substring(idx, end).toInt() + idx = end + 1 + return result + } + + fun readName(): String { + val result = line.substring(idx) + idx = line.length + 1 + return result + } + +} + +private fun String.toLineParser() = LicenseMetadataLineParser(this) \ No newline at end of file diff -r 5bba9369df0f -r f07de07b90c4 core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt --- a/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt Wed Mar 12 19:00:20 2025 -0400 +++ b/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt Wed Mar 12 19:49:58 2025 -0400 @@ -22,8 +22,11 @@ package com.geekorum.aboutoss.core import android.content.Context +import com.geekorum.aboutoss.core.gms.OssLicenseParser +import com.geekorum.aboutoss.core.gms.openRawResourcesByName import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.withContext +import okio.source /** * Retrieve License information stored in application resources @@ -58,8 +61,8 @@ .use { licensesMetadataInput -> val parser = OssLicenseParser() parser.parseLicenses( - thirdPartyLicensesInput = licensesInput, - thirdPartyLicensesMetadataInput = licensesMetadataInput + thirdPartyLicensesInput = licensesInput.source(), + thirdPartyLicensesMetadataInput = licensesMetadataInput.source() ) } } diff -r 5bba9369df0f -r f07de07b90c4 core/src/main/java/com/geekorum/aboutoss/core/OssLicenseParser.kt --- a/core/src/main/java/com/geekorum/aboutoss/core/OssLicenseParser.kt Wed Mar 12 19:00:20 2025 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,128 +0,0 @@ -/* - * AboutOss is an utility library to retrieve and display - * opensource licenses in Android applications. - * - * Copyright (C) 2023 by Frederic-Charles Barthelery. - * - * This file is part of AboutOss. - * - * AboutOss is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * AboutOss is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with AboutOss. If not, see . - */ -package com.geekorum.aboutoss.core - -import android.annotation.SuppressLint -import android.content.Context -import okio.ByteString -import okio.buffer -import okio.source -import java.io.InputStream - -/** - * Parse licences data generated by the "com.google.android.gms.oss-licenses-plugin" gradle plugin. - */ -class OssLicenseParser { - - /** - * Parse licenses - * @param [thirdPartyLicensesInput] is usually res/raw/third_party_licenses file - * @param [thirdPartyLicensesMetadataInput] is usually res/raw/third_party_license_metadata file - */ - fun parseLicenses( - thirdPartyLicensesInput: InputStream, thirdPartyLicensesMetadataInput: InputStream - ): Map { - val licenses = readLicensesFile(thirdPartyLicensesInput) - return buildLicenseInfo(licenses, thirdPartyLicensesMetadataInput) - } - - private fun readLicensesFile(thirdPartyLicensesInput: InputStream): ByteString { - return thirdPartyLicensesInput.source().use { source -> - source.buffer().use { - it.readByteString() - } - } - } - - private fun buildLicenseInfo(license: ByteString, thirdPartyLicensesMetadataInput: InputStream): Map { - return thirdPartyLicensesMetadataInput.source().use { source -> - source.buffer().use { - buildMap { - while (true) { - val line = it.readUtf8Line() ?: break - if (line.isNotBlank()) { - with(line.toLineParser()) { - val start = readStartIdx() - val length = readLength() - val dependency = readName() - val licenseTxt = license.substring( - beginIndex = start, - endIndex = start + length + 1 - ).string(Charsets.UTF_8) - put(dependency, licenseTxt) - } - } - } - } - } - } - } - - companion object { - @SuppressLint("DiscouragedApi") - fun openDefaultThirdPartyLicenses(context: Context): InputStream { - return openRawResourcesByName(context, "third_party_licenses") - } - - @SuppressLint("DiscouragedApi") - fun openDefaultThirdPartyLicensesMetadata(context: Context): InputStream { - return openRawResourcesByName(context, "third_party_license_metadata") - } - - @SuppressLint("DiscouragedApi") - fun openRawResourcesByName(context: Context, name: String): InputStream { - val resourceId = context.resources.getIdentifier(name, "raw", context.packageName) - check(resourceId != 0) { "$name was not found in resources raw of ${context.packageName}"} - return context.resources.openRawResource(resourceId) - } - } -} - -private class LicenseMetadataLineParser( - private val line: String -) { - - private var idx = 0 - - fun readStartIdx(): Int { - val end = line.indexOf(':', startIndex = idx) - val result = line.substring(idx, end).toInt() - idx = end + 1 - return result - } - - fun readLength(): Int { - val end = line.indexOf(' ', startIndex = idx) - val result = line.substring(idx, end).toInt() - idx = end + 1 - return result - } - - fun readName(): String { - val result = line.substring(idx) - idx = line.length + 1 - return result - } - -} - -private fun String.toLineParser() = LicenseMetadataLineParser(this) \ No newline at end of file diff -r 5bba9369df0f -r f07de07b90c4 core/src/main/java/com/geekorum/aboutoss/core/gms/OssLicenseParserExt.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/main/java/com/geekorum/aboutoss/core/gms/OssLicenseParserExt.kt Wed Mar 12 19:49:58 2025 -0400 @@ -0,0 +1,43 @@ +/* + * AboutOss is an utility library to retrieve and display + * opensource licenses in Android applications. + * + * Copyright (C) 2023 by Frederic-Charles Barthelery. + * + * This file is part of AboutOss. + * + * AboutOss is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AboutOss is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AboutOss. If not, see . + */ +package com.geekorum.aboutoss.core.gms + +import android.annotation.SuppressLint +import android.content.Context +import java.io.InputStream + +@SuppressLint("DiscouragedApi") +fun OssLicenseParser.Companion.openDefaultThirdPartyLicenses(context: Context): InputStream { + return openRawResourcesByName(context, "third_party_licenses") +} + +@SuppressLint("DiscouragedApi") +fun OssLicenseParser.Companion.openDefaultThirdPartyLicensesMetadata(context: Context): InputStream { + return openRawResourcesByName(context, "third_party_license_metadata") +} + +@SuppressLint("DiscouragedApi") +fun OssLicenseParser.Companion.openRawResourcesByName(context: Context, name: String): InputStream { + val resourceId = context.resources.getIdentifier(name, "raw", context.packageName) + check(resourceId != 0) { "$name was not found in resources raw of ${context.packageName}"} + return context.resources.openRawResource(resourceId) +}