com.android.tools.lint.detector.api
Class Project

java.lang.Object
  extended by com.android.tools.lint.detector.api.Project

@Beta
public class Project
extends java.lang.Object

A project contains information about an Android project being scanned for Lint errors.

NOTE: This is not a public or final API; if you rely on this be prepared to adjust your code for the next tools release.


Field Summary
protected  java.util.List<Project> mAllLibraries
           
protected  java.lang.Boolean mAppCompat
           
protected  int mBuildSdk
           
protected  LintClient mClient
           
protected  Configuration mConfiguration
           
protected  java.io.File mDir
           
protected  java.util.List<Project> mDirectLibraries
           
protected  java.util.List<java.io.File> mFiles
          If non null, specifies a non-empty list of specific files under this project which should be checked.
protected  java.util.List<java.io.File> mGradleFiles
           
protected  java.lang.Boolean mGradleProject
           
protected  java.util.List<java.io.File> mJavaClassFolders
           
protected  java.util.List<java.io.File> mJavaLibraries
           
protected  java.util.List<java.io.File> mJavaSourceFolders
           
protected  boolean mLibrary
           
protected  java.util.List<java.io.File> mManifestFiles
           
protected  com.android.sdklib.AndroidVersion mManifestMinSdk
           
protected  com.android.sdklib.AndroidVersion mManifestTargetSdk
           
protected  boolean mMergeManifests
           
protected  java.lang.String mName
           
protected  java.lang.String mPackage
           
protected  java.util.List<java.io.File> mProguardFiles
           
protected  java.lang.String mProguardPath
           
protected  java.io.File mReferenceDir
           
protected  boolean mReportIssues
           
protected  java.util.List<java.io.File> mResourceFolders
           
protected  SdkInfo mSdkInfo
          The SDK info, if any
protected  java.lang.Boolean mSupportLib
           
protected  com.android.sdklib.IAndroidTarget mTarget
           
 
Constructor Summary
protected Project(LintClient client, java.io.File dir, java.io.File referenceDir)
          Creates a new Project.
 
Method Summary
 void addFile(java.io.File file)
          Adds the given file to the list of files which should be checked in this project.
