LoginSignup
10
9

More than 5 years have passed since last update.

AndroidAnnotations with Kotlin

Last updated at Posted at 2016-11-07

久々にandroidのコードを書き始めました。
最近はkotlinでSpringBootなサーバサイドのコードばかりを書いてたこともあり、
アノテーションとkotlinをどうしても使いたいと調べたところ、
AndroidAnnotationsとkotlinの利用となりました。

少し前の記事ですと、AndroidAnnotationsとkotlinは、
一緒に使えないということになっていましたが、今ではできるようです。

日本語の情報がまったくなかったので、
とりあえず自分のハマったポイントをまとめたいと思います。

gradleの設定

まずはGradleの設定です。

build.gradle

buildscript {
    ext.kotlin_version = '1.0.4'

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // AndroidAnnotations
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}
app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
def AAVersion = '4.1.0'
android {
    compileSdkVersion 25
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 17    //この辺は自由に
        targetSdkVersion 19 //この辺は自由に
        versionCode 1       //この辺は自由に
        versionName "1.0"   //この辺は自由に
    }
}

dependencies {
    //kotlin
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //appcompat(なくてもいいけど、どうせ使う)
    compile 'com.android.support:appcompat-v7:25.0.0'

    //android annotations
    kapt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

//ここが大事
kapt {
    generateStubs = true
    arguments {
        arg("resourcePackageName",'com.example')
        arg("androidManifestFile",variant.outputs[0].processResourcesTask.manifestFile)
    }
}

とこんな感じに設定します。

Activityの作成

次にactivityを作ります。

MainActivity.kt

@EActivity(R.layout.activity_main)
open class MainActivity:AppcompatActivity(){//openを忘れない

    //ViewのInject
    @ViewById(R.id.button)
    lateinit var button:Button

    //savedInstanceStateが使いたい場合
    @InstanceState
    var savedInstanceState:Bundle? = null

    //Clickイベントを処理したい時
    @Click(R.id.button)
    open fun buttonClick(){//openを忘れない
        //なにか
    }
}

大事なのはopenを忘れないことです。
kotlinはopenを付けないと常にfinalです。
finalではannotation processorが色々できません。
なので、Spring bootもそうですが、open祭りになります。
(open祭りがなくなる案も出てますが・・・。)

AndroidManifest.xmlの設定

最後にmanifestの設定です。

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0.0.SNAPSHOT" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:allowBackup="true">
        <activity android:name=".MainActivity_" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity何だけどMainActivity_を使いましょう。
androidannotationsでは最後に_が付けられたクラスが生成されます。
もしクラスが無いよーなんてIDEに言われても@EActivityなクラスは最後に_(アンダースコア)を付けましょう。ビルドしたら生成されます。
また@EFragmentなクラスを利用する際も_(アンダースコア)を忘れないように!

その他

その他の使い方はDocumentを読もう!
https://github.com/androidannotations/androidannotations/wiki
基本的にopen_(アンダースコア)を忘れなければOKだと思います。

追加

kotlin1.0.6になって新しい、プラグインができました。
all-openとno-argです。
all-openはopenを書く手間が
no-argは、わざわざ引数の無いコンストラクラをつくる手間が省ける代物です。
この辺を設定すれば更に開発が捗ることと思います。

10
9
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
10
9