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.
- 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).
- How to solve this issue? Sorry for my limited maven knowledge. 3.