LoginSignup
35
33

More than 5 years have passed since last update.

Android な Gradle のプロジェクトで Proguard のアノテーションを使う

Posted at

標準で最初から使えるようにして欲しい…

モジュール配下にlibsディレクトリを作る

Maven にないので、アノテーションの jar をlibsディレクトリに突っ込みます。
そのためのlibsディレクトリを作っておきましょう(最新の AndroidStudio なら新しいプロジェクト作ると勝手に作ってくれるはず)。

あわせて、Eclipse の時みたいにlibsへいい感じにパスを通してくれないので、ビルドスクリプトを修正します(最新の AndroidStudio なら新しいプロジェクト作ると勝手に書いておいてくれるはず)。

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

libsディレクトリに、アノテーションを含むannotations.jarを入れる

アノテーションが含まれている jar は ProGuard のなかにおいてあります。

${ANDROID_SDK_ROOT}/tools/proguard/examples/annotations

libsへコピペしてください。

もう一つ、jar と一緒に annotations.pro というファイルも有りますので、合わせて一緒に入れておきます。

proguard-rules.txtを修正する

annotations.jarを入れても、そのままではアノテーションもろとも ProGuard によって闇の彼方へ葬られてしまうので、ProGuard 関連のアノテーションが付いているものは適宜アノテーションに合わせたキープの仕方を宣言する必要があります。

そのあたりはすべて、annotations.proに記述されているので、proguard-rules.txtには、そのannotation.proも読み込んでね、ということを伝えます。


-dontshrink
-include libs/annotations.pro

このあたりの話題は、こちらの記事と同様ですね。

アノテーションを付ける

アノテーションは以下のものが有ります

@Keep
@KeepApplication
@KeepClassMemberNames
@KeepClassMembers
@KeepGettersSetters
@KeepName
@KeepPublicClassMemberNames
@KeepPublicClassMembers
@KeepPublicGettersSetters
@KeepPublicImplementations
@KeepPublicProtectedClassMemberNames
@KeepPublicProtectedClassMembers

クラスの宣言か、あるいはメンバの宣言に付けて使います。

たとえば、Gson によってシリアライズやデシリアライズを行うクラスの場合、以下のようにします。


@KeepClassMembers
public class MyBean implements Parcelable {
    // ...{snip}...
}
35
33
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
35
33