LoginSignup
50
48

More than 5 years have passed since last update.

使ったProGuardまとめ

Last updated at Posted at 2013-11-12

自分用プロガード設定ファイルから使えそうな部分を

Android開発の工程でたいしたことないはずなのに一番嫌いな作業はプロガード設定ファイルを書くこと。
なんかなかなか自分が意図したように出来ないし、大規模なプログラムだといろんな要素が干渉し合い何時間も食われることも。
正直自分もイマイチ理解出来てないので突っ込みどころが満載かも知れませんが、血と汗と涙のテキストです。容赦なく突っ込んでくれたら勉強になります。

#-keepattributes *Annotation*, Signature, Exceptions, InnerClass

# Javascript interface
-keepattributes JavascriptInterface
-keepclasseswithmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

最初の一文はよくありがちですが、何でもかんでもキープすると難読化の意味が無いのでコメントアウト。
Javascriptインターフェイスはいまやアノテーション必須なので、こうしておけば良い。
なぜかひとつずつ関数名を指定するやり方がStackOverFlowをはじめ横行しているが、なぜかこの書き方をしているサイトは当初見当たらなかった。
昔はアノテーション必須じゃなかったからかな?

# Native interface
# Keep names - Native method names. Keep all native class/method names.
-keepclasseswithmembers class * {
    native <methods>;
}

これ要らないかも。SDKに含まれてるほうの設定ファイルに書いてあるかも。Androidの公式ドキュメントに書いてある通りにパスを通すとSDKに含まれるプロガード設定ファイルも合わせて利用する流れになるので、そちらのほうも読んでみてください。

# Billing system
-keep,allowshrinking public class com.android.vending.billing.*

グーグルプレイのアプリ内課金を使うとき。v3のドキュメントに従って記述しました。

# twitter4j (3rd party library for twitter)
-libraryjars libs/twitter4j-core-3.0.3.jar
-dontwarn twitter4j.management.**
-dontwarn twitter4j.TwitterAPIMonitor
-dontwarn twitter4j.internal.**
-dontwarn twitter4j.Annotation
-keep class twitter4j.** { *; }

# facebook4j (3rd party library for facebook)
-libraryjars libs/facebook4j-core-1.1.11.jar
-dontwarn facebook4j.management.**
-dontwarn facebook4j.FacebookAPIMonitor
-dontwarn facebook4j.internal.**
-dontwarn facebook4j.Annotation
-keep class facebook4j.** { *; }

TwitterとFacebookはこれらのライブラリを使うと楽。バージョン名など適当に書き換えてください。
Facebookは公式じゃなくて上記のライブラリを使うとTwitter版のを元に作られているので
クリソツなコーディングでいける。両方ともサードパーティ。現在Twitterは公式のAndroid向けライブラリは存在しない。

# gson (library for Json by Google)
-libraryjars libs/gson-2.2.4.jar
-keep class com.google.gson.** { *; }
-keep class com.google.gson.stream.** { *; }
-keep class sun.misc.Unsafe { *; }
-keepattributes Expose
-keepattributes SerializedName
-keepattributes Since
-keepattributes Until
-keepclasseswithmembers class * { @com.google.gson.annotations.Expose <fields>; }

Gson使う時も試行錯誤した結果、上記でいけました。
アノテーションでJsonオブジェクトとして作ったクラスのクラス名を保護しているので明示的に@Exposeをつけて保護すればいい。

# ActiveAndroid (library for SQLite)
-libraryjars libs/activeandroid-3.1.jar
-keep class com.activeandroid.** { *; }
-keep class com.activeandroid.**.** { *; }
-keep class * extends com.activeandroid.Model
-keepattributes Column
-keepattributes Table
-keepclasseswithmembers class * { @com.activeandroid.annotation.Column <fields>; }

このライブラリもGsonと同じでクラス名やメンバ変数名などを利用しているのでデリケート。上記でいけました。

# AdMob ads SDK
-libraryjars libs/GoogleAdMobAdsSdk-6.2.1.jar
-dontwarn com.google.ads.**

アドモブ用。要らないかも。うまく動かなかったときに書いてみてください。

# Remove debugging - Android LogCat
-assumenosideeffects class android.util.Log {
    public static *** v(...);
    public static *** d(...);
    public static *** i(...);
    public static *** w(...);
    public static *** e(...);
}

これしておけば普通にログ用のコード書いて残しておいても差し障りありません。
普段コーディングで機密な情報も確認のためにガンガンログ吐いておいて大丈夫。残しておいても大丈夫。上記の設定で全部Androidのログキャット出力のコードを取り除いてくれる。でも確認のためリリースビルドしてからほんとにログが出てないか確かめてみてください。なんかあっても自己責任でたのんます。

ところでStackOverFlowやいくつかのブログで次のようなコードを進めるサイトがあります。

-assumenosideeffects class android.util.Log { <methods>; }

これはやめといたほうがいいです。
http://qiita.com/GeneralD@github/items/759840150996de230c37
に詳細は書いておきました。

# Remove debugging - All Log4j API calls. Remove all invocations of the Log4j API whose return values are not used.
-assumenosideeffects public class org.apache.log4j.** { <methods>; }

# Remove debugging - All logging API calls. Remove all invocations of the logging API whose return values are not used.
# DON'T commentout!! AdMob doesn't work!! (always network error)
#-assumenosideeffects public class java.util.logging.* { <methods>; }

# Remove debugging - Thread_dumpStack calls. Remove all invocations of Thread.dumpStack().
-assumenosideeffects public class java.lang.Thread { public static void dumpStack(); }

# Remove debugging - Throwable_printStackTrace calls. Remove all invocations of Throwable.printStackTrace().
-assumenosideeffects public class java.lang.Throwable { public void printStackTrace(); }

他のログも上記のコードで取り除けます。たしかどこだったかのサイトから持って来ました。勝手に我が物顔しちゃって申し訳ないっす。
ただし、ただコピペしたかったわけじゃないです。

#-assumenosideeffects public class java.util.logging.* { <methods>; }

としておきましたが、ここをコメントアウトしたことがミソです。
これがあるとAdMob SDKと干渉します。バージョン6.4.1や6.2.1でうまく動かなくなった原因ですのでコメントアウトです。
多分
http://qiita.com/GeneralD@github/items/759840150996de230c37
で説明したisLoggable内で利用されているからかなと思います。

ご清聴ありがとう〜。

50
48
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
50
48