license-tools-pluginを試してみた

アプリ内にライセンス情報を表示したかったのでcookpadが提供しているlicense-tools-pluginを試してみました。

ライブラリの導入

build.gradle(project:)

以下のように変更を行います。

buildscript {
    ext.kotlin_version = '1.3.11'
    ...省略
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //追加
        classpath 'com.cookpad.android.licensetools:license-tools-plugin:1.7.0' 
    }
}
...省略

build.gradle(Module: app)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
//追加
apply plugin: 'com.cookpad.android.licensetools'

android {
    ...省略

    //JDK8以上が必須のようなので追加(Android Studio 3.3.2以上であればあらかじめ用意されている)
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    ...省略
  
    //追加
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //ライセンス表記出すためにRxJavaを入れておきました。使用するライブラリに置き換えてください
    implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
}

Sync Nowボタンを押してエラーが出なければ成功です。

ライセンス情報を追加

ライセンス情報の出力

Android Studioの右端のtabにあるGradle > プロジェクト名 > :app > Tasks > verification > checkLicensesをダブルクリックで実行します。

実行すると以下のようにコンソールにライセンス情報が出力されます。BUILD FAILED となっていますが問題ありません

-artifact:からurlまでの塊を全てコピーします

> Task :app:checkLicenses FAILED
# Libraries not listed in licenses.yml:
- artifact: com.android.support:animated-vector-drawable:+
  name: Android Support AnimatedVectorDrawable
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://developer.android.com/tools/extras/support-library.html
- artifact: com.android.support:cursoradapter:+
  name: Android Support Library Cursor Adapter
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://developer.android.com/tools/extras/support-library.html
- artifact: org.jetbrains.kotlin:kotlin-stdlib-common:+
  name: org.jetbrains.kotlin:kotlin-stdlib-common
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: https://kotlinlang.org/
- artifact: android.arch.lifecycle:livedata:+
  name: Android Lifecycle LiveData
  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/topic/libraries/architecture/index.html


...省略


- artifact: com.android.support:support-compat:+
  name: Android Support Library compat
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://developer.android.com/tools/extras/support-library.html
- artifact: com.android.support.constraint:constraint-layout-solver:+
  name: Android ConstraintLayout Solver
  copyrightHolder: #COPYRIGHT_HOLDER#
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://tools.android.com

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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 0s
1 actionable task: 1 executed
19:13:49: Task execution finished 'checkLicenses'.

licenses.ymlの作成

appフォルダ下にlicenses.ymlというファイルをを作成し、コピーしたデータを貼り付けます。

copyrightHolderの記述

checkLicenseで出力されたものをそのままlicenses.ymlに貼り付けただけではだめで、copyrightHolderの部分に正しい名前を記述してあげる必要があります。

- artifact: com.android.support:animated-vector-drawable:+
  name: Android Support AnimatedVectorDrawable
  copyrightHolder: #COPYRIGHT_HOLDER#     ⬅︎ここ!!!!!
  license: The Apache Software License, Version 2.0
  licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.txt
  url: http://developer.android.com/tools/extras/support-library.html

これがめちゃくちゃめんどくさいですが、記述していないと表示することができません。

Androidの標準ライブラリはThe Android Open Source Projectと記述してあげれば良いようです。

問題ないかの確認

ライセンス情報出力でやったGradle > プロジェクト名 > :app > Tasks > verification > checkLicensesをもう一度ダブルクリックします。

以下のようなログが出力されていれば成功です。

19:22:58: Executing task 'checkLicenses'...

Executing tasks: [checkLicenses]

> Task :app:checkLicenses

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
19:22:59: Task execution finished 'checkLicenses'.

Htmlの書き出し

assetフォルダの作成

File > New > Folder > Assets Folderでhtml保存用のフォルダを作成します。

htmlファイル出力

Android Studioの右端のtabにあるGradle > プロジェクト名 > :app > Tasks > other > generateLicensePage をダブルクリックします。

成功している場合assetsフォルダにhtmlが出力されているはずです。

WebViewで表示

asset内にhtmlが自動生成されているので以下のように呼び出せば表示されます

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        web_view.loadUrl("file:///android_asset/licenses.html")
    }
}

スクリーンショット

ここまでの手順で問題なければ以下のように表示されていると思います。スクロールしてライブラリの情報を確認することができます。

参考文献

オープンソースライセンスの管理を楽にする -Android アプリ編(公式 チュートリアル)

あの素晴らしいlicense-tools-pluginをもう一度

Android StudioでAssetsフォルダを簡単に作成する方法について