1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support 18 19import com.android.build.gradle.LibraryExtension 20import com.android.build.gradle.api.LibraryVariant 21import com.android.builder.core.BuilderConstants 22import com.google.common.collect.ImmutableMap 23import net.ltgt.gradle.errorprone.ErrorProneBasePlugin 24import net.ltgt.gradle.errorprone.ErrorProneToolChain 25import org.gradle.api.Action 26import org.gradle.api.JavaVersion 27import org.gradle.api.Plugin 28import org.gradle.api.Project 29import org.gradle.api.artifacts.maven.MavenDeployer 30import org.gradle.api.tasks.Upload 31import org.gradle.api.tasks.bundling.Jar 32 33/** 34 * Support library specific com.android.library plugin that sets common configurations needed for 35 * support library modules. 36 */ 37class SupportLibraryPlugin implements Plugin<Project> { 38 private static final String INSTRUMENTATION_RUNNER = 39 "android.support.test.runner.AndroidJUnitRunner"; 40 41 @Override 42 public void apply(Project project) { 43 SupportLibraryExtension supportLibraryExtension = 44 project.extensions.create("supportLibrary", SupportLibraryExtension, project); 45 46 project.apply(ImmutableMap.of("plugin", "com.android.library")); 47 project.apply(ImmutableMap.of("plugin", ErrorProneBasePlugin.class)); 48 49 LibraryExtension library = project.extensions.findByType(LibraryExtension.class); 50 51 library.compileSdkVersion project.currentSdk 52 53 library.defaultConfig { 54 // Update the version meta-data in each Manifest. 55 addManifestPlaceholders(["support-version": project.rootProject.supportVersion]) 56 57 // Set test related options. 58 testInstrumentationRunner INSTRUMENTATION_RUNNER 59 } 60 61 library.signingConfigs { 62 debug { 63 // Use a local debug keystore to avoid build server issues. 64 storeFile project.rootProject.init.debugKeystore 65 } 66 } 67 68 library.sourceSets { 69 main { 70 // We use a non-standard manifest path. 71 manifest.srcFile 'AndroidManifest.xml' 72 } 73 74 androidTest { 75 // We use a non-standard test directory structure. 76 root 'tests' 77 java.srcDir 'tests/src' 78 res.srcDir 'tests/res' 79 manifest.srcFile 'tests/AndroidManifest.xml' 80 } 81 } 82 83 // Always lint check NewApi as fatal. 84 library.lintOptions { 85 abortOnError true 86 ignoreWarnings true 87 88 // Write output directly to the console (and nowhere else). 89 textOutput 'stderr' 90 textReport true 91 htmlReport false 92 xmlReport false 93 94 // Format output for convenience. 95 explainIssues true 96 noLines false 97 quiet true 98 99 // Always fail on NewApi. 100 error 'NewApi' 101 } 102 103 // Java 8 is only fully supported on API 24+ and not all Java 8 features are binary 104 // compatible with API < 24, so use Java 7 for both source AND target. 105 library.compileOptions { 106 sourceCompatibility JavaVersion.VERSION_1_7 107 targetCompatibility JavaVersion.VERSION_1_7 108 } 109 110 // Create sources jar for release builds 111 library.getLibraryVariants().all(new Action<LibraryVariant>() { 112 @Override 113 public void execute(LibraryVariant libraryVariant) { 114 if (!libraryVariant.getBuildType().getName().equals(BuilderConstants.RELEASE)) { 115 return; // Skip non-release builds. 116 } 117 118 Jar sourceJar = project.getTasks().create("sourceJarRelease", Jar.class); 119 sourceJar.setClassifier("sources"); 120 sourceJar.from(library.getSourceSets().findByName("main").getJava().getSrcDirs()); 121 project.getArtifacts().add("archives", sourceJar); 122 } 123 }); 124 125 // Set uploadArchives options. 126 Upload uploadTask = (Upload) project.getTasks().getByName("uploadArchives"); 127 project.afterEvaluate { 128 uploadTask.repositories { 129 mavenDeployer { 130 repository(url: project.uri(project.rootProject.supportRepoOut)) 131 } 132 }; 133 uploadTask.getRepositories().withType(MavenDeployer.class, new Action<MavenDeployer>() { 134 @Override 135 public void execute(MavenDeployer mavenDeployer) { 136 mavenDeployer.getPom().project { 137 name supportLibraryExtension.getName() 138 description supportLibraryExtension.getDescription() 139 url 'http://developer.android.com/tools/extras/support-library.html' 140 inceptionYear supportLibraryExtension.getInceptionYear() 141 142 licenses { 143 license { 144 name 'The Apache Software License, Version 2.0' 145 url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 146 distribution 'repo' 147 } 148 149 supportLibraryExtension.getLicenses().each { 150 SupportLibraryExtension.License supportLicense -> 151 license { 152 name supportLicense.name 153 url supportLicense.url 154 distribution 'repo' 155 } 156 } 157 } 158 159 scm { 160 url "http://source.android.com" 161 connection "scm:git:https://android.googlesource.com/platform/frameworks/support" 162 } 163 developers { 164 developer { 165 name 'The Android Open Source Project' 166 } 167 } 168 } 169 } 170 }); 171 } 172 173 if (project.rootProject.usingFullSdk) { 174 // Library projects don't run lint by default, so set up dependency. 175 uploadTask.dependsOn project.tasks.lint 176 } 177 178 final ErrorProneToolChain toolChain = ErrorProneToolChain.create(project); 179 library.getBuildTypes().create("errorProne") 180 library.getLibraryVariants().all(new Action<LibraryVariant>() { 181 @Override 182 void execute(LibraryVariant libraryVariant) { 183 if (libraryVariant.getBuildType().getName().equals("errorProne")) { 184 libraryVariant.getJavaCompile().setToolChain(toolChain); 185 186 libraryVariant.getJavaCompile().options.compilerArgs += [ 187 '-XDcompilePolicy=simple', // Workaround for b/36098770 188 189 // Enforce the following checks. 190 '-Xep:MissingOverride:ERROR', 191 '-Xep:ClassNewInstance:ERROR', 192 ] 193 } 194 } 195 }) 196 } 197} 198