2
4

More than 1 year has passed since last update.

【Kotlin】 LicenseToolsPluginを利用したOSSライセンス画面の実装

Posted at

初めに

Androidアプリを作成するにあたって、たくさんのライブラリを利用するかと思います。
著作権的な観点から、それらのライブラリを一覧にして表示する必要があります。

LINEやインスタでも以下のように形式は違いますが表示している画面があります。(普段は目にしませんが)

LINE Instagram

このような形式の表示方法は公式(Include open source notices)でもサポートしています。
LINEでの表示形式が公式でサポートされている手順での実装だと思います。

今回は、このOSS一覧画面をLicenseToolsPluginを使って少しリッチに表示する方法を紹介します。
https://github.com/cookpad/LicenseToolsPlugin

こちらがLicenseToolsPluginを使って表示させた際の画面です。

Pluginの導入

plugins {
+++   id "com.cookpad.android.plugin.license-tools" version "${latest_version}"
}

もちろん${latest_version}には最新のリリースバージョンを入力してください。(執筆時2021/9/29時点では1.2.8

licenses.ymlの作成

Step1
appフォルダ下にlicenses.ymlというファイルを作成します。

Step2
次に、ターミナルでプロジェクトのあるディレクトリまで移動し、以下のコマンドを実行します。

./gradlew checkLicenses

実行するとライセンス情報が出力され、BUILD FAILEDになるかと思います。
その出力されたライセンス情報をコピーします

❯ ./gradlew checkLicenses

> Configure project :app
WARNING:: DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
It will be removed in version 7.0 of the Android Gradle plugin.
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.12.0'.

> Task :app:checkLicenses FAILED
# Libraries not listed in licenses.yml:


----------  ここからコピーする -----------
- artifact: androidx.activity:activity-ktx:+
  name: activity-ktx
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://developer.android.com/jetpack/androidx/releases/activity#1.2.2
- artifact: androidx.activity:activity:+
  name: activity
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://developer.android.com/jetpack/androidx/releases/activity#1.2.4
- artifact: androidx.annotation:annotation-experimental:+
  name: annotation-experimental
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://developer.android.com/jetpack/androidx/releases/annotation#1.1.0

・
・
・
- artifact: stax:stax-api:+
  name: stax-api
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://stax.codehaus.org/
- artifact: stax:stax:+
  name: stax
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: #LICENSE#
  url: http://stax.codehaus.org/
- artifact: xpp3:xpp3:+
  name: xpp3
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: #LICENSE#

----------- ここまでをコピー ------------

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkLicenses'.
> checkLicenses: missing libraries in licenses.yml

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
1 actionable task: 1 executed

Step3
出力されたライセンス情報を貼り付けたlicenses.ymlを修正していきます。
#COPYRIGHT_HOLDER#を正しく入力していないと画面に表示できないため、これを書き換えます。

- artifact: androidx.activity:activity-ktx:+
  name: activity-ktx
  copyrightHolder: #COPYRIGHT_HOLDER# ← これ
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://developer.android.com/jetpack/androidx/releases/activity#1.2.2

例えばhiltを使っていた場合は、Googleに書き換えています。

- artifact: com.google.dagger:hilt-android:+
  name: hilt-android
  copyrightHolder: Google
  license: Apache 2.0
  licenseUrl: https://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://github.com/google/dagger

私は、Androidの標準ライブラリ(androidx.activity:activity-ktx:+とか)は表示していないのでskipを設定して表示していません。
表示しない場合は#COPYRIGHT_HOLDER#は書き換えなくても大丈夫です。

  artifact: androidx.activity:activity-ktx:+
  name: activity-ktx
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://developer.android.com/jetpack/androidx/releases/activity#1.2.2
+ skip: true

Step4
licences.ymlの書き換えが完了したら再び先ほどのコマンドを実行します。ちゃんと書き換えができていると以下のように成功するはずです。

❯ ./gradlew checkLicenses
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details

> Configure project :app
WARNING:: DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
It will be removed in version 7.0 of the Android Gradle plugin.
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.12.0'.

BUILD SUCCESSFUL in 17s
1 actionable task: 1 executed

licenses.htmlの作成

Step1
assetsフォルダを作成します。
image.png

Step2
ターミナルで以下のコマンドを実行します。

./gradlew generateLicensePage

実行に成功すると先ほど作成したassetsフォルダにlicenses.htmlが作成されます。

❯ ./gradlew generateLicensePage

> Configure project :app
WARNING:: DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
It will be removed in version 7.0 of the Android Gradle plugin.
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.12.0'.

BUILD SUCCESSFUL in 4s
1 actionable task: 1 executed

licenses.htmlをActivityから呼び出す。

これが最後の手順になります。
先ほど作成したlisenses.htmlWebViewから呼び出します。

OssLicenseActivity.kt
class OssLicenseActivity : AppCompatActivity() {
    private lateinit var binding: ActivityOssLicenseBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityOssLicenseBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.webView.loadUrl("file:///android_asset/licenses.html")
    }
}
activity_oss_licenses.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".view.settings.OssLicenseActivity">

        <WebView
            android:id="@+id/web_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

以上がLicenseToolsPluginを使ったライセンス一覧画面の実装手順になります。
標準の表示形式より、少しリッチに表示できるので試してみてください!

参考文献

license-tools-pluginを試してみた

2
4
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
4