【发布时间】:2015-12-10 14:33:20
【问题描述】:
我想在 Java/Maven 项目中使用 Google 的 Android 支持注释库 (http://tools.android.com/tech-docs/support-annotations) 提供的一些有用的注释。
我希望在编译时使用 mvn 或 Eclipse 验证注释,而不使用 Android Studio。
这可能吗?
我的pom.xml 我写了以下内容:
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-annotations</artifactId>
<version>23.0.0</version>
<scope>provided</scope>
</dependency>
我目前没有使用android-maven-plugin,因为它只是一个简单的 jar,不依赖于其他 Android 库。声明依赖项允许我在代码中使用注释。例如:
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
public class TestAnnot
{
@CallSuper
public void blah() { }
public void bleh(@NotNull String str) { }
}
这在 Eclipse 和 mvn 中都能正常编译。但是,任何明显违反注释规则的行为都不会被标记。例如,目前没有什么能阻止我这样做:
public class SubTestAnnot
{
@Overrides
public void blah()
{
// not calling super.blah() even though I should
bleh(null); // calling bleh(String) with null even though that's not allowed
}
}
我查看了该库的源代码(查看<Android SDK path>/extras/android/m2repository/com/android/support/support-annotations/23.0.0),但它似乎只声明了注释本身,但没有验证它们的代码。所以我假设谷歌已经单独实现了,也许是在一个 SDK 组件中?
那么,有谁知道我如何配置 Maven 和/或 eclipse 来实际检查这些注释?
【问题讨论】:
-
Android Studio 会在调用 super 失败的方法下划红色下划线,但实际上并没有阻止编译并且应用程序仍然运行。
标签: java android eclipse maven annotations