Android Data Binding は いつ実力を発揮できるのだ?!

Data_Binding_Guide___Android_Developers

Data Binding Guide | Android Developers

たくさん記事を見かけるようになったのでかんたんに比較してみようかと思い.

20160104-195007

これまで

public class MainActivity extends AppCompatActivity {

  private TextView name;
  private TextView age;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    name = (TextView) findViewById(R.id.name);
    age = (TextView) findViewById(R.id.age);

    User user = new User("ジョーンズ", 30);

    name.setText(user.name);
    age.setText(String.valueOf(user.age));
  }

  public void onClickName(View v) {
    Toast.makeText(this, " 名前をクリックしました。", Toast.LENGTH_SHORT).show();
  }

}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickName"
        />
    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
 </LinearLayout>

ButterKnife

dependencies {
 	...
    compile 'com.jakewharton:butterknife:7.0.1'
}
public class MainActivity extends AppCompatActivity {

  @Bind(R.id.name) TextView name;
  @Bind(R.id.age) TextView age;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    ButterKnife.bind(this);

    User user = new User("ジョーンズ", 30);

    name.setText(user.name);
    age.setText(String.valueOf(user.age));
  }

  @OnClick(R.id.name)
  public void onClickName(View v) {
    Toast.makeText(this, " 名前をクリックしました。", Toast.LENGTH_SHORT).show();
  }

}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
 </LinearLayout>

Data Binding

android {
	...
    dataBinding {
        enabled = true
    }
}
public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MainActivityBinding binding = 
         DataBindingUtil.setContentView(this, R.layout.main_activity);

    User user = new User("ジョーンズ", 30);

    binding.setUser(user);
    binding.setActivity(this);
  }

  public void onClickName(View v) {
    Toast.makeText(this, " 名前をクリックしました。", Toast.LENGTH_SHORT).show();
  }

}
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="user"
            type="com.example.User"/>
        <variable
            name="activity"
            type="com.example.MainActivity" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}"
            android:onClick="@{activity.onClickName}"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(user.age)}"
            />
    </LinearLayout>
</layout>

むむむ...

レイアウトを繰り返し使うような ListView/RecyclerView で実力を発揮するのかな?

This is more than binding simple data to your views and avoiding the boilerplate code. To me, this is actually mixing business logic in your UI layouts. Your views should be as dumb as possible, adhering to the Single Responsibility Principle with the only responsibility to show data. This could lead to a complicated, cluttered and unclean codebase.

We've seen some of the examples of the Data Binding API. I'm sure that this API was created in order to ease the developers' work and avoid boilerplate, but one can overuse it and accidentally create chaos in his code. Mixing Java in your view code has never been a good idea. Does JSP ring a bell?

How you can go wrong with the new Data Binding API

強力な分だけ「用法・用量」に注意, ということなのか.

ちなみに AndroidStudio最新環境では build.gradle 内 dataBinding { enabled = true } のみで使えるようになっておる.

Data Binding Guide | Android Developers


【2015-10-14 公開!!】Google I/O 2015 のソースコードに見るディレクトリ構成

d2f9b090-727a-11e5-96aa-b92347fe83f2

半年くらい遅れていましたが公開されている.

Oct 14, 2015, 5:41 PM GMT+9
Initial source posted. We'll be iterating on it a bit going forward. As always we welcome contributions.

PaulRashidi

I/O 2015 Source Code · Issue #139 · google/iosched

google_iosched

google/iosched

Androidアプリコード部分の基本的なディレクトリ/ファイル (モジュール/クラス) の構成をみる.

iosched/android/src/main/java/com/google/samples/apps/iosched/

機能別 (供用)

AppApplication.java
Config.java

provider/*Contract.java
provider/*Enum.java
provider/*Provider.java
provider/*Database.java (extends SQLOpenHelper)

receiver/*Receiver.java
service/*Service.java
util/*Utils.java

io/model/*.java
io/*Handler.java (extends JSONHandler)
io/JSONHandler.java

ui/BaseActivity.java

画面別

*/*Constants.java

*/*Activity.java
*/*Fragment.java
*/*View.java

*/*Adapter.java

*/*Helper.java
*/*Utils.java

*/data/*Data.java
*/*Model.java (interface)

直感的に分かりやすく整理されています.

ソースディレクトリの構成時にもっておくべき「会社別」のイメージ


