sshifty
sshifty

Reputation: 1

How to setup AndroidAnnotations on android studio?

I did go to Project Structure ->app>Dependencies-> add library->annotations

And when I put @EActivity(R.layout.activity_main) over the main activity what I can do is to create an annotations..but it can not do anything with (R.layout.activity_main).

Androud Studio 2.1.2

Upvotes: 0

Views: 5728

Answers (4)

Shimon Doodkin
Shimon Doodkin

Reputation: 4579

in file app/build.gradle add to dependencies :

annotationProcessor 'org.androidannotations:androidannotations:4.4.0'
implementation 'org.androidannotations:androidannotations-api:4.4.0'

full file:

apply plugin: 'com.android.application'

android {

    compileSdkVersion 28
    defaultConfig {
        applicationId "com.doodkin.svetacalculator"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    annotationProcessor 'org.androidannotations:androidannotations:4.4.0'
    implementation 'org.androidannotations:androidannotations-api:4.4.0'
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

then, for example, need to add @EActivity to an activity

package com.doodkin.svetacalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity // need to add this
public class MainActivity extends AppCompatActivity {

    @ViewById // example use this
    public EditText screen;

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


        screen.setText("test");
    }
}

then after adding @EActivity to an activity. need to modify its class name in app/src/main/AndroidManifest.xml to add an underscore to end of the name of the class

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.doodkin.svetacalculator">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>

Upvotes: 1

Sree...
Sree...

Reputation: 343

This might help

def AAVersion = "4.5.0-SNAPSHOT" // change this to your desired version, for example the latest stable: 4.4.0
dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
}

Upvotes: 1

Pierry
Pierry

Reputation: 989

Sample - just build.gradle(app):

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
  }
}

repositories {
  mavenCentral()
}

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '4.0.0'

dependencies {
  apt "org.androidannotations:androidannotations:$AAVersion"
  compile "org.androidannotations:androidannotations:$AAVersion"
  compile fileTree(dir: 'libs', include: '*.jar')
  // other dependencies
}

apt {
  arguments {
    androidManifestFile variant.outputs[0].processResources.manifestFile
    resourcePackageName 'myfull.packagename'
  }
}

android {
  compileSdkVersion 24
  buildToolsVersion '24.0.0'
  defaultConfig {
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName '1'
    multiDexEnabled true
  }
  sourceSets {
    main {
      manifest.srcFile 'src/main/AndroidManifest.xml'
      java.srcDirs = ['src/main/java', 'build/generated/source/apt/${variant.dirName}']
      resources.srcDirs = ['src/main/res']
      res.srcDirs = ['src/main/res']
      assets.srcDirs = ['src/main/assets']
    }
  }
  packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
  }
  productFlavors {}
  dexOptions {
    javaMaxHeapSize "4g"
  }
}

Upvotes: 0

Phate P
Phate P

Reputation: 1691

Good way is to add your Annotation through Gradle. Then you are able to import and edit it.

Here you have link for official AndroidAnnotation documentation

And here is my working gradle configuration: build.gradle (Projec:ProjectName)

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    // replace with the current version of the Android plugin
    classpath 'com.android.tools.build:gradle:2.1.2'
    // replace with the current version of the android-apt plugin
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

repositories {
    mavenCentral()
    mavenLocal()
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (Module:app)

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
def AAVersion = '4.0.0'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

defaultConfig {
    applicationId "example.project"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

Upvotes: 2

Related Questions