pluginManagement{/** * The pluginManagement.repositories block configures the * repositories Gradle uses to search or download the Gradle plugins and * their transitive dependencies. Gradle pre-configures support for remote * repositories such as JCenter, Maven Central, and Ivy. You can also use * local repositories or define your own remote repositories. Here we * define the Gradle Plugin Portal, Google's Maven repository, * and the Maven Central Repository as the repositories Gradle should use to look for its * dependencies. */repositories{gradlePluginPortal()google()mavenCentral()}}dependencyResolutionManagement{/** * The dependencyResolutionManagement.repositories * block is where you configure the repositories and dependencies used by * all modules in your project, such as libraries that you are using to * create your application. However, you should configure module-specific * dependencies in each module-level build.gradle file. For new projects, * Android Studio includes Google's Maven repository and the Maven Central * Repository by default, but it does not configure any dependencies (unless * you select a template that requires some). */repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories{google()mavenCentral()}}rootProject.name="My Application"include(":app")
Groovy
pluginManagement{/** * The pluginManagement.repositories block configures the * repositories Gradle uses to search or download the Gradle plugins and * their transitive dependencies. Gradle pre-configures support for remote * repositories such as JCenter, Maven Central, and Ivy. You can also use * local repositories or define your own remote repositories. Here we * define the Gradle Plugin Portal, Google's Maven repository, * and the Maven Central Repository as the repositories Gradle should use to look for its * dependencies. */repositories{gradlePluginPortal()google()mavenCentral()}}dependencyResolutionManagement{/** * The dependencyResolutionManagement.repositories * block is where you configure the repositories and dependencies used by * all modules in your project, such as libraries that you are using to * create your application. However, you should configure module-specific * dependencies in each module-level build.gradle file. For new projects, * Android Studio includes Google's Maven repository and the Maven Central * Repository by default, but it does not configure any dependencies (unless * you select a template that requires some). */repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories{google()mavenCentral()}}rootProject.name="My Application"include':app'
plugins{/** * Use `apply false` in the top-level build.gradle file to add a Gradle * plugin as a build dependency but not apply it to the current (root) * project. Don't use `apply false` in sub-projects. For more information, * see Applying external plugins with same version to subprojects. */id("com.android.application")version"8.9.0"applyfalseid("com.android.library")version"8.9.0"applyfalseid("org.jetbrains.kotlin.android")version"2.1.10"applyfalse}
Groovy
plugins{/** * Use `apply false` in the top-level build.gradle file to add a Gradle * plugin as a build dependency but not apply it to the current (root) * project. Don't use `apply false` in sub-projects. For more information, * see Applying external plugins with same version to subprojects. */id'com.android.application'version'8.9.0'applyfalseid'com.android.library'version'8.9.0'applyfalseid'org.jetbrains.kotlin.android'version'2.1.10'applyfalse}
如需支持较低版本的 Android,您可能需要在代码中添加更多条件检查,或更多地使用 AndroidX 兼容性库。您应将支持较低版本的维护费用与仍在使用这些较低版本的用户百分比进行权衡。如需了解当前的版本使用百分比,请参阅 Android Studio 的“New Project”向导中的版本图表。
在 Android Studio 中修改代码或在构建期间运行检查时,lint 会针对您使用的 minSdk 中不提供的 API 发出警告。您应通过将较新功能设为基于条件的或使用 Appcompat 实现向后兼容性来解决这些问题。
targetSdk
targetSdk 有以下两种用途:
它设置应用的运行时行为。
它用于证明您已针对哪个 Android 版本进行了测试。
如果您的应用在使用高于 targetSdk 的 Android 版本的设备上运行,Android 会在兼容模式下运行您的应用,该模式的行为与 targetSdk 中所指示的较低版本类似。例如,当 API 23 引入运行时权限模型时,并非所有应用都已准备好立即采用该模型。
通过将 targetSdk 设置为 22,这些应用无需使用运行时权限即可在 API 23 设备上运行,并且可以使用最新 compileSdk 版本中包含的功能。Google Play 分发政策会强制执行针对目标 API 级别的其他政策。
targetSdk 的值必须小于或等于 compileSdk 的值。
注意:compileSdk 和 targetSdk 的值不必相同。请谨记以下基本原则:
通过 compileSdk,您可以使用新的 API
targetSdk 用于设置应用的运行时行为
targetSdk 必须小于或等于 compileSdk
应用模块 build 脚本示例
以下 Android 应用模块 build 脚本示例简要说明了一些基础 DSL 元素和设置:
Kotlin
/** * The first section in the build configuration applies the Android Gradle plugin * to this build and makes the android block available to specify * Android-specific build options. */plugins{id("com.android.application")}/** * Locate (and possibly download) a JDK used to build your kotlin * source code. This also acts as a default for sourceCompatibility, * targetCompatibility and jvmTarget. Note that this does not affect which JDK * is used to run the Gradle build itself, and does not need to take into * account the JDK version required by Gradle plugins (such as the * Android Gradle Plugin) */kotlin{jvmToolchain(11)}/** * The android block is where you configure all your Android-specific * build options. */android{/** * The app's namespace. Used primarily to access app resources. */namespace="com.example.myapp"/** * compileSdk specifies the Android API level Gradle should use to * compile your app. This means your app can use the API features included in * this API level and lower. */compileSdk=33/** * The defaultConfig block encapsulates default settings and entries for all * build variants and can override some attributes in main/AndroidManifest.xml * dynamically from the build system. You can configure product flavors to override * these values for different versions of your app. */defaultConfig{// Uniquely identifies the package for publishing.applicationId="com.example.myapp"// Defines the minimum API level required to run the app.minSdk=21// Specifies the API level used to test the app.targetSdk=33// Defines the version number of your app.versionCode=1// Defines a user-friendly version name for your app.versionName="1.0"}/** * The buildTypes block is where you can configure multiple build types. * By default, the build system defines two build types: debug and release. The * debug build type is not explicitly shown in the default build configuration, * but it includes debugging tools and is signed with the debug key. The release * build type applies ProGuard settings and is not signed by default. */buildTypes{/** * By default, Android Studio configures the release build type to enable code * shrinking, using minifyEnabled, and specifies the default ProGuard rules file. */getByName("release"){isMinifyEnabled=true// Enables code shrinking for the release build type.proguardFiles(getDefaultProguardFile("proguard-android.txt"),"proguard-rules.pro")}}/** * The productFlavors block is where you can configure multiple product flavors. * This lets you create different versions of your app that can * override the defaultConfig block with their own settings. Product flavors * are optional, and the build system does not create them by default. * * This example creates a free and paid product flavor. Each product flavor * then specifies its own application ID, so that they can exist on the Google * Play Store or an Android device simultaneously. * * If you declare product flavors, you must also declare flavor dimensions * and assign each flavor to a flavor dimension. */flavorDimensions+="tier"productFlavors{create("free"){dimension="tier"applicationId="com.example.myapp.free"}create("paid"){dimension="tier"applicationId="com.example.myapp.paid"}}/** * To override source and target compatibility (if different from the * toolchain JDK version), add the following. All of these * default to the same value as kotlin.jvmToolchain. If you're using the * same version for these values and kotlin.jvmToolchain, you can * remove these blocks. *///compileOptions {// sourceCompatibility = JavaVersion.VERSION_11// targetCompatibility = JavaVersion.VERSION_11//}//kotlinOptions {// jvmTarget = "11"//}}/** * The dependencies block in the module-level build configuration file * specifies dependencies required to build only the module itself. * To learn more, go to Add build dependencies. */dependencies{implementation(project(":lib"))implementation("androidx.appcompat:appcompat:1.7.0")implementation(fileTree(mapOf("dir"to"libs","include"tolistOf("*.jar"))))}
Groovy
/** * The first line in the build configuration applies the Android Gradle plugin * to this build and makes the android block available to specify * Android-specific build options. */plugins{id'com.android.application'}/** * Locate (and possibly download) a JDK used to build your kotlin * source code. This also acts as a default for sourceCompatibility, * targetCompatibility and jvmTarget. Note that this does not affect which JDK * is used to run the Gradle build itself, and does not need to take into * account the JDK version required by Gradle plugins (such as the * Android Gradle Plugin) */kotlin{jvmToolchain11}/** * The android block is where you configure all your Android-specific * build options. */android{/** * The app's namespace. Used primarily to access app resources. */namespace'com.example.myapp'/** * compileSdk specifies the Android API level Gradle should use to * compile your app. This means your app can use the API features included in * this API level and lower. */compileSdk33/** * The defaultConfig block encapsulates default settings and entries for all * build variants and can override some attributes in main/AndroidManifest.xml * dynamically from the build system. You can configure product flavors to override * these values for different versions of your app. */defaultConfig{// Uniquely identifies the package for publishing.applicationId'com.example.myapp'// Defines the minimum API level required to run the app.minSdk21// Specifies the API level used to test the app.targetSdk33// Defines the version number of your app.versionCode1// Defines a user-friendly version name for your app.versionName"1.0"}/** * The buildTypes block is where you can configure multiple build types. * By default, the build system defines two build types: debug and release. The * debug build type is not explicitly shown in the default build configuration, * but it includes debugging tools and is signed with the debug key. The release * build type applies ProGuard settings and is not signed by default. */buildTypes{/** * By default, Android Studio configures the release build type to enable code * shrinking, using minifyEnabled, and specifies the default ProGuard rules file. */release{minifyEnabledtrue// Enables code shrinking for the release build type.proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'}}/** * The productFlavors block is where you can configure multiple product flavors. * This lets you create different versions of your app that can * override the defaultConfig block with their own settings. Product flavors * are optional, and the build system does not create them by default. * * This example creates a free and paid product flavor. Each product flavor * then specifies its own application ID, so that they can exist on the Google * Play Store or an Android device simultaneously. * * If you declare product flavors, you must also declare flavor dimensions * and assign each flavor to a flavor dimension. */flavorDimensions"tier"productFlavors{free{dimension"tier"applicationId'com.example.myapp.free'}paid{dimension"tier"applicationId'com.example.myapp.paid'}}/** * To override source and target compatibility (if different from the * tool chain JDK version), add the following. All of these * default to the same value as kotlin.jvmToolchain. If you're using the * same version for these values and kotlin.jvmToolchain, you can * remove these blocks. *///compileOptions {// sourceCompatibility JavaVersion.VERSION_11// targetCompatibility JavaVersion.VERSION_11//}//kotlinOptions {// jvmTarget = '11'//}}/** * The dependencies block in the module-level build configuration file * specifies dependencies required to build only the module itself. * To learn more, go to Add build dependencies. */dependencies{implementationproject(":lib")implementation'androidx.appcompat:appcompat:1.7.0'implementationfileTree(dir:'libs',include:['*.jar'])}