com.android.builder
Class AndroidBuilder

java.lang.Object
  extended by com.android.builder.AndroidBuilder

public class AndroidBuilder
extends java.lang.Object

This is the main builder class. It is given all the data to process the build (such as DefaultProductFlavors, DefaultBuildType and dependencies) and use them when doing specific build steps. To use: create a builder with AndroidBuilder(SdkParser, String, ILogger, boolean) then build steps can be done with processManifest(java.io.File, java.util.List, java.util.List, String, int, String, int, int, String) processTestManifest(String, int, int, String, String, Boolean, Boolean, java.util.List, String) #processResources(java.io.File, java.io.File, java.io.File, java.util.List, String, String, String, String, String, com.android.builder.VariantConfiguration.Type, boolean, com.android.builder.model.AaptOptions) compileAllAidlFiles(java.util.List, java.io.File, java.util.List, com.android.builder.compiling.DependencyFileProcessor) convertByteCode(Iterable, Iterable, File, DexOptions, boolean) packageApk(String, String, java.util.List, String, java.util.Collection, java.util.Set, boolean, com.android.builder.model.SigningConfig, String) Java compilation is not handled but the builder provides the bootclasspath with getBootClasspath(SdkParser).


Constructor Summary
AndroidBuilder(SdkParser sdkParser, java.lang.String createdBy, com.android.utils.ILogger logger, boolean verboseExec)
          Creates an AndroidBuilder

This receives an SdkParser to provide the build with information about the SDK, as well as an ILogger to display output.

 
Method Summary
 void compileAidlFile(java.io.File aidlFile, java.io.File sourceOutputDir, java.util.List<java.io.File> importFolders, DependencyFileProcessor dependencyFileProcessor)
          Compiles the given aidl file.
 void compileAllAidlFiles(java.util.List<java.io.File> sourceFolders, java.io.File sourceOutputDir, java.util.List<java.io.File> importFolders, DependencyFileProcessor dependencyFileProcessor)
          Compiles all the aidl files found in the given source folders.
 void compileAllRenderscriptFiles(java.util.List<java.io.File> sourceFolders, java.util.List<java.io.File> importFolders, java.io.File sourceOutputDir, java.io.File resOutputDir, java.io.File objOutputDir, java.io.File libOutputDir, int targetApi, boolean debugBuild, int optimLevel, boolean ndkMode, boolean supportMode, java.util.Set<java.lang.String> abiFilters)
          Compiles all the renderscript files found in the given source folders.
 void convertByteCode(java.lang.Iterable<java.io.File> inputs, java.lang.Iterable<java.io.File> preDexedLibraries, java.io.File outDexFile, DexOptions dexOptions, boolean incremental)
          Converts the bytecode to Dalvik format
static com.android.builder.model.ClassField createClassField(java.lang.String type, java.lang.String name, java.lang.String value)
           
 com.android.ide.common.internal.AaptRunner getAaptRunner()
          Returns an AaptRunner able to run aapt commands.
static java.util.List<java.lang.String> getBootClasspath(SdkParser sdkParser)
          Helper method to get the boot classpath to be used during compilation.
 com.android.ide.common.internal.CommandLineRunner getCommandLineRunner()
           
 java.util.Set<java.io.File> getCompileClasspath(VariantConfiguration variantConfiguration)
          Returns the compile classpath for this config.
 java.util.List<java.io.File> getLeafFolders(java.lang.String extension, java.util.List<java.io.File>... importFolders)
          Computes and returns the leaf folders based on a given file extension.
 java.util.List<java.io.File> getPackagedJars(VariantConfiguration variantConfiguration)
          Returns the list of packaged jars for this config.
 java.io.File getSupportNativeLibFolder()
           
 boolean isBuildingLibrary()
          Sets whether this builder is currently used to build a library
 boolean isInsertSourceMarkers()
          Returns whether we should insert source markers in generated files (such as XML resources and merged manifest files)
 void packageApk(java.lang.String androidResPkgLocation, java.lang.String classesDexLocation, java.util.List<java.io.File> packagedJars, java.lang.String javaResourcesLocation, java.util.Collection<java.io.File> jniLibsFolders, java.util.Set<java.lang.String> abiFilters, boolean jniDebugBuild, com.android.builder.model.SigningConfig signingConfig, java.lang.String outApkLocation)
          Packages the apk.
 void preDexLibrary(java.io.File inputFile, java.io.File outFile, DexOptions dexOptions)
          Converts the bytecode to Dalvik format
 void processManifest(java.io.File mainManifest, java.util.List<java.io.File> manifestOverlays, java.util.List<? extends ManifestDependency> libraries, java.lang.String packageOverride, int versionCode, java.lang.String versionName, int minSdkVersion, int targetSdkVersion, java.lang.String outManifestLocation)
          Merges all the manifests into a single manifest
 void processResources(java.io.File manifestFile, java.io.File resFolder, java.io.File assetsDir, java.util.List<? extends SymbolFileProvider> libraries, java.lang.String packageForR, java.lang.String sourceOutputDir, java.lang.String symbolOutputDir, java.lang.String resPackageOutput, java.lang.String proguardOutput, VariantConfiguration.Type type, boolean debuggable, com.android.builder.model.AaptOptions options, java.util.Collection<java.lang.String> resourceConfigs)
          Process the resources and generate R.java and/or the packaged resources.
 void processTestManifest(java.lang.String testPackageName, int minSdkVersion, int targetSdkVersion, java.lang.String testedPackageName, java.lang.String instrumentationRunner, java.lang.Boolean handleProfiling, java.lang.Boolean functionalTest, java.util.List<? extends ManifestDependency> libraries, java.lang.String outManifestLocation)
          Creates the manifest for a test variant
 AndroidBuilder setBuildingLibrary(boolean library)
          Sets whether this builder is currently used to build a library.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AndroidBuilder

