compile with在安卓开发是什么

在安卓开发中,编译器(Compiler)是非常重要的一环。在编写代码完成后,我们需要将它转换成可执行的机器码。这个过程就称之为编译。compile with 作为 Android Studio 编译环境中的一个重要选项,其作用是指定将代码编译为可运行应用的 Android 版本。本文将逐一介绍 compile with 的原理和使用方法。

什么是 compile with?

compile with 可以直译为“编译为”,在 Android Studio 中指定了一个版本号,用于告诉编译器我们使用的是什么 Android 平台版本。compile with 选项会影响代码编译过程中许多的决策,包括运行时权限许可、库依赖和应用程序二进制接口(API)等。同时,compile with 也会直接影响到应用程序运行的最低设备版本。简单来说,这个选项将决定你的应用程序在哪些平台上能够运行。

compile with 原理

compile with 的原理在于操作系统的不同版本。每一个 Android 系统版本都有其相关的 API 版本。编译时使用的 API 版本会决定你的应用程序是否能被运行在某一特定平台上,以及它是否能够在该平台上获得正确的行为和可用性。API 层是 Android 安卓提供的标准公共程序界面,软件开发者可以利用它们来调用其他程序的子例程或者运行自己的子例程。

Android 操作系统版本对应的 API 级别如下:

| Android 版本 | API 级别 |

| :---: | :---: |

| 1.0 | 1 |

| 1.1 | 2 |

| 1.5 | 3 |

| 1.6 | 4 |

| 2.0 - 2.0.1 | 5 |

| 2.1.x | 7 |

| 2.2 - 2.2.3 | 8 |

| 2.3 - 2.3.2 | 9 |

| 2.3.3 - 2.3.7 | 10 |

| 3.0 | 11 |

| 3.1 | 12 |

| 3.2 | 13 |

| 4.0.1 - 4.0.2 | 14 |

| 4.0.3 - 4.0.4 | 15 |

| 4.1.x | 16 |

| 4.2.x | 17 |

| 4.3.x | 18 |

| 4.4 - 4.4.4 | 19 |

| 5.0 - 5.1.1 | 21 |

| 6.0 - 6.0.1 | 23 |

| 7.0 - 7.1.2 | 24 |

| 8.0 - 8.1 | 26 |

| 9.0 | 28 |

| 10.0 | 29 |

在 Android 中,为了确保应用程序能够支持特定版本的操作系统,我们需要在代码中使用编译器指定特定的 API 级别。

compile with 的使用

在 Android Studio 中,你可以通过修改 build.gradle 实现 compile with。build.gradle 是一个 Gradle 基础配置文件,它包含了 Android 应用程序的所有设置和构建信息。简单的来说,这个文件告诉 Gradle 在构建应用程序时需要做什么。typical Android Studio 的 build.gradle 文件如下:

```

apply plugin: 'com.android.application'

android {

compileSdkVersion 29

buildToolsVersion "29.0.3"

defaultConfig {

applicationId "com.example.myapplication"

minSdkVersion 21

targetSdkVersion 29

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_8

targetCompatibility JavaVersion.VERSION_1_8

}

lintOptions {

checkReleaseBuilds false

abortOnError false

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.2.0'

implementation 'com.google.android.material:material:1.2.1'

implementation 'androidx.constraintlayout:constraintlayout:2.0.1'

testImplementation 'junit:junit:4.+'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

```

其中,compileSdkVersion、minSdkVersion 和 targetSdkVersion 就是配置 compile with 的主要选项。具体来说,compileSdkVersion 指定的版本号将决定你的应用能够使用哪些接口;minSdkVersion 和 targetSdkVersion 指定的版本将分别决定你的应用必须支持的最低的 Android 版本和目标版本。

总结

compile with 是安卓开发中非常基础而重要的一个概念,当你正在开发一款 Android 应用程序时,它将影响你应用的运行环境和最终的版本发布。因此,在编写应用程序时,请务必留意你所选定的 API 级别,以确保你应用可以顺利运行。

川公网安备 51019002001728号