public abstract class BaseExtension extends java.lang.Object implements AndroidConfig
You don't use this plugin directly. Instead, use one of the following:
AppExtension
: outputs the com.android.application
plugin you use to create
an Android app module.
LibraryExtension
: outputs the com.android.library
plugin you use to create an Android
library module.
TestExtension
: outputs the com.android.test
plugin you use to create an
Android test module.
FeatureExtension
: outputs the com.android.feature
plugin you use to create
a feature module for your Android Instant
Apps.
The following applies the Android plugin to an app's module-level build.gradle
file:
// Applies the application plugin and makes the 'android' block available to specify // Android-specific build options. apply plugin: 'com.android.application'
To learn more about creating and organizing Android projects, read Projects Overview.
Modifier and Type | Field and Description |
---|---|
protected com.android.build.gradle.internal.scope.GlobalScope |
globalScope |
protected org.gradle.api.logging.Logger |
logger |
protected org.gradle.api.Project |
project |
Modifier and Type | Method and Description |
---|---|
void |
aaptOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.AaptOptions> action)
Specifies options for the Android Asset Packaging Tool (AAPT).
|
void |
adbOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.AdbOptions> action)
Specifies options for the Android Debug Bridge
(ADB), such as APK installation options.
|
abstract void |
addVariant(BaseVariant variant) |
void |
buildToolsVersion(java.lang.String version) |
void |
buildTypes(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.BuildType>> action)
Encapsulates all build type configurations for this project.
|
protected void |
checkWritability() |
void |
compileOptions(org.gradle.api.Action<com.android.build.gradle.internal.CompileOptions> action)
Specifies Java compiler options, such as the language level of the Java source code and
generated bytecode.
|
void |
compileSdkVersion(int apiLevel) |
void |
compileSdkVersion(java.lang.String version) |
void |
dataBinding(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DataBindingOptions> action)
Specifies options for the Data Binding
Library.
|
void |
defaultConfig(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DefaultConfig> action)
Specifies defaults for variant properties that the Android plugin applies to all build
variants.
|
void |
defaultPublishConfig(java.lang.String value) |
void |
deviceProvider(com.android.builder.testing.api.DeviceProvider deviceProvider) |
void |
dexOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DexOptions> action)
Specifies options for the DEX tool, such as enabling library pre-dexing.
|
void |
disableWrite()
Disallow further modification on the extension.
|
void |
externalNativeBuild(org.gradle.api.Action<com.android.build.gradle.internal.dsl.ExternalNativeBuild> action)
|
void |
flavorDimensions(java.lang.String... dimensions)
Specifies the names of product flavor dimensions for this project.
|
void |
generatePureSplits(boolean flag) |
com.android.build.gradle.internal.dsl.AaptOptions |
getAaptOptions()
Specifies options for the Android Asset Packaging Tool (AAPT).
|
java.io.File |
getAdbExe()
Deprecated.
|
java.io.File |
getAdbExecutable()
Returns a path to the Android Debug Bridge
(ADB) executable from the Android SDK.
|
com.android.build.gradle.internal.dsl.AdbOptions |
getAdbOptions()
Specifies APK install options for the Android Debug Bridge
(ADB).
|
java.util.Collection<java.lang.String> |
getAidlPackageWhiteList()
Aidl files to package in the aar.
|
java.lang.Boolean |
getBaseFeature()
Whether the feature module is the base feature.
|
java.util.List<java.io.File> |
getBootClasspath()
Returns the list of files that form bootClasspath used for compilation.
|
org.gradle.api.NamedDomainObjectContainer<BaseVariantOutput> |
getBuildOutputs()
All build outputs for all variants, can be used by users to customize a build output.
|
com.android.repository.Revision |
getBuildToolsRevision()
This property is for internal use only.
|
java.lang.String |
getBuildToolsVersion()
Specifies the version of the SDK Build Tools to
use when building your project.
|
org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.BuildType> |
getBuildTypes()
Encapsulates all build type configurations for this project.
|
com.android.build.gradle.internal.CompileOptions |
getCompileOptions()
Specifies Java compiler options, such as the language level of the Java source code and
generated bytecode.
|
java.lang.String |
getCompileSdkVersion()
Specifies the API level to compile your project against.
|
com.android.build.gradle.internal.dsl.DataBindingOptions |
getDataBinding()
Specifies options for the Data Binding
Library.
|
com.android.build.gradle.internal.dsl.DefaultConfig |
getDefaultConfig()
Specifies defaults for variant properties that the Android plugin applies to all build
variants.
|
java.io.File |
getDefaultProguardFile(java.lang.String name) |
java.lang.String |
getDefaultPublishConfig()
Specifies the version of the module to publish externally.
|
java.util.List<com.android.builder.testing.api.DeviceProvider> |
getDeviceProviders()
List of device providers
|
com.android.build.gradle.internal.dsl.DexOptions |
getDexOptions()
Specifies options for the DEX tool, such as enabling library pre-dexing.
|
com.android.build.gradle.internal.dsl.ExternalNativeBuild |
getExternalNativeBuild()
|
java.util.List<java.lang.String> |
getFlavorDimensionList()
Specifies the names of product flavor dimensions for this project.
|
boolean |
getGeneratePureSplits()
Specifies whether to build APK splits or multiple APKs from configurations in the
splits block. |
com.android.build.gradle.internal.coverage.JacocoOptions |
getJacoco()
Configure JaCoCo version that is used for offline instrumentation and coverage report.
|
java.util.Collection<com.android.builder.core.LibraryRequest> |
getLibraryRequests() |
com.android.build.gradle.internal.dsl.LintOptions |
getLintOptions()
Specifies options for the lint tool.
|
java.io.File |
getNdkDirectory()
Returns the path to the Android
NDK that Gradle uses for this project.
|
java.lang.String |
getNdkVersion()
Name of the NDK version that will be used when building native code.
|
java.lang.Boolean |
getPackageBuildConfig()
Whether to package build config class file.
|
com.android.build.gradle.internal.dsl.PackagingOptions |
getPackagingOptions()
Specifies options and rules that determine which files the Android plugin packages into your
APK.
|
org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.ProductFlavor> |
getProductFlavors()
Encapsulates all product flavors configurations for this project.
|
java.lang.String |
getResourcePrefix()
Specifies the module's resource prefix to Android Studio for editor features, such as Lint
checks.
|
java.io.File |
getSdkDirectory()
Returns the path to the Android SDK that Gradle uses for this project.
|
org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.SigningConfig> |
getSigningConfigs()
Encapsulates signing configurations that you can apply to
BuildType and ProductFlavor configurations. |
org.gradle.api.NamedDomainObjectContainer<AndroidSourceSet> |
getSourceSets()
Encapsulates source set configurations for all variants.
|
com.android.build.gradle.internal.dsl.Splits |
getSplits()
Specifies configurations for building multiple
APKs or APK splits.
|
com.android.build.gradle.internal.dsl.TestOptions |
getTestOptions()
Specifies options for how the Android plugin should run local and instrumented tests.
|
java.util.List<com.android.builder.testing.api.TestServer> |
getTestServers()
List of remote CI servers.
|
java.util.List<com.android.build.api.transform.Transform> |
getTransforms() |
java.util.List<java.util.List<java.lang.Object>> |
getTransformsDependencies() |
org.gradle.api.Action<com.android.build.api.variant.VariantFilter> |
getVariantFilter()
Specifies variants the Android plugin should include or remove from your Gradle project.
|
void |
jacoco(org.gradle.api.Action<com.android.build.gradle.internal.coverage.JacocoOptions> action)
Deprecated.
|
void |
lintOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.LintOptions> action)
Specifies options for the lint tool.
|
void |
packagingOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.PackagingOptions> action)
Specifies options and rules that determine which files the Android plugin packages into your
APK.
|
void |
productFlavors(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.ProductFlavor>> action)
Encapsulates all product flavors configurations for this project.
|
void |
registerArtifactType(java.lang.String name,
boolean isTest,
int artifactType) |
void |
registerBuildTypeSourceProvider(java.lang.String name,
com.android.build.gradle.internal.dsl.BuildType buildType,
com.android.builder.model.SourceProvider sourceProvider) |
void |
registerJavaArtifact(java.lang.String name,
BaseVariant variant,
java.lang.String assembleTaskName,
java.lang.String javaCompileTaskName,
java.util.Collection<java.io.File> generatedSourceFolders,
java.lang.Iterable<java.lang.String> ideSetupTaskNames,
org.gradle.api.artifacts.Configuration configuration,
java.io.File classesFolder,
java.io.File javaResourceFolder,
com.android.builder.model.SourceProvider sourceProvider) |
void |
registerMultiFlavorSourceProvider(java.lang.String name,
java.lang.String flavorName,
com.android.builder.model.SourceProvider sourceProvider) |
void |
registerProductFlavorSourceProvider(java.lang.String name,
com.android.build.gradle.internal.dsl.ProductFlavor productFlavor,
com.android.builder.model.SourceProvider sourceProvider) |
void |
registerTransform(com.android.build.api.transform.Transform transform,
java.lang.Object... dependencies) |
void |
resourcePrefix(java.lang.String prefix) |
void |
setBuildToolsVersion(java.lang.String version) |
void |
setCompileSdkVersion(int apiLevel) |
void |
setCompileSdkVersion(java.lang.String target) |
void |
setDefaultPublishConfig(java.lang.String value) |
void |
setGeneratePureSplits(boolean flag) |
void |
setNdkVersion(java.lang.String version) |
void |
setPublishNonDefault(boolean publishNonDefault) |
void |
setVariantFilter(org.gradle.api.Action<com.android.build.api.variant.VariantFilter> filter) |
void |
signingConfigs(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.SigningConfig>> action)
Encapsulates signing configurations that you can apply to
BuildType and ProductFlavor configurations. |
void |
sourceSets(org.gradle.api.Action<org.gradle.api.NamedDomainObjectContainer<AndroidSourceSet>> action)
Encapsulates source set configurations for all variants.
|
void |
splits(org.gradle.api.Action<com.android.build.gradle.internal.dsl.Splits> action)
Specifies configurations for building multiple
APKs or APK splits.
|
void |
testOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.TestOptions> action)
Specifies options for how the Android plugin should run local and instrumented tests.
|
void |
testServer(com.android.builder.testing.api.TestServer testServer) |
void |
useLibrary(java.lang.String name)
Includes the specified library to the classpath.
|
void |
useLibrary(java.lang.String name,
boolean required)
/** Includes the specified library to the classpath.
|
void |
variantFilter(org.gradle.api.Action<com.android.build.api.variant.VariantFilter> filter) |
static com.android.builder.model.SourceProvider |
wrapJavaSourceSet(org.gradle.api.tasks.SourceSet sourceSet) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getTestBuildType
protected final com.android.build.gradle.internal.scope.GlobalScope globalScope
protected org.gradle.api.logging.Logger logger
protected org.gradle.api.Project project
public void disableWrite()
protected void checkWritability()
public void compileSdkVersion(java.lang.String version)
getCompileSdkVersion()
public void compileSdkVersion(int apiLevel)
getCompileSdkVersion()
public void setCompileSdkVersion(int apiLevel)
public void setCompileSdkVersion(java.lang.String target)
public void useLibrary(java.lang.String name)
You typically use this property to support optional platform libraries that ship with the Android SDK. The following sample adds the Apache HTTP API library to the project classpath:
android { // Adds a platform library that ships with the Android SDK. useLibrary 'org.apache.http.legacy' }
To include libraries that do not ship with the SDK, such as local library modules or
binaries from remote repositories, add the libraries as
dependencies in the dependencies
block. Note that Android plugin 3.0.0 and
later introduce new
dependency configurations. To learn more about Gradle dependencies, read Dependency
Management Basics.
name
- the name of the library.public void useLibrary(java.lang.String name, boolean required)
You typically use this property to support optional platform libraries that ship with the Android SDK. The following sample adds the Apache HTTP API library to the project classpath:
android { // Adds a platform library that ships with the Android SDK. useLibrary 'org.apache.http.legacy' }
To include libraries that do not ship with the SDK, such as local library modules or
binaries from remote repositories, add the libraries as
dependencies in the dependencies
block. Note that Android plugin 3.0.0 and
later introduce new
dependency configurations. To learn more about Gradle dependencies, read Dependency
Management Basics.
name
- the name of the library.required
- if using the library requires a manifest entry, the entry will indicate that
the library is not required.public void buildToolsVersion(java.lang.String version)
public java.lang.String getNdkVersion()
The value null means that no particular NDK version is requested. In this case, the latest available NDK will be used.
getNdkVersion
in interface AndroidConfig
public void setNdkVersion(@Nullable java.lang.String version)
public java.lang.String getBuildToolsVersion()
When using Android plugin 3.0.0 or later, configuring this property is optional. By default, the plugin uses the minimum version of the build tools required by the version of the plugin you're using. To specify a different version of the build tools for the plugin to use, specify the version as follows:
// Specifying this property is optional. buildToolsVersion "26.0.0"
For a list of build tools releases, read the release notes.
Note that the value assigned to this property is parsed and stored in a normalized form, so reading it back may give a slightly different result.
getBuildToolsVersion
in interface AndroidConfig
public void setBuildToolsVersion(java.lang.String version)
public void buildTypes(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.BuildType>> action)
For more information about the properties you can configure in this block, see BuildType
.
public void productFlavors(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.ProductFlavor>> action)
For more information about the properties you can configure in this block, see ProductFlavor
public void signingConfigs(org.gradle.api.Action<? super org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.SigningConfig>> action)
BuildType
and ProductFlavor
configurations.
For more information about the properties you can configure in this block, see SigningConfig
public void flavorDimensions(java.lang.String... dimensions)
When configuring product flavors with Android plugin 3.0.0 and higher, you must specify at
least one flavor dimension, using the
flavorDimensions
property, and then assign each flavor to a dimension.
Otherwise, you will get the following build error:
Error:All flavors must now belong to a named flavor dimension. The flavor 'flavor_name' is not assigned to a flavor dimension.
By default, when you specify only one dimension, all flavors you configure automatically belong to that dimension. If you specify more than one dimension, you need to manually assign each flavor to a dimension, as shown in the sample below.
Flavor dimensions allow you to create groups of product flavors that you can combine with flavors from other flavor dimensions. For example, you can have one dimension that includes a 'free' and 'paid' version of your app, and another dimension for flavors that support different API levels, such as 'minApi21' and 'minApi24'. The Android plugin can then combine flavors from these dimensions—including their settings, code, and resources—to create variants such as 'debugFreeMinApi21' and 'releasePaidMinApi24', and so on. The sample below shows you how to specify flavor dimensions and add product flavors to them.
android { ... // Specifies the flavor dimensions you want to use. The order in which you // list each dimension determines its priority, from highest to lowest, // when Gradle merges variant sources and configurations. You must assign // each product flavor you configure to one of the flavor dimensions. flavorDimensions 'api', 'version' productFlavors { demo { // Assigns this product flavor to the 'version' flavor dimension. dimension 'version' ... } full { dimension 'version' ... } minApi24 { // Assigns this flavor to the 'api' dimension. dimension 'api' minSdkVersion '24' versionNameSuffix "-minApi24" ... } minApi21 { dimension "api" minSdkVersion '21' versionNameSuffix "-minApi21" ... } } }
To learn more, read Combine multiple flavors.
public void sourceSets(org.gradle.api.Action<org.gradle.api.NamedDomainObjectContainer<AndroidSourceSet>> action)
Note that the Android plugin uses its own implementation of source sets. For more
information about the properties you can configure in this block, see AndroidSourceSet
.
public org.gradle.api.NamedDomainObjectContainer<AndroidSourceSet> getSourceSets()
The Android plugin looks for your project's source code and resources in groups of
directories called source sets.
Each source set also determines the scope of build outputs that should consume its code and
resources. For example, when creating a new project from Android Studio, the IDE creates
directories for a main/
source set that contains the code and resources you want
to share between all your build variants.
You can then define basic functionality in the main/
source set, but use
product flavor source sets to change only the branding of your app between different clients,
or include special permissions and logging functionality to only "debug" versions of your
app.
The Android plugin expects you to organize files for source set directories a certain way,
similar to the main/
source set. For example, Gradle expects Java class files
that are specific to your "debug" build type to be located in the src/debug/java/
directory.
Gradle provides a useful task to shows you how to organize your files for each build type-, product flavor-, and build variant-specific source set. you can run this task from the command line as follows:
./gradlew sourceSets
The following sample output describes where Gradle expects to find certain files for the "debug" build type:
------------------------------------------------------------ Project :app ------------------------------------------------------------ ... debug ---- Compile configuration: compile build.gradle name: android.sourceSets.debug Java sources: [app/src/debug/java] Manifest file: app/src/debug/AndroidManifest.xml Android resources: [app/src/debug/res] Assets: [app/src/debug/assets] AIDL sources: [app/src/debug/aidl] RenderScript sources: [app/src/debug/rs] JNI sources: [app/src/debug/jni] JNI libraries: [app/src/debug/jniLibs] Java-style resources: [app/src/debug/resources]
If you have sources that are not organized into the default source set directories that
Gradle expects, as described in the sample output above, you can use the sourceSet
block to change where Gradle looks to gather files for each component of a given
source set. You don't need to relocate the files; you only need to provide Gradle with the
path(s), relative to the module-level build.gradle
file, where Gradle should
expect to find files for each source set component.
Note: You should specify only static paths whenever possible. Specifying dynamic paths reduces build speed and consistency.
The following code sample maps sources from the app/other/
directory to
certain components of the main
source set and changes the root directory of the
androidTest
source set:
android { ... sourceSets { // Encapsulates configurations for the main source set. main { // Changes the directory for Java sources. The default directory is // 'src/main/java'. java.srcDirs = ['other/java'] // If you list multiple directories, Gradle uses all of them to collect // sources. Because Gradle gives these directories equal priority, if // you define the same resource in more than one directory, you get an // error when merging resources. The default directory is 'src/main/res'. res.srcDirs = ['other/res1', 'other/res2'] // Note: You should avoid specifying a directory which is a parent to one // or more other directories you specify. For example, avoid the following: // res.srcDirs = ['other/res1', 'other/res1/layouts', 'other/res1/strings'] // You should specify either only the root 'other/res1' directory, or only the // nested 'other/res1/layouts' and 'other/res1/strings' directories. // For each source set, you can specify only one Android manifest. // By default, Android Studio creates a manifest for your main source // set in the src/main/ directory. manifest.srcFile 'other/AndroidManifest.xml' ... } // Create additional blocks to configure other source sets. androidTest { // If all the files for a source set are located under a single root // directory, you can specify that directory using the setRoot property. // When gathering sources for the source set, Gradle looks only in locations // relative to the root directory you specify. For example, after applying the // configuration below for the androidTest source set, Gradle looks for Java // sources only in the src/tests/java/ directory. setRoot 'src/tests' ... } } }
getSourceSets
in interface AndroidConfig
AndroidSourceSetFactory
public org.gradle.api.NamedDomainObjectContainer<BaseVariantOutput> getBuildOutputs()
getBuildOutputs
in interface AndroidConfig
public void defaultConfig(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DefaultConfig> action)
You can override any defaultConfig
property when
configuring product flavors.
For more information about the properties you can configure in this block, see ProductFlavor
.
public void aaptOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.AaptOptions> action)
For more information about the properties you can configure in this block, see AaptOptions
.
public void dexOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DexOptions> action)
For more information about the properties you can configure in this block, see DexOptions
.
public void lintOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.LintOptions> action)
For more information about the properties you can configure in this block, see LintOptions
.
public void externalNativeBuild(org.gradle.api.Action<com.android.build.gradle.internal.dsl.ExternalNativeBuild> action)
public void testOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.TestOptions> action)
For more information about the properties you can configure in this block, see TestOptions
.
public void compileOptions(org.gradle.api.Action<com.android.build.gradle.internal.CompileOptions> action)
For more information about the properties you can configure in this block, see CompileOptions
.
public void packagingOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.PackagingOptions> action)
For more information about the properties you can configure in this block, see PackagingOptions
.
@Deprecated public void jacoco(org.gradle.api.Action<com.android.build.gradle.internal.coverage.JacocoOptions> action)
To specify the version of JaCoCo you want to use, you now need to include it as a
buildscript dependency in your project-level build.gradle
file, as follows:
buildscript { dependencies { classpath "org.jacoco:org.jacoco.core:<jacoco-version>" ... } }
public void adbOptions(org.gradle.api.Action<com.android.build.gradle.internal.dsl.AdbOptions> action)
For more information about the properties you can configure in this block, see AdbOptions
.
public void splits(org.gradle.api.Action<com.android.build.gradle.internal.dsl.Splits> action)
For more information about the properties you can configure in this block, see Splits
.
public void dataBinding(org.gradle.api.Action<com.android.build.gradle.internal.dsl.DataBindingOptions> action)
For more information about the properties you can configure in this block, see DataBindingOptions
.
public com.android.build.gradle.internal.dsl.DataBindingOptions getDataBinding()
Data binding helps you write declarative layouts and minimize the glue code necessary to bind your application logic and layouts.
getDataBinding
in interface AndroidConfig
public void deviceProvider(com.android.builder.testing.api.DeviceProvider deviceProvider)
@NonNull public java.util.List<com.android.builder.testing.api.DeviceProvider> getDeviceProviders()
AndroidConfig
getDeviceProviders
in interface AndroidConfig
public void testServer(com.android.builder.testing.api.TestServer testServer)
@NonNull public java.util.List<com.android.builder.testing.api.TestServer> getTestServers()
AndroidConfig
getTestServers
in interface AndroidConfig
public void registerTransform(@NonNull com.android.build.api.transform.Transform transform, java.lang.Object... dependencies)
@NonNull public java.util.List<com.android.build.api.transform.Transform> getTransforms()
getTransforms
in interface AndroidConfig
@NonNull public java.util.List<java.util.List<java.lang.Object>> getTransformsDependencies()
getTransformsDependencies
in interface AndroidConfig
public org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.ProductFlavor> getProductFlavors()
Product flavors represent different versions of your project that you expect to co-exist on a single device, the Google Play store, or repository. For example, you can configure 'demo' and 'full' product flavors for your app, and each of those flavors can specify different features, device requirements, resources, and application ID's--while sharing common source code and resources. So, product flavors allow you to output different versions of your project by simply changing only the components and settings that are different between them.
Configuring product flavors is similar to configuring
build types: add them to the productFlavors
block of your module's
build.gradle
file and configure the settings you want. Product flavors support the
same properties as the DefaultConfig
block--this is because defaultConfig
defines a ProductFlavor
object that the plugin uses as the base
configuration for all other flavors. Each flavor you configure can then override any of the
default values in defaultConfig
, such as the applicationId
.
When using Android plugin 3.0.0 and higher, each flavor must belong to a
flavorDimensions
value. By default, when you specify only one
dimension, all flavors you configure belong to that dimension. If you specify more than one
flavor dimension, you need to manually assign each flavor to a dimension. To learn more, read
Use Flavor Dimensions for variant-aware dependency management.
When you configure product flavors, the Android plugin automatically combines them with
your BuildType
configurations to create build
variants. If the plugin creates certain build variants that you don't want, you can filter
variants.
getProductFlavors
in interface AndroidConfig
ProductFlavor
public org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.BuildType> getBuildTypes()
Unlike using ProductFlavor
to create
different versions of your project that you expect to co-exist on a single device, build
types determine how Gradle builds and packages each version of your project. Developers
typically use them to configure projects for various stages of a development lifecycle. For
example, when creating a new project from Android Studio, the Android plugin configures a
'debug' and 'release' build type for you. By default, the 'debug' build type enables
debugging options and signs your APK with a generic debug keystore. Conversely, The 'release'
build type strips out debug symbols and requires you to create a
release key and keystore for your app. You can then combine build types with product
flavors to create
build variants.
getBuildTypes
in interface AndroidConfig
BuildType
public org.gradle.api.NamedDomainObjectContainer<com.android.build.gradle.internal.dsl.SigningConfig> getSigningConfigs()
BuildType
and ProductFlavor
configurations.
Android requires that all APKs be digitally signed with a certificate before they can be
installed onto a device. When deploying a debug version of your project from Android Studio,
the Android plugin automatically signs your APK with a generic debug certificate. However, to
build an APK for release, you must sign the APK with a
release key and keystore. You can do this by either using the
Android Studio UI or manually configuring
your build.gradle
file.
getSigningConfigs
in interface AndroidConfig
SigningConfig
public void defaultPublishConfig(java.lang.String value)
public java.lang.String getDefaultPublishConfig()
If you don't configure this property, the Android plugin publishes the release version of the module by default. If the module configures product flavors, you need to configure this property with the name of the variant you want the plugin to publish, as shown below:
// Specifies the 'demoDebug' build variant as the default variant // that the plugin should publish to external consumers. defaultPublishConfig 'demoDebug'
If you plan to only consume your library module locally, you do not need to configure this property. Android plugin 3.0.0 and higher use variant-aware dependency resolution to automatically match the variant of the producer to that of the consumer. That is, when publishing a module to another local module, the plugin no longer respects this property when determining which version of the module to publish to the consumer.
getDefaultPublishConfig
in interface AndroidConfig
public void setDefaultPublishConfig(java.lang.String value)
public void setPublishNonDefault(boolean publishNonDefault)
public void variantFilter(org.gradle.api.Action<com.android.build.api.variant.VariantFilter> filter)
public void setVariantFilter(org.gradle.api.Action<com.android.build.api.variant.VariantFilter> filter)
public org.gradle.api.Action<com.android.build.api.variant.VariantFilter> getVariantFilter()
By default, the Android plugin creates a build variant for every possible combination of
the product flavors and build types that you configure, and adds them to your Gradle project.
However, there may be certain build variants that either you do not need or do not make sense
in the context of your project. You can remove certain build variant configurations by creating
a variant filter in your module-level build.gradle
file.
The following example tells the plugin to ignore all variants that combine the "dev" product flavor, which you can configure to optimize build speeds during development, and the "release" build type:
android { ... variantFilter { variant -> def buildTypeName = variant.buildType*.name def flavorName = variant.flavors*.name if (flavorName.contains("dev") && buildTypeName.contains("release")) { // Tells Gradle to ignore each variant that satisfies the conditions above. setIgnore(true) } } }
During subsequent builds, Gradle ignores any build variants that meet the conditions you specify. If you're using Android Studio, those variants no longer appear in the drop down menu when you click Build > Select Build Variant from the menu bar.
getVariantFilter
in interface AndroidConfig
VariantFilter
public com.android.build.gradle.internal.dsl.AdbOptions getAdbOptions()
getAdbOptions
in interface AndroidConfig
AdbOptions
public java.lang.String getResourcePrefix()
Including unique prefixes for module resources helps avoid naming collisions with
resources from other modules. For example, when creating a library with String resources, you
may want to name each resource with a unique prefix, such as "mylib_"
to avoid
naming collisions with similar resources that the consumer defines. You can then specify this
prefix, as shown below, so that Android Studio expects this prefix when you name module
resources:
// This property is useful only when developing your project in Android Studio. resourcePrefix 'mylib_'
getResourcePrefix
in interface AndroidConfig
public java.util.List<java.lang.String> getFlavorDimensionList()
To configure flavor dimensions, use
flavorDimensions
. To learn more, read combine
multiple product flavors.
getFlavorDimensionList
in interface AndroidConfig
@Incubating public boolean getGeneratePureSplits()
splits
block.
When you set this property to true
, the Android plugin generates each object
in the splits
block as a portion of a
whole APK, called an APK split. Compared to building multiple APKs, each APK split
includes only the components that each ABI or screen density requires. Generating APK splits
is an incubating feature, which requires you to set BaseFlavor.minSdkVersion(int)
to 21
or
higher, and is currently supported only when publishing Android Instant Apps.
When you do not configure this property or set it to false
(default), the
Android plugin builds separate APKs for each object you configure in the splits
block that you can deploy to a device.
To learn more about building different versions of your app that each target a different Application Binary Interfaces
or screen density, read Build Multiple
APKs.
getGeneratePureSplits
in interface AndroidConfig
public void resourcePrefix(java.lang.String prefix)
public abstract void addVariant(BaseVariant variant)
public void registerArtifactType(@NonNull java.lang.String name, boolean isTest, int artifactType)
public void registerBuildTypeSourceProvider(@NonNull java.lang.String name, @NonNull com.android.build.gradle.internal.dsl.BuildType buildType, @NonNull com.android.builder.model.SourceProvider sourceProvider)
public void registerProductFlavorSourceProvider(@NonNull java.lang.String name, @NonNull com.android.build.gradle.internal.dsl.ProductFlavor productFlavor, @NonNull com.android.builder.model.SourceProvider sourceProvider)
public void registerJavaArtifact(@NonNull java.lang.String name, @NonNull BaseVariant variant, @NonNull java.lang.String assembleTaskName, @NonNull java.lang.String javaCompileTaskName, @NonNull java.util.Collection<java.io.File> generatedSourceFolders, @NonNull java.lang.Iterable<java.lang.String> ideSetupTaskNames, @NonNull org.gradle.api.artifacts.Configuration configuration, @NonNull java.io.File classesFolder, @NonNull java.io.File javaResourceFolder, @Nullable com.android.builder.model.SourceProvider sourceProvider)
public void registerMultiFlavorSourceProvider(@NonNull java.lang.String name, @NonNull java.lang.String flavorName, @NonNull com.android.builder.model.SourceProvider sourceProvider)
@NonNull public static com.android.builder.model.SourceProvider wrapJavaSourceSet(@NonNull org.gradle.api.tasks.SourceSet sourceSet)
public java.lang.String getCompileSdkVersion()
This means your code can use only the Android APIs included in that API level and lower.
You can configure the compile sdk version by adding the following to the android
block: compileSdkVersion 26
.
You should generally use the most up-to-date API level available. If you are planning to also support older API levels, it's good practice to use the Lint tool to check if you are using APIs that are not available in earlier API levels.
The value you assign to this property is parsed and stored in a normalized form, so reading it back may return a slightly different value.
getCompileSdkVersion
in interface AndroidConfig
@Internal @NonNull public com.android.repository.Revision getBuildToolsRevision()
To specify the version of the SDK Build Tools that the Android plugin should use, use buildToolsVersion instead.
getBuildToolsRevision
in interface AndroidConfig
public java.util.Collection<com.android.builder.core.LibraryRequest> getLibraryRequests()
getLibraryRequests
in interface AndroidConfig
public java.io.File getSdkDirectory()
To learn more about downloading and installing the Android SDK, read Update Your Tools with the SDK Manager.
public java.io.File getNdkDirectory()
You can install the Android NDK by either using the SDK manager or downloading the standalone NDK package.
public java.util.List<java.io.File> getBootClasspath()
AndroidConfig
getBootClasspath
in interface AndroidConfig
public java.io.File getAdbExecutable()
@Deprecated public java.io.File getAdbExe()
getAdbExecutable()
.public java.io.File getDefaultProguardFile(java.lang.String name)
public void generatePureSplits(boolean flag)
public void setGeneratePureSplits(boolean flag)
public com.android.build.gradle.internal.dsl.DefaultConfig getDefaultConfig()
You can override any defaultConfig
property when configuring
product flavors.
getDefaultConfig
in interface AndroidConfig
ProductFlavor
public com.android.build.gradle.internal.dsl.AaptOptions getAaptOptions()
getAaptOptions
in interface AndroidConfig
AaptOptions
public com.android.build.gradle.internal.CompileOptions getCompileOptions()
getCompileOptions
in interface AndroidConfig
CompileOptions
public com.android.build.gradle.internal.dsl.DexOptions getDexOptions()
Experimenting with DEX options tailored for your workstation may improve build performance. To learn more, read Optimize your build.
getDexOptions
in interface AndroidConfig
DexOptions
public com.android.build.gradle.internal.coverage.JacocoOptions getJacoco()
To specify the version of JaCoCo you want to use, add the following to build.gradle
file:
android { jacoco { version "<jacoco-version>" } }
getJacoco
in interface AndroidConfig
public com.android.build.gradle.internal.dsl.LintOptions getLintOptions()
Android Studio and the Android SDK provide a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code without having to execute the app or write test cases. Each problem the tool detects is reported with a description message and a severity level, so that you can quickly prioritize the critical improvements that need to be made.
This property allows you to configure certain lint options, such as which checks to run or ignore. If you're using Android Studio, you can configure similar lint options from the IDE. To learn more about using and running lint, read Improve Your Code with Lint.
getLintOptions
in interface AndroidConfig
LintOptions
public com.android.build.gradle.internal.dsl.ExternalNativeBuild getExternalNativeBuild()
When using Android Studio 2.2 or higher with Android plugin 2.2.0 or higher, you can compile C and C++ code into a native library that Gradle packages into your APK.
To learn more, read Add C and C++ Code to Your Project.
getExternalNativeBuild
in interface AndroidConfig
ExternalNativeBuild
public com.android.build.gradle.internal.dsl.PackagingOptions getPackagingOptions()
For example, the following example tells the plugin to avoid packaging files that are intended only for testing:
packagingOptions { // Tells the plugin to not include any files in the 'testing-data/' directory, // which is specified as an absolute path from the root of the APK archive. // The exclude property includes certain defaults paths to help you avoid common // duplicate file errors when building projects with multiple dependencies. exclude "/testing-data/**" }
To learn more about how to specify rules for packaging, merging, and excluding files, see
PackagingOptions
getPackagingOptions
in interface AndroidConfig
PackagingOptions
public com.android.build.gradle.internal.dsl.Splits getSplits()
To generate APK splits, you need to also set
generatePureSplits
to true
. However, generating APK splits is
an incubating feature, which requires you to set BaseFlavor.minSdkVersion(int)
to 21
or
higher, and is currently supported only when publishing Android Instant Apps.
getSplits
in interface AndroidConfig
Splits
public com.android.build.gradle.internal.dsl.TestOptions getTestOptions()
To learn more, read Configure Gradle test options.
getTestOptions
in interface AndroidConfig
TestOptions
public java.lang.Boolean getPackageBuildConfig()
AndroidConfig
getPackageBuildConfig
in interface AndroidConfig
public java.util.Collection<java.lang.String> getAidlPackageWhiteList()
AndroidConfig
getAidlPackageWhiteList
in interface AndroidConfig
public java.lang.Boolean getBaseFeature()
AndroidConfig
getBaseFeature
in interface AndroidConfig