public AndroidBuilder(@NonNull
                      SdkParser sdkParser,
                      @Nullable
                      java.lang.String createdBy,
                      @NonNull
                      com.android.utils.ILogger logger,
                      boolean verboseExec)
Creates an AndroidBuilder

This receives an SdkParser to provide the build with information about the SDK, as well as an ILogger to display output.

verboseExec is needed on top of the ILogger due to remote exec tools not being able to output info and verbose messages separately.

Parameters:
sdkParser - the SdkParser
logger - the Logger
verboseExec - whether external tools are launched in verbose mode
Method Detail

getBootClasspath

@NonNull
public static java.util.List<java.lang.String> getBootClasspath(@NonNull
                                                                        SdkParser sdkParser)
Helper method to get the boot classpath to be used during compilation.


setBuildingLibrary

public AndroidBuilder setBuildingLibrary(boolean library)
Sets whether this builder is currently used to build a library. Defaults to false.


isBuildingLibrary

public boolean isBuildingLibrary()
Sets whether this builder is currently used to build a library


getCompileClasspath

@NonNull
public java.util.Set<java.io.File> getCompileClasspath(@NonNull
                                                               VariantConfiguration variantConfiguration)
Returns the compile classpath for this config. If the config tests a library, this will include the classpath of the tested config

Returns:
a non null, but possibly empty set.

getPackagedJars

@NonNull
public java.util.List<java.io.File> getPackagedJars(@NonNull
                                                            VariantConfiguration variantConfiguration)
Returns the list of packaged jars for this config. If the config tests a library, this will include the jars of the tested config

Returns:
a non null, but possibly empty list.

getSupportNativeLibFolder

@NonNull
public java.io.File getSupportNativeLibFolder()

getAaptRunner

@NonNull
public com.android.ide.common.internal.AaptRunner getAaptRunner()
Returns an AaptRunner able to run aapt commands.

Returns:
an AaptRunner object

getCommandLineRunner

@NonNull
public com.android.ide.common.internal.CommandLineRunner getCommandLineRunner()

createClassField

@NonNull
public static com.android.builder.model.ClassField createClassField(@NonNull
                                                                            java.lang.String type,
                                                                            @NonNull
                                                                            java.lang.String name,
                                                                            @NonNull
                                                                            java.lang.String value)

processManifest

public void processManifest(@NonNull
                            java.io.File mainManifest,
                            @NonNull
                            java.util.List<java.io.File> manifestOverlays,
                            @NonNull
                            java.util.List<? extends ManifestDependency> libraries,
                            java.lang.String packageOverride,
                            int versionCode,
                            java.lang.String versionName,
                            int minSdkVersion,
                            int targetSdkVersion,
                            @NonNull
                            java.lang.String outManifestLocation)
Merges all the manifests into a single manifest

Parameters:
mainManifest - The main manifest of the application.
manifestOverlays - manifest overlays coming from flavors and build types
libraries - the library dependency graph
packageOverride - a package name override. Can be null.
versionCode - a version code to inject in the manifest or -1 to do nothing.
versionName - a version name to inject in the manifest or null to do nothing.
minSdkVersion - a minSdkVersion to inject in the manifest or -1 to do nothing.
targetSdkVersion - a targetSdkVersion to inject in the manifest or -1 to do nothing.
outManifestLocation - the output location for the merged manifest
See Also:
VariantConfiguration.getMainManifest(), VariantConfiguration.getManifestOverlays(), VariantConfiguration.getDirectLibraries(), VariantConfiguration.getMergedFlavor(), DefaultProductFlavor.getVersionCode(), DefaultProductFlavor.getVersionName(), DefaultProductFlavor.getMinSdkVersion(), DefaultProductFlavor.getTargetSdkVersion()

