LoginSignup
8
7

More than 5 years have passed since last update.

Android StudioでAndroid Annotationsを使う方法

Posted at

app/build.gradle の内容を次のように書き換えて、GradleのSyncを行います。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // Androidプラグインに使用しているバージョンに書き換える
        classpath 'com.android.tools.build:gradle:0.12.+'
        // Android Gradle プラグイン0.11から、android-apt 1.3以上を使わなければいけない
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}

apply plugin: "com.android.application"
apply plugin: 'android-apt'

dependencies {
    apt 'org.androidannotations:androidannotations:3.1'
    compile 'org.androidannotations:androidannotations-api:3.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // その他の必要な依存ライブラリー
    // compile 'com.android.support:support-v4:20.0.0'
    // compile 'com.android.support:appcompat-v7:20.0.0'
    // ...
}

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName android.defaultConfig.applicationId

        // 追加のアノテーションプロセッシングオプションを下記のように指定可能
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'
    defaultConfig {
        applicationId 'アプリのパッケージ名'
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName '1.0'
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'build/generated/source/apt/${variant.dirName}']
        }
    }
}

ライブラリーのバージョンなど、必要に応じて書き換えてください。

これで@EActivityなどが使えるようになります。
なお、Make Project (Ctrl+F9)をしないとクラスが生成されないので、AndroidManifest.xmlに_付きのActivity等を宣言する際は初回のみエラーが出るかも。

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