意味不明な Gradle の Warning や Error を消して激ビルド時間短縮

なんとなく Gradle コンソールを開けてみると死ぬほど書き出されてるエラーやワーニング.


AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$8) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$9) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from source, using an up-to-date compiler","sources":[{}]}
AGPBI: {"kind":"error","text":"and without specifying any \"-target\" type options. The consequence of ignoring","sources":[{}]}
AGPBI: {"kind":"error","text":"this warning is that reflective operations on this class will incorrectly","sources":[{}]}
AGPBI: {"kind":"error","text":"indicate that it is *not* an inner class.","sources":[{}]}
AGPBI: {"kind":"error","text":"warning: Ignoring InnerClasses attribute for an anonymous inner class","sources":[{}]}
AGPBI: {"kind":"error","text":"(org.apache.commons.beanutils.BeanMap$1) that doesn\u0027t come with an","sources":[{}]}
AGPBI: {"kind":"error","text":"associated EnclosingMethod attribute. This class was probably produced by a","sources":[{}]}
AGPBI: {"kind":"error","text":"compiler that did not target the modern .class file format. The recommended","sources":[{}]}
AGPBI: {"kind":"error","text":"solution is to recompile the class from s

いつから出ていたのかわかりません.

Proguard 周りなのでしょうが, 調べても意味がわかりませんでした.

が,

Large number of errors during Gradle build after upgrading to Android Studio 2.0 - Stack Overflow

意味のない warning や error は, .idea フォルダを消すことで取り除くことができます。このフォルダは Android Studio 1.5 が作成したもので 2.0 にインポートされています。消しても自動的に作成されます。

ideadir

一度削除すると再作成されますが, 意味不明なログが吐かれなくなり, ビルド時間も数分から数十秒に激速となりました。

...

などと喜んでいたが, また出てる.

...

Issue Tacker を見てみる.

Issue 215748 - android - AGPBI reports errors instead of warnings - Android Open Source Project - Issue Tracker - Google Project Hosting

まだ生きてる issue なのですが, 一時しのぎとして以下とか.


# https://code.google.com/p/android/issues/detail?id=215748
-keepattributes Exceptions, Signature, InnerClasses, EnclosingMethod
-dontoptimize

一応出なくなりますけれども,

速くなりますけれども,

「don't optimize」て...

またはこんな一時しのぎ法も.

Issue 222989 - android - Android Studio 2.2 : Build successful with 1944 errors - Android Open Source Project - Issue Tracker - Google Project Hosting

It affects me too it is caused by gradle not android studio (but it tries to update the gradle)
Temporary solution:
I went back to gradle 2.1.3 (that I used earlier):

classpath 'com.android.tools.build:gradle:2.1.3' ->this works

If I am using classpath 'com.android.tools.build:gradle:2.2.0' I am also getting tons of warning message and the output is an unaligned file...

「com.android.tools.build:gradle のバージョンを下げる」という方法ですが, うまくダウングレードできないような...

なんなんすかね, まったく.

issues_-_android_-_android_open_source_project_-_issue_tracker_-_google_project_hosting

Issues - android - Android Open Source Project - Issue Tracker - Google Project Hosting

2016-10-05

待てないので以下でとりあえず対応.
AGPBI 関連の多量のエラーを排除する


「これより速いのがあったら言ってこい」Google非公式DIフレームワーク Tiger 登場!!

Google製のライブラリが集まっていいるGitHubリポジトリがありますが.

Google

Google

DIフレームワークが新しく登場しています.

google_tiger

google/tiger

DISCLAIMER: This is not an official Google product. Google's production dependency injection frameworks are Dagger and Guice.

Dagger や Guice があるので, このフレームワークは「非公式」ということですが, 速さの面で気持ちいい啖呵を切っているようにも見えます.

Challenge

If one found a DI framework faster, let me know. I will drop the title as long as it is proved.

「これより速いの見つけたら教えてね♥」

Group

[email protected]

と, メールアドレスもこだわっています.

サンプルを見た感じアノテーションを利用した Dagger とよく似た雰囲気になっておりますが, それぞれのスコープに対してひとつのインジェクターを生成するしくみのようです.


public class PseudoActivity {
  @Inject
  Foo foo;

  @Inject
  @BarAnnotation
  Bar bar;

  @Inject
  @BarAnnotation
  Bar bar2;

  @Inject
  Baz baz;
  
  @Inject
  Shboom shboom;

  void onCreate(DaggerApplicationComponent applicationComponent) {
    DaggerActivityComponent activityComponent =
        new DaggerActivityComponent.Builder().daggerApplicationComponent(applicationComponent).build();
    activityComponent.injectPseudoActivity(this);
  }

  @Override
  public String toString() {
    return "PseudoActivity[foo: " + foo + ", bar: " + bar + " bar2: " + bar2 + ", baz: " + baz
        + " shboom: " + shboom + "]";
  }
}

こんなかんじで最近はGoogleから実験的なライブラリが公開されるようになってきていますが, どれも微妙な雰囲気はあります.

Include the "not an official Google product" disclaimer by gk5885 · Pull Request #3 · google/tiger

これ, 今後どうなっていくのでしょうかー.

Google発リアクティブなライブラリ「Agera」の評判を眺める


DiffUtil で簡単に RecyclerView を更新する

list_mail

例えば, RecyclerView で一覧を作成して表示しているとして


1. 田中一郎
2. 佐藤二郎
3. 鈴木三郎
....

サーバなどに問い合わせて取得された以下データ


1. 田中一郎
2. ジローラモ
3. 鈴木三郎
...

を一覧画面に反映させたい場合.

RecyclerView.Adapter に更新を通知する場合,

RecyclerView.Adapter | Android Developers

notify*() のうちアイテムの更新に関するものは


notifyDataSetChanged()
notifyItemChanged(int position, Object payload)
notifyItemChanged(int position)
notifyItemRangeChanged(int positionStart, int itemCount, Object payload)
notifyItemRangeChanged(int positionStart, int itemCount)

ですが, それぞれ以下の面倒な部分がありました.


notifyDataSetChanged()

→ 変更部分のみに関してのアニメーションが表示されない.


notifyItemChanged(int position, Object payload)
notifyItemChanged(int position)
notifyItemRangeChanged(int positionStart, int itemCount, Object payload)
notifyItemRangeChanged(int positionStart, int itemCount)

→ アニメーションは表示されるが, 変更部分の position を計算して渡す必要がある.

その他 hasStableIds() と Loader を使ったりなど面倒でしたよね.

DiffUtil を使う

support-library-v7 24.2.0 で登場の DiffUtil を使いましょう.

DiffUtil | Android Developers

コールバッククラス作成後, それを使った更新メソッドを Adapter 内に記述します.

MyDiffUtilCallback.java


public class MyDiffUtilCallback extends DiffUtil.Callback{

    List<Person> oldPersons;
    List<Person> newPersons;

    public MyDiffUtilCallback(List<Person> oldPersons, List<Person> newPersons) {
        this.oldPersons = oldPersons;
        this.newPersons = newPersons;
    }

    @Override
    public int getOldListSize() {
        return oldPersons.size();
    }

    @Override
    public int getNewListSize() {
        return newPersons.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return oldPersons.get(oldItemPosition).id == newPersons.get(newItemPosition).id;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        return oldPersons.get(oldItemPosition).equals(newPersons.get(newItemPosition));
    }

    @Nullable
    @Override
    public Object getChangePayload(int oldItemPosition, int newItemPosition) {
	// some additional information
        return super.getChangePayload(oldItemPosition, newItemPosition);
    }
}

MyRecyclerViewAdapter.java


public void updateList(ArrayList<Person> newList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallback(this.persons, newList));
        diffResult.dispatchUpdatesTo(this);
}

