LoginSignup
5
4

More than 5 years have passed since last update.

Android StudioでDataBindingを使う

Last updated at Posted at 2015-09-19

Android DevelopersサイトでData Binding Guildが公開されています。でもバージョンが古いと怒られたり、AndroidStudioで作ったプロジェクがビルドエラーになったりと色々苦労がありました。そんな中でうまくいったバージョンの組み合わせがあったので、以下に上記サイトやAndroidStudioのデフォルト値との差分を書いておきます。

各種バージョン

いろいろなもののバージョンを書いておきます。

AndroidStudio

1.3.2を使っています。

Build Environment

AndroidStudioのプロジェクト直下のbuild.gradleでは、現時点での最新バージョンを指定しました。

/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:1.3.1"
        classpath "com.android.databinding:dataBinder:1.0-rc2"
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

appモジュール下のbuild.gradleでは、appcompatのバージョンを23から22に下げました。それに伴い、compileSdkVersionとtargetSdkVersionのバージョンも下げています。

/app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'

android {
    compileSdkVersion 22
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "red.itvirtuoso.databindingsample"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

この組み合わせでビルドが通り、nexus 7 (ver 4.4.4)で動作確認できました。

苦労したところ

いろいろ苦労したことを書いておきます。

レイアウトファイルについて

Androidで自動生成されるレイアウトファイルは、<XxxxLayout>タグがルートになっています。

AndroidStuidoが自動生成するレイアウトファイルの例
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="red.itvirtuoso.databindingsample.Main2Activity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

しかしDataBindingを使うときは、<layout>タグを用意して、その子供として<XxxxLayout>タグ、そしてバインドするクラスを指定する<data>タグを用意します。このとき、もともと<XxxxLayout>にあった名前空間の指定(xmlns:androidなど)は、<layout>タグに引っ越したほうがいいかもしれません。

DataBindingGuideにあるレイアウトファイルの例
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="user" type="red.itvirtuoso.databindingsample.User"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.firstName}"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.lastName}"/>
    </LinearLayout>
</layout>

生成されるBinding用のクラス名について

Data Binding Guidでは、レイアウトファイルから自動生成されるクラスはMainActivityBindingだと説明されています。しかしこのファイルは、レイアウトファイルの名前から決定されていて、AndroidStudioが作るレイアウトファイル名はactivity_main.xmlとなっているので、自動生成されるクラスはActivityMainBindingとなっていました。

github

動作したソースをgithubで公開しておきます。
https://github.com/nakaken0629/DataBindingSample

5
4
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
5
4