在为Android开发应用程序时,Min和Target SDK版本有什么不同?Eclipse不允许我创建新项目,除非最小版本和目标版本相同!


当前回答

如果你正在制作需要危险权限的应用程序,并将targetSDK设置为23或更高,你应该小心。如果你在运行时不检查权限,你会得到一个SecurityException,如果你在一个try块中使用代码,例如open camera,如果你不检查logcat,就很难发现错误。

其他回答

如果你正在制作需要危险权限的应用程序,并将targetSDK设置为23或更高,你应该小心。如果你在运行时不检查权限,你会得到一个SecurityException,如果你在一个try块中使用代码,例如open camera,如果你不检查logcat,就很难发现错误。

Target sdk是你想要的目标版本,而min sdk是最小的一个。

android: minSdkVersion

一个整数,指定应用程序运行所需的最小API级别。如果系统的API级别低于此属性中指定的值,Android系统将阻止用户安装应用程序。您应该始终声明此属性。

android: targetSdkVersion

一个整数,指定应用程序的目标API级别。

With this attribute set, the application says that it is able to run on older versions (down to minSdkVersion), but was explicitly tested to work with the version specified here. Specifying this target version allows the platform to disable compatibility settings that are not required for the target version (which may otherwise be turned on in order to maintain forward-compatibility) or enable newer features that are not available to older applications. This does not mean that you can program different features for different versions of the platform—it simply informs the platform that you have tested against the target version and the platform should not perform any extra work to maintain forward-compatibility with the target version.

更多信息请参考这个URL:

http://developer.android.com/guide/topics/manifest/uses-sdk-element.html

android:minSdkVersion和android:targetSdkVersion都是Integer值,我们需要在android manifest文件中声明,但两者都有不同的属性。

android:minSdkVersion:这是运行android应用程序所需的最低API级别。如果我们将在较低的API版本上安装相同的应用程序,解析器将出现错误,应用程序不支持的问题将出现。

android:targetSdkVersion: Target sdk version用于设置应用程序的Target API级别。如果manifest中没有声明这个属性,minSdk version将是你的TargetSdk version。这总是正确的,“应用程序支持安装在所有更高版本的API,我们声明为TargetSdk版本”。要使应用程序有限的目标,我们需要声明maxSdkVersion在我们的manifest文件…

如果你得到一些编译错误,例如:

<uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="15" />

.

private void methodThatRequiresAPI11() {
        BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Config.ARGB_8888;  // API Level 1          
                options.inSampleSize = 8;    // API Level 1
                options.inBitmap = bitmap;   // **API Level 11**
        //...
    }

你会得到编译错误:

字段要求API级别11(当前最小值为10): # inBitmap android.graphics.BitmapFactory美元选项

自从第17版的Android开发工具(ADT),有一个新的和非常有用的注释@TargetApi,可以很容易地修复这个问题。将它添加到包含有问题声明的方法之前:

@TargetApi
private void methodThatRequiresAPI11() {            
  BitmapFactory.Options options = new BitmapFactory.Options();
      options.inPreferredConfig = Config.ARGB_8888;  // API Level 1          
      options.inSampleSize = 8;    // API Level 1

      // This will avoid exception NoSuchFieldError (or NoSuchMethodError) at runtime. 
      if (Integer.valueOf(android.os.Build.VERSION.SDK) >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        options.inBitmap = bitmap;   // **API Level 11**
            //...
      }
    }

现在没有编译错误,它将运行!

EDIT:这将导致API级别低于11的运行时错误。在11或更高版本上,它运行起来没有问题。因此,必须确保在版本检查保护的执行路径上调用此方法。TargetApi只允许你编译它,但是你要自担风险。