これで, アイテムのポジションの計算はおまかせにして, 同時に綺麗なアニメーションが変更部分に適用されます.

実際, データ量が多い場合は厳しいようなので, RxJava なら メインスレッドにて以下.


@Override
public void onNext(DiffUtil.DiffResult result) {
    result.dispatchUpdatesTo(mProductAdapter);
}

kotlinなら同様に以下。


internal class DiffUtilCallback(
    private val oldItems: List<Item>,
    private val newItems: List<Item>
) : DiffUtil.Callback() {

  override fun getOldListSize() = oldItems.size

  override fun getNewListSize() = newItems.size

  override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int)
      = oldItems[oldItemPosition].id == newItems[newItemPosition].id

  override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int)
      = oldItems[oldItemPosition] == newItems[newItemPosition]
}

以下のように使うと便利かもしれません.

リアクティブにデータ更新検知して自動画面更新させる


75%速度向上という Gradle 3.0 を Android Studio で試す

更新されてます, Gradle が 3.0 に.

Gradle_3_0_released_-_News_-_Gradle_Forums

Gradle 3.0 released - News - Gradle Forums

Gradle_Distributions

Gradle Distributions

The Gradle Daemon is a key performance enhancer, making builds up to 75% faster, but it needed to be explicitly enabled in previous versions of Gradle. This is no longer necessary as the Daemon is now enabled by default in 3.0.

