3

I am new to maven and is trying to setup an android project with it. I am done with the basic setup using maven-android-plugin, and is able to build my project successfully. I have added min-sdk version to my project pom as

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

My target sdk version is 4.2 which is also mentioned in pom. Maven try to compile it against both the versions.The problem starts when in add some code to run only with specific android version. The code i have added is (I am reusing this code from "Displaying Bitmap Effectively" android training article):

    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;
    }
}

Now the problem starts. This code is running fine even on android 2.2.1 but maven build is failing with following errors:

[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

There are few points which i fail to understand.

  1. If maven fails in compiling this code against android 2.2.1 lib, how is it not failing at runtime on phone. (Note that it cannot even find GINGERBREAD).
  2. How to solve this issue? Sorry for my limited maven knowledge.
  3. 3.
j0k
  • 22,600
  • 28
  • 79
  • 90
Shashank Tomar
  • 861
  • 10
  • 15

1 Answers1

0

You need to compile the app against Android 2.3 or higher to get StrictMode.

You must be compiling the app in your IDE against a higher OS version.
Use the same version in your Maven config.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • I am using target as 4.2 in maven, but my min sdk version is 2.2.1. It tries to compile it against both, but the code i have added make sures that at runtime it get executed only if the required classes are available. Is there a mechanism to add such runtime dependency in maven? – Shashank Tomar Dec 09 '12 at 02:13
  • It doesn't matter what your minimum or target SDK versions are. If you want the compile to recognise `StrictMode`, you need to compile against an Android version which has it, i.e. >= 2.3 – Christopher Orr Dec 09 '12 at 14:23