LoginSignup
13
7

More than 5 years have passed since last update.

AndroidStudio3.1canary9でKotlin+DataBinding

Last updated at Posted at 2018-01-25

こんにちは。kettsun0123です。
CyberAgent,Incにて、SUPERCHOICEやってます。

昨日Android Architecture Components 勉強会 #2に参加して、課題をこなす上で「Kotlin+DataBinding」で詰まりまして、解決したので書き残しておきます。

開発環境

  • AndroidStudio3.1 canary9(たぶんcanary8でもできます、情報求む)
  • gradle3.1.0-alpha09
  • kotlin1.2.21

DataBindingを入れる

以下のステップで行います。

  1. kaptを適用する
  2. DataBindingを有効にする
  3. dependenciesを追加する
  4. 手作業で参照解決する

1.kaptを適用する

app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt' // Add this line

android {
    compileSdkVersion 27

    ...

2.DataBindingを有効にする

app/build.gradle

    ...

android {
    compileSdkVersion 27

    // Add this
    dataBinding {
        enabled = true
    }

    defaultConfig {
        applicationId "xyz.kettsun.livedatalab"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    ...

3.dependenciesを追加する

app/build.gradle
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation "android.arch.lifecycle:extensions:1.1.0"
    kapt "android.arch.lifecycle:compiler:1.1.0"

    kapt "com.android.databinding:compiler:3.1.0-alpha09" // Add this line

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

ここまでやったら一旦Rebuild。

追記(2018/01/25)
どうやら

こちらの記事で書かなくても良くなったと書かれてあるので、dependenciesの更新の行追加は必要ないかも?

4.手作業で参照解決する

AndroidStudio3.1 canaryの影響なのか、buildディレクトリ内のkaptに入ったDatabindingを alt+Enterで参照できないので、手作業でimportしてあげます

MainActivity.kt
...

import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.TextView
import hoge.hogege.databinding.ActivityMainBinding // Add this line
import java.util.*

class MainActivity : AppCompatActivity() {

    lateinit var textView: TextView

    private val binding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.hogehoge ...
    ...

パッケージ名は自身のプロジェクトを見て適宜変えてください。

ちなみに、AS上で「ActivityMainBinding」などで検索すると生成されてるのは確認できるかと思います。

ビルド

ここまでやったらビルドが成功するかと思います。

P.S.

パッケージ名をjavakotlinに変えてる人は

app/build.gradle
android {
    compileSdkVersion 27

    dataBinding {
        enabled = true
    }

    ...

    // Add this
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

sourceSetに追加を忘れずに。

13
7
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
13
7