processTestManifest

public void processTestManifest(@NonNull
                                java.lang.String testPackageName,
                                int minSdkVersion,
                                int targetSdkVersion,
                                @NonNull
                                java.lang.String testedPackageName,
                                @NonNull
                                java.lang.String instrumentationRunner,
                                @NonNull
                                java.lang.Boolean handleProfiling,
                                @NonNull
                                java.lang.Boolean functionalTest,
                                @NonNull
                                java.util.List<? extends ManifestDependency> libraries,
                                @NonNull
                                java.lang.String outManifestLocation)
Creates the manifest for a test variant

Parameters:
testPackageName - the package name of the test application
minSdkVersion - the minSdkVersion of the test application
targetSdkVersion - the targetSdkVersion of the test application
testedPackageName - the package name of the tested application
instrumentationRunner - the name of the instrumentation runner
handleProfiling - whether or not the Instrumentation object will turn profiling on and off
functionalTest - whether or not the Instrumentation class should run as a functional test
libraries - the library dependency graph
outManifestLocation - the output location for the merged manifest
See Also:
VariantConfiguration.getPackageName(), VariantConfiguration.getTestedConfig(), VariantConfiguration.getMinSdkVersion(), VariantConfiguration.getTestedPackageName(), VariantConfiguration.getInstrumentationRunner(), VariantConfiguration.getHandleProfiling(), VariantConfiguration.getFunctionalTest(), VariantConfiguration.getDirectLibraries()

isInsertSourceMarkers

public boolean isInsertSourceMarkers()
Returns whether we should insert source markers in generated files (such as XML resources and merged manifest files)

Returns:
true to generate source comments

processResources

public void processResources(@NonNull
                             java.io.File manifestFile,
                             @NonNull
                             java.io.File resFolder,
                             @Nullable
                             java.io.File assetsDir,
                             @NonNull
                             java.util.List<? extends SymbolFileProvider> libraries,
                             @Nullable
                             java.lang.String packageForR,
                             @Nullable
                             java.lang.String sourceOutputDir,
                             @Nullable
                             java.lang.String symbolOutputDir,
                             @Nullable
                             java.lang.String resPackageOutput,
                             @Nullable
                             java.lang.String proguardOutput,
                             VariantConfiguration.Type type,
                             boolean debuggable,
                             @NonNull
                             com.android.builder.model.AaptOptions options,
                             @NonNull
                             java.util.Collection<java.lang.String> resourceConfigs)
                      throws java.io.IOException,
                             java.lang.InterruptedException,
                             com.android.ide.common.internal.LoggedErrorException
Process the resources and generate R.java and/or the packaged resources.

Parameters:
manifestFile - the location of the manifest file
resFolder - the merged res folder
assetsDir - the merged asset folder
libraries - the flat list of libraries
packageForR - Package override to generate the R class in a different package.
sourceOutputDir - optional source folder to generate R.java
resPackageOutput - optional filepath for packaged resources
proguardOutput - optional filepath for proguard file to generate
type - the type of the variant being built
debuggable - whether the app is debuggable
options - the AaptOptions
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

compileAllAidlFiles

public void compileAllAidlFiles(@NonNull
                                java.util.List<java.io.File> sourceFolders,
                                @NonNull
                                java.io.File sourceOutputDir,
                                @NonNull
                                java.util.List<java.io.File> importFolders,
                                @Nullable
                                DependencyFileProcessor dependencyFileProcessor)
                         throws java.io.IOException,
                                java.lang.InterruptedException,
                                com.android.ide.common.internal.LoggedErrorException
Compiles all the aidl files found in the given source folders.

Parameters:
sourceFolders - all the source folders to find files to compile
sourceOutputDir - the output dir in which to generate the source code
importFolders - import folders
dependencyFileProcessor - the dependencyFileProcessor to record the dependencies of the compilation.
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

compileAidlFile

public void compileAidlFile(@NonNull
                            java.io.File aidlFile,
                            @NonNull
                            java.io.File sourceOutputDir,
                            @NonNull
                            java.util.List<java.io.File> importFolders,
                            @Nullable
                            DependencyFileProcessor dependencyFileProcessor)
                     throws java.io.IOException,
                            java.lang.InterruptedException,
                            com.android.ide.common.internal.LoggedErrorException
Compiles the given aidl file.

Parameters:
aidlFile - the AIDL file to compile
sourceOutputDir - the output dir in which to generate the source code
importFolders - all the import folders, including the source folders.
dependencyFileProcessor - the dependencyFileProcessor to record the dependencies of the compilation.
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

