3

我是 maven 新手,正在尝试用它设置一个 android 项目。我使用maven-android-plugin完成了基本设置,并且能够成功构建我的项目。我已将 min-sdk 版本添加到我的项目 pom 中

<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
</dependency>

我的目标 sdk 版本是 4.2,在 pom.xml 中也提到过。Maven 尝试针对这两个版本进行编译。当添加一些代码以仅使用特定的 android 版本运行时,问题就开始了。我添加的代码是(我正在重用“有效显示位图” android培训文章中的这段代码):

    public class Utils {
    private Utils() {};

    @TargetApi(11)
    public static void enableStrictMode() {
        if (Utils.hasGingerbread()) {
            StrictMode.ThreadPolicy.Builder threadPolicyBuilder =
                    new StrictMode.ThreadPolicy.Builder()
                            .detectAll()
                            .penaltyLog();
            StrictMode.VmPolicy.Builder vmPolicyBuilder =
                    new StrictMode.VmPolicy.Builder()
                            .detectAll()
                            .penaltyLog();

            if (Utils.hasHoneycomb()) {
                threadPolicyBuilder.penaltyFlashScreen();
                vmPolicyBuilder
                        .setClassInstanceLimit(ImageGridActivity.class, 1)
                        .setClassInstanceLimit(ImageDetailActivity.class, 1);
            }
            StrictMode.setThreadPolicy(threadPolicyBuilder.build());
            StrictMode.setVmPolicy(vmPolicyBuilder.build());
        }
    }

    public static boolean hasFroyo() {
        // Can use static final constants like FROYO, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    public static boolean hasGingerbread() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean hasHoneycombMR1() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
    }

    public static boolean hasJellyBean() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
    }
}

现在问题开始了。此代码即使在 android 2.2.1 上也运行良好,但 maven 构建失败并出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project laminar-app: Compilation failure: Compilation failure:
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[24,17] error: cannot find symbol
[ERROR] symbol:   class StrictMode
[ERROR] location: package android.os
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[35,35] error: package StrictMode.ThreadPolicy does not exist
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[36,47] error: package StrictMode.ThreadPolicy does not exist
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[39,31] error: package StrictMode.VmPolicy does not exist
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[40,43] error: package StrictMode.VmPolicy does not exist
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[55,12] error: cannot find symbol
[ERROR] symbol:   variable StrictMode
[ERROR] location: class Utils
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[56,12] error: cannot find symbol
[ERROR] symbol:   variable StrictMode
[ERROR] location: class Utils
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[61,59] error: cannot find symbol
[ERROR] symbol:   variable GINGERBREAD
[ERROR] location: class VERSION_CODES
[ERROR] /Users/shashant/Documents/workspace/Android/laminar-parent/laminar-app/src/main/com/laminar/utils/Utils.java:[65,59] error: cannot find symbol

有几点我不明白。

  1. 如果 maven 无法针对 android 2.2.1 lib 编译此代码,那么它在手机上运行时如何不会失败。(请注意,它甚至找不到 GINGERBREAD)。
  2. 如何解决这个问题?对不起,我有限的 Maven 知识。
  3. 3.
4

1 回答 1

0

您需要针对 Android 2.3 或更高版本编译应用程序才能获得StrictMode.

您必须在 IDE 中针对更高的操作系统版本编译应用程序。
在 Maven 配置中使用相同的版本。

于 2012-12-08T19:51:41.273 回答