aboutsummaryrefslogtreecommitdiff
path: root/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleProject.java
blob: 79304d8b814ad8549cbc167d43d8ece6da4b5fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package com.android.build.gradle.internal;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.Files;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.builder.model.AndroidArtifact;
import com.android.builder.model.AndroidLibrary;
import com.android.builder.model.AndroidProject;
import com.android.builder.model.BuildTypeContainer;
import com.android.builder.model.Dependencies;
import com.android.builder.model.ProductFlavor;
import com.android.builder.model.ProductFlavorContainer;
import com.android.builder.model.SourceProvider;
import com.android.builder.model.Variant;
import com.android.sdklib.AndroidTargetHash;
import com.android.sdklib.AndroidVersion;
import com.android.tools.lint.detector.api.Project;
import com.android.utils.Pair;
import com.android.utils.XmlUtils;

import org.w3c.dom.Document;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static java.io.File.separatorChar;

/**
 * An implementation of Lint's {@link Project} class wrapping a Gradle model (project or
 * library)
 */
public class LintGradleProject extends Project {
    private LintGradleProject(
            @NonNull LintGradleClient client,
            @NonNull File dir,
            @NonNull File referenceDir,
            @NonNull File manifest) {
        super(client, dir, referenceDir);
        mGradleProject = true;
        mMergeManifests = true;
        mDirectLibraries = Lists.newArrayList();
        readManifest(manifest);
    }

    /**
     * Creates a {@link com.android.build.gradle.internal.LintGradleProject} from
     * the given {@link com.android.builder.model.AndroidProject} definition for
     * a given {@link com.android.builder.model.Variant}, and returns it along with
     * a set of lint custom rule jars applicable for the given model project.
     *
     * @param client the client
     * @param project the model project
     * @param variant the variant
     * @param gradleProject the gradle project
     * @return a pair of new project and list of custom rule jars
     */
    @NonNull
    public static Pair<LintGradleProject, List<File>> create(
            @NonNull LintGradleClient client,
            @NonNull AndroidProject project,
            @NonNull Variant variant,
            @NonNull org.gradle.api.Project gradleProject) {
        File dir = gradleProject.getRootDir();
        AppGradleProject lintProject = new AppGradleProject(client, dir,
                dir, project, variant);

        List<File> customRules = Lists.newArrayList();
        File appLintJar = new File(gradleProject.getBuildDir(),
                "lint" + separatorChar + "lint.jar");
        if (appLintJar.exists()) {
            customRules.add(appLintJar);
        }

        Set<AndroidLibrary> libraries = Sets.newHashSet();
        Dependencies dependencies = variant.getMainArtifact().getDependencies();
        for (AndroidLibrary library : dependencies.getLibraries()) {
            lintProject.addDirectLibrary(createLibrary(client, library, libraries, customRules));
        }

        return Pair.<LintGradleProject,List<File>>of(lintProject, customRules);
    }


    @Override
    protected void initialize() {
        // Deliberately not calling super; that code is for ADT compatibility
    }

    protected void readManifest(File manifest) {
        if (manifest.exists()) {
            try {
                String xml = Files.toString(manifest, Charsets.UTF_8);
                Document document = XmlUtils.parseDocumentSilently(xml, true);
                readManifest(document);
            } catch (IOException e) {
                mClient.log(e, "Could not read manifest %1$s", manifest);
            }
        }
    }

    @Override
    public boolean isGradleProject() {
        return true;
    }

    void addDirectLibrary(@NonNull Project project) {
        mDirectLibraries.add(project);
    }

    @NonNull
    private static LibraryProject createLibrary(@NonNull LintGradleClient client,
            @NonNull AndroidLibrary library,
            @NonNull Set<AndroidLibrary> seen, List<File> customRules) {
        seen.add(library);
        File dir = library.getFolder();
        LibraryProject project = new LibraryProject(client, dir, dir, library);

        File ruleJar = library.getLintJar();
        if (ruleJar.exists()) {
            customRules.add(ruleJar);
        }

        for (AndroidLibrary dependent : library.getLibraryDependencies()) {
            if (!seen.contains(dependent)) {
                project.addDirectLibrary(createLibrary(client, dependent, seen, customRules));
            }
        }

        return project;
    }

    private static class AppGradleProject extends LintGradleProject {
        private AndroidProject mProject;
        private Variant mVariant;
        private List<SourceProvider> mProviders;