static Project create(LintClient client, java.io.File dir, java.io.File referenceDir)
          Creates a new Project for the given directory.
 java.lang.Boolean dependsOn(java.lang.String artifact)
          Returns true if this project depends on the given artifact.
 boolean equals(java.lang.Object obj)
           
 java.util.List<Project> getAllLibraries()
          Returns the transitive closure of the library projects for this project
 java.util.List<java.lang.String> getApplicableDensities()
          Returns the set of applicable densities for this project.
 int getBuildSdk()
          Returns the target API used to build the project, or -1 if not known
 com.android.sdklib.IAndroidTarget getBuildTarget()
          Returns the target used to build the project, or null if not known
 Configuration getConfiguration()
          Gets the configuration associated with this project
 com.android.builder.model.Variant getCurrentVariant()
          Returns the current selected variant, if any (and if the current project is a Gradle project).
 java.io.File getDir()
          Returns the project root directory
 java.util.List<Project> getDirectLibraries()
          Returns the list of library projects referenced by this project
 java.lang.String getDisplayPath(java.io.File file)
          Returns the relative path of a given file relative to the user specified directory (which is often the project directory but sometimes a higher up directory when a directory tree is being scanned
 java.util.List<java.io.File> getGradleBuildScripts()
          Returns the Gradle build script files configured for this project, if any
 com.android.builder.model.AndroidLibrary getGradleLibraryModel()
          Returns the project model for this project if it corresponds to a Gradle library.
 com.android.builder.model.AndroidProject getGradleProjectModel()
          Returns the project model for this project if it corresponds to a Gradle project.
 java.util.List<java.io.File> getJavaClassFolders()
          Returns the list of output folders for class files
 java.util.List<java.io.File> getJavaLibraries()
          Returns the list of Java libraries (typically .jar files) that this project depends on.
 java.util.List<java.io.File> getJavaSourceFolders()
          Returns the list of source folders for Java source files
 java.util.List<java.io.File> getManifestFiles()
          Gets the paths to the manifest files in this project, if any exists.
 int getMinSdk()
          Returns the minimum API level requested by the manifest, or -1 if not specified.
 com.android.sdklib.AndroidVersion getMinSdkVersion()
          Returns the minimum API level for the project
 java.lang.String getName()
          Returns the name of the project
 java.lang.String getPackage()
          Returns the application package specified by the manifest
 java.util.List<java.io.File> getProguardFiles()
          Returns the proguard files configured for this project, if any
 java.io.File getReferenceDir()
          Returns the original user supplied directory where the lint search started.
 java.lang.String getRelativePath(java.io.File file)
          Returns the relative path of a given file within the current project.
 boolean getReportIssues()
          Returns whether lint should report issues in this project.
 java.util.List<java.io.File> getResourceFolders()
          Returns the resource folder.
 SdkInfo getSdkInfo()
          Gets the SDK info for the current project.
 java.util.List<java.io.File> getSubset()
          The list of files to be checked in this project.
 java.util.Map<java.lang.String,java.lang.String> getSuperClassMap()
          Returns a super class map for this project.
 int getTargetSdk()
          Returns the target API level specified by the manifest, or -1 if not specified.
 com.android.sdklib.AndroidVersion getTargetSdkVersion()
          Returns the target API level for the project
 int hashCode()
           
protected  void initialize()
           
 boolean isAndroidProject()
          Returns true if this project is an Android project.
static boolean isAospFrameworksProject(java.io.File dir)
          Is this the frameworks AOSP project? Needs some hardcoded support since it doesn't have a manifest file, etc.
 boolean isGradleProject()
          Returns true if this project is a Gradle-based Android project
 boolean isLibrary()
          Returns true if this project is an Android library project
 boolean isMergingManifests()
          Returns whether manifest merging is in effect
 void readManifest(org.w3c.dom.Document document)
          Initialized the manifest state from the given manifest model
 void setName(java.lang.String name)
          Sets the name of the project
 void setReportIssues(boolean reportIssues)
          Sets whether lint should report issues in this project.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

mClient

protected final LintClient mClient

mDir

protected final java.io.File mDir

mReferenceDir

protected final java.io.File mReferenceDir

mConfiguration

protected Configuration mConfiguration

mPackage

protected java.lang.String mPackage

mBuildSdk

protected int mBuildSdk

mTarget

protected com.android.sdklib.IAndroidTarget mTarget

mManifestMinSdk

protected com.android.sdklib.AndroidVersion mManifestMinSdk

mManifestTargetSdk

protected com.android.sdklib.AndroidVersion mManifestTargetSdk

mLibrary

protected boolean mLibrary

mName

protected java.lang.String mName

mProguardPath

protected java.lang.String mProguardPath

mMergeManifests

protected boolean mMergeManifests

mSdkInfo

protected SdkInfo mSdkInfo
The SDK info, if any


mFiles

protected java.util.List<java.io.File> mFiles
If non null, specifies a non-empty list of specific files under this project which should be checked.


mProguardFiles

protected java.util.List<java.io.File> mProguardFiles

mGradleFiles

protected java.util.List<java.io.File> mGradleFiles

mManifestFiles

protected java.util.List<java.io.File> mManifestFiles

mJavaSourceFolders

protected java.util.List<java.io.File> mJavaSourceFolders

mJavaClassFolders

protected java.util.List<java.io.File> mJavaClassFolders

mJavaLibraries

protected java.util.List<java.io.File> mJavaLibraries

mResourceFolders

protected java.util.List<java.io.File> mResourceFolders

mDirectLibraries

protected java.util.List<Project> mDirectLibraries

mAllLibraries

protected java.util.List<Project> mAllLibraries

mReportIssues

protected boolean mReportIssues

mGradleProject

protected java.lang.Boolean mGradleProject

mSupportLib

protected java.lang.Boolean mSupportLib

mAppCompat

protected java.lang.Boolean mAppCompat
Constructor Detail

Project

protected Project(@NonNull
                  LintClient client,
                  @NonNull
                  java.io.File dir,
                  @NonNull
                  java.io.File referenceDir)
Creates a new Project. Use one of the factory methods to create.

Method Detail

create

@NonNull
public static Project create(@NonNull
                                     LintClient client,
                                     @NonNull
                                     java.io.File dir,
                                     @NonNull
                                     java.io.File referenceDir)
Creates a new Project for the given directory.

Parameters:
client - the tool running the lint check
dir - the root directory of the project
referenceDir - See getReferenceDir().
Returns:
a new Project

isGradleProject

public boolean isGradleProject()
Returns true if this project is a Gradle-based Android project

Returns:
true if this is a Gradle-based project

isAndroidProject

public boolean isAndroidProject()
Returns true if this project is an Android project.

Returns:
true if this project is an Android project.

getGradleProjectModel

@Nullable
public com.android.builder.model.AndroidProject getGradleProjectModel()
Returns the project model for this project if it corresponds to a Gradle project. This is the case if isGradleProject() is true and isLibrary() is false.

Returns:
the project model, or null

getGradleLibraryModel

@Nullable
public com.android.builder.model.AndroidLibrary getGradleLibraryModel()
Returns the project model for this project if it corresponds to a Gradle library. This is the case if both isGradleProject() and isLibrary() return true.

Returns:
the project model, or null

getCurrentVariant

@Nullable
public com.android.builder.model.Variant getCurrentVariant()
Returns the current selected variant, if any (and if the current project is a Gradle project). This can be used by incremental lint rules to warn about problems in the current context. Lint rules should however strive to perform cross variant analysis and warn about problems in any configuration.

Returns:
the select variant, or null

initialize

protected void initialize()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(@Nullable
                      java.lang.Object obj)
Overrides:
equals in class java.lang.Object

addFile

public void addFile(@NonNull
                    java.io.File file)
Adds the given file to the list of files which should be checked in this project. If no files are added, the whole project will be checked.

Parameters:
file - the file to be checked

getSubset

@Nullable
public java.util.List<java.io.File> getSubset()
The list of files to be checked in this project. If null, the whole project should be checked.

Returns:
the subset of files to be checked, or null for the whole project

getJavaSourceFolders

@NonNull
public java.util.List<java.io.File> getJavaSourceFolders()
Returns the list of source folders for Java source files

Returns:
a list of source folders to search for .java files

getJavaClassFolders

@NonNull
public java.util.List<java.io.File> getJavaClassFolders()
Returns the list of output folders for class files

Returns:
a list of output folders to search for .class files

getJavaLibraries

@NonNull
public java.util.List<java.io.File> getJavaLibraries()
Returns the list of Java libraries (typically .jar files) that this project depends on. Note that this refers to jar libraries, not Android library projects which are processed in a separate pass with their own source and class folders.

Returns:
a list of .jar files (or class folders) that this project depends on.

getResourceFolders

@NonNull
public java.util.List<java.io.File> getResourceFolders()
Returns the resource folder.

Returns:
a file pointing to the resource folder, or null if the project does not contain any resources

getDisplayPath

@NonNull
public java.lang.String getDisplayPath(@NonNull
                                               java.io.File file)
Returns the relative path of a given file relative to the user specified directory (which is often the project directory but sometimes a higher up directory when a directory tree is being scanned

Parameters:
file - the file under this project to check
Returns:
the path relative to the reference directory (often the project directory)

getRelativePath

@NonNull
public java.lang.String getRelativePath(@NonNull
                                                java.io.File file)
Returns the relative path of a given file within the current project.

Parameters:
file - the file under this project to check
Returns:
the path relative to the project

getDir

@NonNull
public java.io.File getDir()
Returns the project root directory

Returns:
the dir

getReferenceDir

@NonNull
public java.io.File getReferenceDir()
Returns the original user supplied directory where the lint search started. For example, if you run lint against /tmp/foo, and it finds a project to lint in /tmp/foo/dev/src/project1, then the dir is /tmp/foo/dev/src/project1 and the referenceDir is /tmp/foo/.

Returns:
the reference directory, never null

getConfiguration

@NonNull
public Configuration getConfiguration()
Gets the configuration associated with this project

Returns:
the configuration associated with this project

getPackage

@Nullable
public java.lang.String getPackage()
Returns the application package specified by the manifest

Returns:
the application package, or null if unknown

getMinSdkVersion

@NonNull
public com.android.sdklib.AndroidVersion getMinSdkVersion()
Returns the minimum API level for the project

Returns:
the minimum API level or AndroidVersion.DEFAULT if unknown

getMinSdk

public int getMinSdk()
Returns the minimum API level requested by the manifest, or -1 if not specified. Use getMinSdkVersion() to get a full version if you need to check if the platform is a preview platform etc.

Returns:
the minimum API level or -1 if unknown

getTargetSdkVersion

@NonNull
public com.android.sdklib.AndroidVersion getTargetSdkVersion()
Returns the target API level for the project

Returns:
the target API level or AndroidVersion.DEFAULT if unknown

getTargetSdk

public int getTargetSdk()
Returns the target API level specified by the manifest, or -1 if not specified. Use getTargetSdkVersion() to get a full version if you need to check if the platform is a preview platform etc.

Returns:
the target API level or -1 if unknown

getBuildSdk

public int getBuildSdk()
Returns the target API used to build the project, or -1 if not known

Returns:
the build target API or -1 if unknown

getBuildTarget

@Nullable
public com.android.sdklib.IAndroidTarget getBuildTarget()
Returns the target used to build the project, or null if not known

Returns:
the build target, or null

readManifest

public void readManifest(@NonNull
                         org.w3c.dom.Document document)
Initialized the manifest state from the given manifest model

Parameters:
document - the DOM document for the manifest XML document

isLibrary

public boolean isLibrary()
Returns true if this project is an Android library project

Returns:
true if this project is an Android library project

getDirectLibraries

@NonNull
public java.util.List<Project> getDirectLibraries()
Returns the list of library projects referenced by this project

Returns:
the list of library projects referenced by this project, never null

getAllLibraries

@NonNull
public java.util.List<Project> getAllLibraries()
Returns the transitive closure of the library projects for this project

Returns:
the transitive closure of the library projects for this project

getSdkInfo

@NonNull
public SdkInfo getSdkInfo()
Gets the SDK info for the current project.

Returns:
the SDK info for the current project, never null

getManifestFiles

@NonNull
public java.util.List<java.io.File> getManifestFiles()
Gets the paths to the manifest files in this project, if any exists. The manifests should be provided such that the main manifest comes first, then any flavor versions, then any build types.

Returns:
the path to the manifest file, or null if it does not exist

getProguardFiles

@NonNull
public java.util.List<java.io.File> getProguardFiles()
Returns the proguard files configured for this project, if any

Returns:
the proguard files, if any

getGradleBuildScripts

@NonNull
public java.util.List<java.io.File> getGradleBuildScripts()
Returns the Gradle build script files configured for this project, if any

Returns:
the Gradle files, if any

getName

@NonNull
public java.lang.String getName()
Returns the name of the project

Returns:
the name of the project, never null

setName

public void setName(@NonNull
                    java.lang.String name)
Sets the name of the project

Parameters:
name - the name of the project, never null

setReportIssues

public void setReportIssues(boolean reportIssues)
Sets whether lint should report issues in this project. See getReportIssues() for a full description of what that means.

Parameters:
reportIssues - whether lint should report issues in this project

getReportIssues

public boolean getReportIssues()
Returns whether lint should report issues in this project.

If a user specifies a project and its library projects for analysis, then those library projects are all "included", and all errors found in all the projects are reported. But if the user is only running lint on the main project, we shouldn't report errors in any of the library projects. We still need to consider them for certain types of checks, such as determining whether resources found in the main project are unused, so the detectors must still get a chance to look at these projects. The #getReportIssues() attribute is used for this purpose.

Returns:
whether lint should report issues in this project

isMergingManifests

public boolean isMergingManifests()
Returns whether manifest merging is in effect

Returns:
true if manifests in library projects should be merged into main projects

isAospFrameworksProject

public static boolean isAospFrameworksProject(@NonNull
                                              java.io.File dir)
Is this the frameworks AOSP project? Needs some hardcoded support since it doesn't have a manifest file, etc.

Parameters:
dir - the project directory to check
Returns:
true if this looks like the frameworks/base/core project

dependsOn

@Nullable
public java.lang.Boolean dependsOn(@NonNull
                                            java.lang.String artifact)
Returns true if this project depends on the given artifact. Note that the project doesn't have to be a Gradle project; the artifact is just an identifier for name a specific library, such as com.android.support:support-v4 to identify the support library

Parameters:
artifact - the Gradle/Maven name of a library
Returns:
true if the library is installed, false if it is not, and null if we're not sure

getApplicableDensities

@Nullable
public java.util.List<java.lang.String> getApplicableDensities()
Returns the set of applicable densities for this project. If null, there are no density restrictions and all densities apply.

Returns:
the list of specific densities that apply in this project, or null if all densities apply

getSuperClassMap

@NonNull
public java.util.Map<java.lang.String,java.lang.String> getSuperClassMap()
Returns a super class map for this project. The keys and values are internal class names (e.g. java/lang/Integer, not java.lang.Integer).

Returns:
a map, possibly empty but never null