2012-12-08 105 views
3

我是新來Maven和試圖建立一個與它的Android項目。我使用maven-android-plugin的基本設置完成,並且能夠成功地建立我的項目。我已經將min-sdk版本添加到我的項目pom中,因爲我的目標sdk版本是4.2,在pom中也提到了它。 Maven的嘗試編譯對陣雙方versions.The問題開始時添加一些代碼來僅與特定的Android版本上運行。我添加的代碼是(我重用從"Displaying Bitmap Effectively"的android培訓這條代碼):Maven和Android的向後兼容性(android.os.StrictMode)

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. 如何解決這個問題?對不起,我有限的知識。
  3. 3.

回答

0

您需要編譯應用程序對Android 2.3或更高版本以獲得StrictMode

您必須編譯應用程序在你對一個更高的操作系統版本的IDE。
在你的Maven的配置使用相同的版本。

+0

我在maven中使用target作爲4.2,但是我的min sdk版本是2.2.1。它試圖針對兩者進行編譯,但是我添加的代碼確保在運行時只有在需要的類可用時纔會執行它。有沒有一種機制在maven中添加這種運行時依賴關係? –

+0

無論您的最低或目標SDK版本是什麼。如果你想讓編譯器識別'StrictMode',你需要編譯一個具有它的Android版本,即> = 2.3 –