如何在Android应用程序中集成android注释?

时间:2016-07-12 07:35:50

标签: java android android-studio sharedpreferences android-annotations

遵循these步骤在项目中配置android annotation个依赖项。官方documentation的链接。

现在在项目中创建了一个sharedPreference类:

@SharedPref(value = SharedPref.Scope.UNIQUE)
public interface SampleApp {
  @DefaultInt(0)
  int appOpenedCount();

  @DefaultInt(0)
  int appOpenedCountVersionSpecific();
}

我正在尝试在android类中访问这些preferences

@SuppressLint("Registered")
@EApplication
public class HuntApp extends Application {

private static HuntApp appInstance;
private static Context mContext;
public static SampleApp_ appPref;

@Override
public void onCreate() {
    super.onCreate();
    appPref = new SampleApp_(this);
}

但它引发了一个错误:

Error:(19, 19) error: cannot find symbol class SampleApp_

我重新确认了依赖项和步骤,但无法构建项目。我在这做错了什么?任何帮助都会很明显。

Edit-1 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:name=".HuntApp_"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".LoginActivity_"></activity>

</application>

Edit-2模块gradle文件:

buildscript {
repositories {
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}

    repositories {
        mavenCentral()
        mavenLocal()
    }

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

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName "com.app.huntapp"
        logLevel 'INFO'
        logFile '/var/log/aa.log'
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.app.huntapp"
        minSdkVersion 19
        targetSdkVersion 24
        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:24.0.0'
    compile 'com.android.support:design:24.0.0'
}

1 个答案:

答案 0 :(得分:0)

build.gradle中你必须把它放在:

apply plugin: 'android-apt'

并在依赖块中:

apt 'org.androidannotations:androidannotations:4.0.0'
compile 'org.androidannotations:androidannotations-api:4.0.0'

然后,您无法实例化prefs类。你必须使用@Pref注入它:

@Pref
SampleApp prefs;