gpt4 book ai didi

android - Maven 和 Android 向后兼容性 (android.os.StrictMode)

转载 作者:太空狗 更新时间:2023-10-29 15:14:34 24 4
gpt4 key购买 nike

我是 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 版本上运行时,问题就开始了。我添加的代码是(我正在重用 "Displaying Bitmap Effectively" 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.

最佳答案

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

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

关于android - Maven 和 Android 向后兼容性 (android.os.StrictMode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com