        private AppGradleProject(
                @NonNull LintGradleClient client,
                @NonNull File dir,
                @NonNull File referenceDir,
                @NonNull AndroidProject project,
                @NonNull Variant variant) {
            super(client, dir, referenceDir, variant.getMainArtifact().getGeneratedManifest());

            mProject = project;
            mVariant = variant;
        }

        @Override
        public boolean isLibrary() {
            return mProject.isLibrary();
        }

        @Override
        public AndroidProject getGradleProjectModel() {
            return mProject;
        }

        @Override
        public Variant getCurrentVariant() {
            return mVariant;
        }

        private List<SourceProvider> getSourceProviders() {
            if (mProviders == null) {
                List<SourceProvider> providers = Lists.newArrayList();
                AndroidArtifact mainArtifact = mVariant.getMainArtifact();

                SourceProvider defaultProvider = mProject.getDefaultConfig().getSourceProvider();
                if (defaultProvider != null) {
                    providers.add(defaultProvider);
                }

                for (String flavorName : mVariant.getProductFlavors()) {
                    for (ProductFlavorContainer flavor : mProject.getProductFlavors()) {
                        if (flavorName.equals(flavor.getProductFlavor().getName())) {
                            SourceProvider provider = flavor.getSourceProvider();
                            if (provider != null) {
                                providers.add(provider);
                            }
                            break;
                        }
                    }
                }

                SourceProvider multiProvider = mainArtifact.getMultiFlavorSourceProvider();
                if (multiProvider != null) {
                    providers.add(multiProvider);
                }

                String buildTypeName = mVariant.getBuildType();
                for (BuildTypeContainer buildType : mProject.getBuildTypes()) {
                    if (buildTypeName.equals(buildType.getBuildType().getName())) {
                        SourceProvider provider = buildType.getSourceProvider();
                        if (provider != null) {
                            providers.add(provider);
                        }
                        break;
                    }
                }

                SourceProvider variantProvider =  mainArtifact.getVariantSourceProvider();
                if (variantProvider != null) {
                    providers.add(variantProvider);
                }

                mProviders = providers;
            }

            return mProviders;
        }

        @NonNull
        @Override
        public List<File> getManifestFiles() {
            if (mManifestFiles == null) {
                mManifestFiles = Lists.newArrayList();
                for (SourceProvider provider : getSourceProviders()) {
                    File manifestFile = provider.getManifestFile();
                    if (manifestFile.exists()) { // model returns path whether or not it exists
                        mManifestFiles.add(manifestFile);
                    }
                }
            }

            return mManifestFiles;
        }

        @Override
        public List<File> getProguardFiles() {
            if (mProguardFiles == null) {
                ProductFlavor flavor = mProject.getDefaultConfig().getProductFlavor();
                mProguardFiles = Lists.newArrayList();
                mProguardFiles.addAll(flavor.getProguardFiles());
                // TODO: This is currently broken:
                //mProguardFiles.addAll(flavor.getConsumerProguardFiles());
                // It will throw
                //org.gradle.tooling.model.UnsupportedMethodException: Unsupported method: BaseConfig.getConsumerProguardFiles().
                //  The version of Gradle you connect to does not support that method.
                //  To resolve the problem you can change/upgrade the target version of Gradle you connect to.
                //  Alternatively, you can ignore this exception and read other information from the model.
                //at org.gradle.tooling.model.internal.Exceptions.unsupportedMethod(Exceptions.java:33)
                //at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$InvocationHandlerImpl.invoke(ProtocolToModelAdapter.java:239)
                //at com.sun.proxy.$Proxy129.getConsumerProguardFiles(Unknown Source)
            }

            return mProguardFiles;
        }

        @NonNull
        @Override
        public List<File> getResourceFolders() {
            if (mResourceFolders == null) {
                mResourceFolders = Lists.newArrayList();
                for (SourceProvider provider : getSourceProviders()) {
                    Collection<File> resDirs = provider.getResDirectories();
                    for (File res : resDirs) {
                        if (res.exists()) { // model returns path whether or not it exists
                            mResourceFolders.add(res);
                        }
                    }
                }
            }

            return mResourceFolders;
        }

        @NonNull
        @Override
        public List<File> getJavaSourceFolders() {
            if (mJavaSourceFolders == null) {
                mJavaSourceFolders = Lists.newArrayList();
                for (SourceProvider provider : getSourceProviders()) {
                    Collection<File> resDirs = provider.getJavaDirectories();
                    for (File res : resDirs) {
                        if (res.exists()) { // model returns path whether or not it exists
                            mJavaSourceFolders.add(res);
                        }
                    }
                }
            }

            return mJavaSourceFolders;
        }