Daemon がデフォルトで起動され, 75% の速度向上らしいので.

 

Android Studio で使ってみよう

プロジェクトルートの位置で.

./gradlew wrapper --gradle-version=3.0

これで, Gradle 側はラッパーインストールできますが, Android Plugin が 2.1.x系では対応できていないようです.

最新のbetaを入れてみましょう.

com_android_tools_build_gradle

com.android.tools.build.gradle

(project-root)/build.gradle


buildscript {
  //System.properties['com.android.build.gradle.overrideVersionCheck'] = 'true'
  repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-beta1'
    }
}

Total time: 6.543 secs

Total time: 1.472 secs

小さいプロジェクトでも, 2回目以降のビルドで, 高速になったことが体感できます.

まあ, beta ということで少し待っていたほうがいいのかも知れませんが.

Latest Bugs topics - Gradle Forums

Android お手軽なビルド時間の短縮メモ


【急げ】オライリー「Gradle Recipes for Android」が無料ダウンロード配布中!!

あのオライリーから発売されているこの本ですが.

Amazon_co_jp:_Gradle_Recipes_for_Android__Master_the_New_Build_System_for_Android__Ken_Kousen__洋書

Chapter 1 Gradle for Android Basics
1.1. Gradle Build Files in Android
1.2. Configure SDK Versions and Other Defaults
1.3. Executing Gradle Builds from the Command Line
1.4. Executing Gradle Builds from Android Studio
1.5. Adding Java Library Dependencies
1.6. Adding Library Dependencies Using Android Studio
1.7. Configuring Repositories

Chapter 2 From Project Import to Release
2.1. Setting Project Properties
2.2. Porting Apps from Eclipse ADT to Android Studio
2.3. Porting Apps from Eclipse ADT Using Eclipse
2.4. Upgrading to a Newer Version of Gradle
2.5. Sharing Settings Among Projects
2.6. Signing a Release APK
2.7. Signing a Release APK Using Android Studio

Chapter 3 Build Types and Flavors
3.1. Working with Build Types
3.2. Product Flavors and Variants
3.3. Merging Resources
3.4. Flavor Dimensions
3.5. Merging Java Sources Across Flavors

Chapter 4 Custom Tasks
4.1. Writing Your Own Custom Tasks
4.2. Adding Custom Tasks to the Build Process
4.3. Excluding Tasks
4.4. Custom Source Sets
4.5. Using Android Libraries

Chapter 5 Testing
5.1. Unit Testing
5.2. Testing with the Android Testing Support Library
5.3. Functional Testing with Robotium
5.4. Activity Testing with Espresso

Chapter 6 Performance and Documentation
6.1. Performance Recommendations
6.2. DSL Documentation
Appendix Just Enough Groovy to Get By
Appendix Gradle Basics



無料版PDFが本家Gradleのサイトからダウンロードできるようになっています!

New_Gradle_Android_Ebook_for_Free_-_Gradle

New Gradle Android Ebook for Free - Gradle

メールアドレスなど入力後, 送信されてくるメール内のリンクからダウンロードできます.

Free_ebook_from_O_Reilly__Learn_Gradle_for_Beginners

図表入りで, 初心者にもわかりやすい内容になっています.

Gradle_Recipes_for_Android

Gradle_Recipes_for_Android 2

早めにダウンロードしたほうがいいような気がします!