LoginSignup
21
30

More than 5 years have passed since last update.

よくある?ひっかかった Lint チェックの解決方法

Last updated at Posted at 2015-12-18

はじめに

コーディング中に Android Studio 上で出てくる警告はちゃんと気にして潰しているのですが、
Lint チェックし忘れて
「コマンドラインからビルドすると警告出るんですけど…」
という問い合わせがあったりします。

本当にすみません。

そんな自戒の意味を込めて、今まで解決した項目の解決方法メモ。

やりたいこと

  • ここでメモることで Lint の存在を思い出す
  • 反省する

備忘録

ビルドするコマンド

cd ${プロジェクトのフォルダ}
./gradlew build

Lint だけするコマンド

cd ${プロジェクトのフォルダ}
./gradlew ${チェックするモジュール名}:lint

Lint の結果ファイルの場所

${プロジェクトのフォルダ}/${モジュール名}/build/outputs/lint-results.html

InflateParams: Layout Inflation without a Parent

フラグメントとかビューとかをインフレートすると出るアレ。

こういうのはコード的に問題ないけど、Lint 的に NG。

LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(resource, null, false);

第三引数の attachToRoot が false の時は、
第二引数の root って実質不要なんだけど、こうすると怒られない。

LayoutInflater inflater = LayoutInflater.from(getActivity());
// ちゃんとルートビューグループを指定する
View view = inflater.inflate(resource, root, false);

GradleDependency: Obsolete Gradle Dependency

うっかり古いビルドツール指定すると出るアレ。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    /// 〜〜〜省略〜〜〜
}

特に理由がなければ、最新のやつを指定しよう。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    /// 〜〜〜省略〜〜〜
}

理由がある場合は、コメントしつつ無効化しよう。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    /// 〜〜〜省略〜〜〜

    lintOptions {
        // どうしても23.0.1 を使いたいので、無効化
        disable 'GradleDependency'
    }
}

WrongConstant: Incorrect constant

なんかダメな指定をした時に出るアレ。

自分の場合は、ビューの表示/非表示切り替えで
非表示時に「View.GONE」にするか「View.INVISIBLE」にするか迷ったので、
あとで修正しやすいようにこう書いていた。

private static final int VISIBILITY_HIDE = View.GONE;

// 〜〜〜省略〜〜〜

view.setVisibility(VISIBILITY_HIDE);

そうすると、こんな感じで怒られる。

${ソースコードのファイル}:${問題箇所の行数}:
Must be one of: View.VISIBLE, View.INVISIBLE, View.GONE

ここでは記載してないけど、ちゃんと問題箇所のコードもちょっと表示してくれる。

要は、指定の定数以外を設定できるからダメ!って言ってる。
というわけで、ちゃんと指定の定数で書く。

view.setVisibility(View.GONE);

このチェックは、重要度が「エラー」なので、ひっかかるとコマンドからビルドできない。
Android Studio 上でビルドできるからといって、油断してはいけない。(戒め)

UnusedResources: Unused resources

不要な文字列リソースとかあると出てくるアレ。

ただの消し忘れです。本当にすみません…。
ここで出てきたリソースは、ちゃんと消しときましょう。

GoogleAppIndexingWarning: Missing support for Google App Indexing

いわゆる、ディープリンクの対応がされてないよって警告のアレ。

不要な時は、無効化しましょう。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    /// 〜〜〜省略〜〜〜

    lintOptions {
        // ディープリンクは利用しないので、無効化
        disable 'GoogleAppIndexingWarning'
    }
}

ちなみに、無効化は複数指定できます。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    /// 〜〜〜省略〜〜〜

    lintOptions {
        // どうしても23.0.1 を使いたいので、無効化
        // ディープリンクは利用しないので、無効化
        disable 'GradleDependency', 'GoogleAppIndexingWarning'
    }
}

RtlHardcoded: Using left/right instead of start/end attributes

レイアウトファイルで古い属性の paddingLeft とか layout_marginRight とか使ってると出てくるアレ。

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/view"
    android:layout_alignRight="@+id/relative_parent"/>

コードの例だと、layout_alignRight を使っているけど、
要は「〜Left」や「〜Right」だとひっかかります。

で、単純に新しいのに置き換えると、こうなる。

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/view"
    android:layout_alignEnd="@+id/relative_parent"/>

「〜Start」や「〜End」にすればよい。

とはいえ、これらは API 17 から追加されたものなので、
それ以前をサポートするやつだと、落ちる。

じゃあ、どうすればいいのかというと、両方書く。

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/view"
    android:layout_alignRight="@+id/relative_parent"
    android:layout_alignEnd="@+id/relative_parent"/>

なぜ、そうすべきかも lint−results.html に書いてある。

Using Gravity#LEFT and Gravity#RIGHT can lead to problems when a layout is rendered in locales where text
 flows from right to left. Use Gravity#START and Gravity#END instead. Similarly, in XML gravity and 
layout_gravity attributes, use start rather than left.

For XML attributes such as paddingLeft and layout_marginLeft, use paddingStart and layout_marginStart.
 NOTE: If your minSdkVersion is less than 17, you should add both the older left/right attributes as well
 as the new start/right attributes. On older platforms, where RTL is not supported and the start/right 
attributes are unknown and therefore ignored, you need the older left/right attributes. There is a 
separate lint check which catches that type of error.

(Note: For Gravity#LEFT and Gravity#START, you can use these constants even when targeting older 
platforms, because the start bitmask is a superset of the left bitmask. Therefore, you can use 
gravity="start" rather than gravity="left|start".)

なるほどなー。

AllowBackup: AllowBackup/FullBackupContent Problems

バックアップの設定をデフォルトのままにしとくと出るアレ。

<application
    android:allowBackup="true"
    ...>

    ...

</application>

別にバックアップ使わないときは false を設定しておこう。

<application
    android:allowBackup="false"
    ...>

    ...

</application>

この場合、もしライブラリ側が allowBackup を true にしているとビルドできないので、
そういうときは、tools:replace 属性も追加しよう。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ...>

    <application
        android:allowBackup="false"
        tools:replace="android:allowBackup"
        ...>

        ...

    </application>

</manifest>

UselessParent: Useless parent layout

親レイアウトが意味ない感じになっていると出てくるアレ。

こういうやつね。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

        ...

    </RelativeLayout>

</RelativeLayout>

ちゃんと意味のある親にしよう。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    ...


</RelativeLayout>

InvalidPackage: Package not included in Android

主にライブラリのせいで出てくるアレ。

自分のときは、「picasso2-okhttp3-downloader」を
使ってたときに出たので、以下のように回避した。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    /// 〜〜〜省略〜〜〜

    compile ('com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2') {
        exclude group: 'com.squareup.okio', module: 'okio'
    }
}

ライブラリによって対応方法が違うので、
「どの jar がダメなのか」のエラー文でググるのがいいと思う。

すべて通る

と、こうなる。

スクリーンショット 2015-12-18 14.00.37.png

おめでとう!ありがとう!

すべて無効化する

Lint チェック(゚⊿゚)イラネ って場合は、こう書くと Lint 自体を無効化できる。

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    /// 〜〜〜省略〜〜〜

    lintOptions {
        // エラーとか完全無視する
        abortOnError false
    }
}

とはいえ、何も考えずに Android Studio 使っていると見逃すエラーもあるので、
個人的にはやらないほうがいいと思う。

まとめ

  • Lint ダイジ、ゼッタイ!
  • Android Studio だけじゃ見つからないエラーも見つかるよ!
  • 他に見つけたら、追記するよ!

参考

21
30
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
21
30