2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】oss-licenses-plugin に手動でライセンスを追加する

Posted at

2023年6月現在のやり方です。
oss-licenses-plugin に自分でライセンスを追加するには以下のようになります。

oss-licenses-pluginにライセンスを手動で追加する を参考にしています。

build.gradle.kts(:app)
project.android.applicationVariants.all {
    tasks.findByName("${name}OssLicensesTask")?.doLast {
        addOssLicense(
            project,
            "Google-Service",
            "https://developer.android.com/studio/terms.html"
        )
    }
}

fun addOssLicense(project: Project, libName: String, licenseContent: String) {
    val UTF_8 = "UTF-8"
    val LINE_SEPARATOR = System.getProperty("line.separator").toByteArray(Charsets.UTF_8)

    project.android.applicationVariants.all {
        if (project.file("build/generated/third_party_licenses/$name").exists()) {
            val dependencyOutput = project.file("build/generated/third_party_licenses/$name")

            val resourceOutput = File(dependencyOutput, "res")
            val outputDir = File(resourceOutput, "raw")

            // ライセンスファイル
            val licensesFile = File(outputDir, "third_party_licenses")
            // ライセンスファイルへの書き込み前に現在の位置を保持
            val start = licensesFile.length()

            // ライセンスファイルへ書き込み
            licensesFile.appendText(licenseContent)
            licensesFile.appendBytes(LINE_SEPARATOR)

            // ライセンスメタデータファイルに書き込み
            val licensesMetadataFile = File(outputDir, "third_party_license_metadata")
            licensesMetadataFile.appendText("${start}:${licenseContent.length} $libName")
            licensesMetadataFile.appendBytes(LINE_SEPARATOR)
        }
    }
}

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?