What is the difference between compileSdkVersion and targetSdkVersion?

前端 未结 11 1982
生来不讨喜
生来不讨喜 2020-11-22 10:44

I have looked at the documentation for building with Gradle, but I\'m still not sure what the difference between compileSdkVersion and targetSdkVersion

相关标签:
11条回答
  • 2020-11-22 11:31

    compileSdkVersion

    The compileSdkVersion is the version of the API the app is compiled against. This means you can use Android API features included in that version of the API (as well as all previous versions, obviously). If you try and use API 16 features but set compileSdkVersion to 15, you will get a compilation error. If you set compileSdkVersion to 16 you can still run the app on a API 15 device as long as your app's execution paths do not attempt to invoke any APIs specific to API 16.

    targetSdkVersion

    The targetSdkVersion has nothing to do with how your app is compiled or what APIs you can utilize. The targetSdkVersion is supposed to indicate that you have tested your app on (presumably up to and including) the version you specify. This is more like a certification or sign off you are giving the Android OS as a hint to how it should handle your app in terms of OS features.

    For example, as the documentation states:

    For example, setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher...

    The Android OS, at runtime, may change how your app is stylized or otherwise executed in the context of the OS based on this value. There are a few other known examples that are influenced by this value and that list is likely to only increase over time.

    For all practical purposes, most apps are going to want to set targetSdkVersion to the latest released version of the API. This will ensure your app looks as good as possible on the most recent Android devices. If you do not specify the targetSdkVersion, it defaults to the minSdkVersion.

    0 讨论(0)
  • 2020-11-22 11:31

    Late to the game.. and there are several great answers above-- essentially, that the compileSdkVersion is the version of the API the app is compiled against, while the targetSdkVersion indicates the version that the app was tested against.

    I'd like to supplement those answers with the following notes:

    1. That targetSdkVersion impacts the way in which permissions are requested:

      • If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the app requests permissions from the user at run-time.
      • If the device is running Android 5.1 (API level 22) or lower, or the app's targetSdkVersion is 22 or lower, the system asks the user to grant the permissions when the user installs the app.
    2. If the compileSdkVersion is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your app continues to work the way you expect. (ref)

    3. With each new Android release...

      • targetSdkVersion should be incremented to match the latest API level, then thoroughly test your application on the corresponding platform version
      • compileSdkVersion, on the other hand, does not need to be changed unless you're adding features exclusive to the new platform version
      • As a result, while targetSdkVersion is often (initially) less than than the compileSdkVersion, it's not uncommon to see a well-maintained/established app with targetSdkVersion > compileSdkVersion
    0 讨论(0)
  • 2020-11-22 11:33

    As a oneliner guide:

    minSdkVersion <= targetSdkVersion <= compileSdkVersion
    

    Ideally:

    minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)
    

    Read more from this great post by Ian Lake

    0 讨论(0)
  • 2020-11-22 11:34

    The CompileSdkVersion is the version of the SDK platform your app works with for compilation, etc DURING the development process (you should always use the latest) This is shipped with the API version you are using

    You will see this in your build.gradle file:

    targetSdkVersion: contains the info your app ships with AFTER the development process to the app store that allows it to TARGET the SPECIFIED version of the Android platform. Depending on the functionality of your app, it can target API versions lower than the current.For instance, you can target API 18 even if the current version is 23.

    Take a good look at this official Google page.

    0 讨论(0)
  • 2020-11-22 11:40

    Not answering to your direct questions, since there are already a lot of detailed answers, but it's worth mentioning, that to the contrary of Android documentation, Android Studio is suggesting to use the same version for compileSDKVersion and targetSDKVersion.

    0 讨论(0)
提交回复
热议问题