        @NonNull
        @Override
        public List<File> getJavaClassFolders() {
            if (mJavaClassFolders == null) {
                mJavaClassFolders = new ArrayList<File>(1);
                File outputClassFolder = mVariant.getMainArtifact().getClassesFolder();
                if (outputClassFolder != null && outputClassFolder.exists()) {
                    mJavaClassFolders.add(outputClassFolder);
                }
            }

            return mJavaClassFolders;
        }

        @Override
        public List<File> getJavaLibraries() {
            if (mJavaLibraries == null) {
                Collection<File> jars = mVariant.getMainArtifact().getDependencies().getJars();
                mJavaLibraries = Lists.newArrayListWithExpectedSize(jars.size());
                for (File jar : jars) {
                    if (jar.exists()) {
                        mJavaLibraries.add(jar);
                    }
                }
            }
            return mJavaLibraries;
        }

        @Nullable
        @Override
        public String getPackage() {
            // For now, lint only needs the manifest package; not the potentially variant specific
            // package. As part of the Gradle work on the Lint API we should make two separate
            // package lookup methods -- one for the manifest package, one for the build package
            if (mPackage == null) { // only used as a fallback in case manifest somehow is null
                String packageName = mProject.getDefaultConfig().getProductFlavor().getPackageName();
                if (packageName != null) {
                    return packageName;
                }
            }

            return mPackage; // from manifest
        }

        @Override
        public int getMinSdk() {
            int minSdk = mProject.getDefaultConfig().getProductFlavor().getMinSdkVersion();
            if (minSdk != -1) {
                return minSdk;
            }

            return mMinSdk; // from manifest
        }

        @Override
        public int getTargetSdk() {
            int targetSdk = mProject.getDefaultConfig().getProductFlavor().getTargetSdkVersion();
            if (targetSdk != -1) {
                return targetSdk;
            }

            return targetSdk; // from manifest
        }

        @Override
        public int getBuildSdk() {
            String compileTarget = mProject.getCompileTarget();
            if (compileTarget != null) {
                AndroidVersion version = AndroidTargetHash.getPlatformVersion(compileTarget);
                if (version != null) {
                    return version.getApiLevel();
                }
            }

            return super.getBuildSdk();
        }
    }

    private static class LibraryProject extends LintGradleProject {
        private AndroidLibrary mLibrary;

        private LibraryProject(
                @NonNull LintGradleClient client,
                @NonNull File dir,
                @NonNull File referenceDir,
                @NonNull AndroidLibrary library) {
            super(client, dir, referenceDir, library.getManifest());
            mLibrary = library;

            // TODO: Make sure we don't use this project for any source library projects!
            mReportIssues = false;
        }

        @Override
        public boolean isLibrary() {
            return true;
        }

        @Override
        public AndroidLibrary getGradleLibraryModel() {
            return mLibrary;
        }

        @Override
        public Variant getCurrentVariant() {
            return null;
        }

        @NonNull
        @Override
        public List<File> getManifestFiles() {
            if (mManifestFiles == null) {
                File manifest = mLibrary.getManifest();
                if (manifest.exists()) {
                    mManifestFiles = Collections.singletonList(manifest);
                } else {
                    mManifestFiles = Collections.emptyList();
                }
            }

            return mManifestFiles;
        }

        @NonNull
        @Override
        public List<File> getResourceFolders() {
            if (mResourceFolders == null) {
                File folder = mLibrary.getResFolder();
                if (folder.exists()) {
                    mResourceFolders = Collections.singletonList(folder);
                } else {
                    mResourceFolders = Collections.emptyList();
                }
            }

            return mResourceFolders;
        }

        @NonNull
        @Override
        public List<File> getJavaSourceFolders() {
            return Collections.emptyList();
        }

        @NonNull
        @Override
        public List<File> getJavaClassFolders() {
            return Collections.emptyList();
        }

        @NonNull
        @Override
        public List<File> getJavaLibraries() {
            if (mJavaLibraries == null) {
                File jarFile = mLibrary.getJarFile();
                if (jarFile.exists()) {
                    mJavaLibraries = Collections.singletonList(jarFile);
                } else {
                    mJavaLibraries = Collections.emptyList();
                }
            }

            return mJavaLibraries;
        }
    }
}