compileAllRenderscriptFiles

public void compileAllRenderscriptFiles(@NonNull
                                        java.util.List<java.io.File> sourceFolders,
                                        @NonNull
                                        java.util.List<java.io.File> importFolders,
                                        @NonNull
                                        java.io.File sourceOutputDir,
                                        @NonNull
                                        java.io.File resOutputDir,
                                        @NonNull
                                        java.io.File objOutputDir,
                                        @NonNull
                                        java.io.File libOutputDir,
                                        int targetApi,
                                        boolean debugBuild,
                                        int optimLevel,
                                        boolean ndkMode,
                                        boolean supportMode,
                                        @Nullable
                                        java.util.Set<java.lang.String> abiFilters)
                                 throws java.io.IOException,
                                        java.lang.InterruptedException,
                                        com.android.ide.common.internal.LoggedErrorException
Compiles all the renderscript files found in the given source folders. Right now this is the only way to compile them as the renderscript compiler requires all renderscript files to be passed for all compilation. Therefore whenever a renderscript file or header changes, all must be recompiled.

Parameters:
sourceFolders - all the source folders to find files to compile
importFolders - all the import folders.
sourceOutputDir - the output dir in which to generate the source code
resOutputDir - the output dir in which to generate the bitcode file
targetApi - the target api
debugBuild - whether the build is debug
optimLevel - the optimization level
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

getLeafFolders

@NonNull
public java.util.List<java.io.File> getLeafFolders(@NonNull
                                                           java.lang.String extension,
                                                           java.util.List<java.io.File>... importFolders)
Computes and returns the leaf folders based on a given file extension. This looks through all the given root import folders, and recursively search for leaf folders containing files matching the given extensions. All the leaf folders are gathered and returned in the list.

Parameters:
extension - the extension to search for.
importFolders - an array of list of root folders.
Returns:
a list of leaf folder, never null.

convertByteCode

public void convertByteCode(@NonNull
                            java.lang.Iterable<java.io.File> inputs,
                            @NonNull
                            java.lang.Iterable<java.io.File> preDexedLibraries,
                            @NonNull
                            java.io.File outDexFile,
                            @NonNull
                            DexOptions dexOptions,
                            boolean incremental)
                     throws java.io.IOException,
                            java.lang.InterruptedException,
                            com.android.ide.common.internal.LoggedErrorException
Converts the bytecode to Dalvik format

Parameters:
inputs - the input files
preDexedLibraries - the list of pre-dexed libraries
outDexFile - the location of the output classes.dex file
dexOptions - dex options
incremental - true if it should attempt incremental dex if applicable
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

preDexLibrary

public void preDexLibrary(@NonNull
                          java.io.File inputFile,
                          @NonNull
                          java.io.File outFile,
                          @NonNull
                          DexOptions dexOptions)
                   throws java.io.IOException,
                          java.lang.InterruptedException,
                          com.android.ide.common.internal.LoggedErrorException
Converts the bytecode to Dalvik format

Parameters:
inputFile - the input file
outFile - the location of the output classes.dex file
dexOptions - dex options
Throws:
java.io.IOException
java.lang.InterruptedException
com.android.ide.common.internal.LoggedErrorException

packageApk

public void packageApk(@NonNull
                       java.lang.String androidResPkgLocation,
                       @NonNull
                       java.lang.String classesDexLocation,
                       @NonNull
                       java.util.List<java.io.File> packagedJars,
                       @Nullable
                       java.lang.String javaResourcesLocation,
                       @Nullable
                       java.util.Collection<java.io.File> jniLibsFolders,
                       @Nullable
                       java.util.Set<java.lang.String> abiFilters,
                       boolean jniDebugBuild,
                       @Nullable
                       com.android.builder.model.SigningConfig signingConfig,
                       @NonNull
                       java.lang.String outApkLocation)
                throws DuplicateFileException,
                       java.io.FileNotFoundException,
                       KeytoolException,
                       PackagerException,
                       SigningException
Packages the apk.

Parameters:
androidResPkgLocation - the location of the packaged resource file
classesDexLocation - the location of the classes.dex file
packagedJars - the jars that are packaged (libraries + jar dependencies)
javaResourcesLocation - the processed Java resource folder
jniLibsFolders - the folders containing jni shared libraries
abiFilters - optional ABI filter
jniDebugBuild - whether the app should include jni debug data
signingConfig - the signing configuration
outApkLocation - location of the APK.
Throws:
DuplicateFileException
java.io.FileNotFoundException - if the store location was not found
KeytoolException
PackagerException
SigningException - when the key cannot be read from the keystore
See Also:
VariantConfiguration.getPackagedJars()