API-23 で「can’t find referenced class org.apache.http.*」

API-23 に上げるとサードパーティなライブラリ群に対して, こんな...

error

削除されているのですね.

API_Differences_between_22_and_23

API Differences between 22 and 23

各ライブラリが対応されるまではどうしたらよいのか.

...
android {
       useLibrary 'org.apache.http.legacy'
       ...
}
...

Apache HTTP connection with Android 6.0 (Marshmallow) - Stack Overflow

なんだか気持ち悪い.

動作の変更点 | Android Developers


C2DM 終了 GCMへの移行は10月までに

こんなの表示されてます. デベロッパーコンソール.

c2dm_gcm

C2DM のサポートがまもなく終了 Android Cloud to Device Messaging(C2DM)のサポートが完全に終了し、以降のサポートが提供されなくなることを 4 月に発表いたしました。C2DM は、次世代の C2DM である Google クラウド メッセージング(GCM)に代わります。C2DM を使用しているアプリの GCM への移行に十分な時間をかけられるよう、移行期限は 10 月まで延長されています。

ClientLogin for Google Cloud Messaging (GCM) has been shut down - Google グループ

該当するかどうかは, 「gcm.jar」 を利用しているかをみればよい.と.

C2DM をまだ使用しているアプリがある場合は、できる限り速やかに GCM に移行してください。2015 年 10 月までにアプリを最新の GCM ライブラリに移行しないと、C2DM プッシュ メッセージングが正常に機能しなくなります。C2DM を使用しているかどうかは、実装で gcm.jar ライブラリまたは GCM ヘルパー ライブラリを使用しているかで判断できます。

取得したトークンの文字列自体ははそのまま使えるのだろうと思います.

Cloud Messaging   |   Google Developers

変更の詳細手順は以下から.

Try Cloud Messaging for Android   |   Cloud Messaging   |   Google Developers


Android Studio の ビルド がやけにトロい

イライラすぎるので キンキンに調べてやろうと, でまずぐぐって糸口.

gradle.properties ファイルを変更
gradle をオフラインモードに変更

Android Studioのgradleビルドを高速化する - Qiita

org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError - Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.configureondemand=true

[AndroidStudio] 機能実装のビルド時間を減らしたい

.gradle/gradle.propertiesに
org.gradle.daemon=true
と書いておけばGradleデーモンが使われるので立ち上がりが速くなる、というのはあちこちに書いてあるが、実はGradleデーモンが使われていない可能性がある。

また、Gradleの引数が一致していない場合には、IntelliJのSettings->Gradle->Gradle VM Optionsの値を、手動で起動したGradleと合わせておく必要がある。私の場合には、以下を設定してある。
-XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -Xmx1024m

夜でもアッサム: AndroidのGradleでのビルドが遅いのをちょっと速くする

Android開発を爆速にする10のコマンドラインスクリプト - クックパッド開発者ブログ

Android/Gradleのビルド高速化実験 #potatotips - diary ichigotake

いろんな方法があるようなので gradle.properties の方法をまずは.

プロジェクトによってあったりなかったりした, gradle.properties.

別プロジェクトからコピーしてくるか, ユーザーガイドサンプルにもある.

~/.gradle $ find . -name gradle.properties

プロジェクトディレクトリの直下に以下1行だけコメントインしたものを置いて,

# Project-wide Gradle settings.                                                                                                                                 

# IDE (e.g. Android Studio) users:                                                                                                                              
# Gradle settings configured through the IDE *will override*                                                                                                    
# any settings specified in this file.                                                                                                                          

# For more details on how to configure your build environment visit                                                                                             
# http://www.gradle.org/docs/current/userguide/build_environment.html                                                                                           

# Specifies the JVM arguments used for the daemon process.                                                                                                      
# The setting is particularly useful for tweaking memory settings.                                                                                              
# Default value: -Xmx10248m -XX:MaxPermSize=256m                                                                                                                
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.                                                                                                 
# This option should only be used with decoupled projects. More details, visit                                                                                  
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects                                                                 
# org.gradle.parallel=true

ビルドしてみたら, 数十秒かかってたのが 2秒くらいになったけど.

gradle.properties

なんでかな(笑)

わたしは今までなにをしてたのかな(笑)

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

【Android Studio】idea.vmoptions の設定

オプションに「–profile」をつけて Run時間が1分から2秒になった話

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