我是 java 和 android 开发的新手。我会用 Android Saripaar v2 (thx at Ragunath Jawahar)用于验证。
我使用 Android Studio。
在一个没有其他模块的新的干净的 android 项目上它工作得很好。
但是在我的多模块应用程序中,注释中的 messageResId 属性存在问题。 (属性值必须是常量)
这是我的代码:
build.gradle(模块:应用)
...
// workaround for "duplicate files during packaging of APK" issue
// see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
allprojects {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "http://files.couchbase.com/maven2/" }
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile project(':auth')
}
...
build.gradle(模块:auth)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.mobsandgeeks:android-saripaar:2.0-SNAPSHOT'
compile project (':other_api_module')
}
auth/java/.../SignInFragment.java 此文件中的错误
public class SignInFragment extends Fragment implements View.OnClickListener, View.OnFocusChangeListener, Validator.ValidationListener{
String TAG = SignInFragment.class.getName();
private ISignInFragment iSignInFragment;
@Email(messageResId = R.string.editTextEmailValidationError) //ERROR: Attribute (R.string.editTextEmailValidationError) value must be constant
private EditText editTextEmail;
private EditText editTextPassword;
static public String EMAIL = "email";
最佳答案
您不能在库模块中使用需要 Android 资源标识符作为其属性的注解。您只能将它们与应用程序模块一起使用。
库模块中的资源标识符不是最终的。另一方面,应用程序模块中的资源标识符是最终的。 Java 注释仅接受属性值的常量(最终变量)。
关于android-saripaar v2 messageResId 必须是常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260455/