1 /* 2 * Copyright 2022 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 17 package androidx.build.lint 18 19 import com.android.SdkConstants 20 import com.android.tools.lint.detector.api.Category 21 import com.android.tools.lint.detector.api.Detector 22 import com.android.tools.lint.detector.api.Implementation 23 import com.android.tools.lint.detector.api.Incident 24 import com.android.tools.lint.detector.api.Issue 25 import com.android.tools.lint.detector.api.Scope 26 import com.android.tools.lint.detector.api.Severity 27 import com.android.tools.lint.detector.api.XmlContext 28 import com.android.tools.lint.detector.api.XmlScanner 29 import org.w3c.dom.Element 30 31 @Suppress("UnstableApiUsage") 32 class AndroidManifestServiceExportedDetector : Detector(), XmlScanner { 33 getApplicableElementsnull34 override fun getApplicableElements(): Collection<String> { 35 return listOf(SdkConstants.TAG_SERVICE) 36 } 37 visitElementnull38 override fun visitElement(context: XmlContext, element: Element) { 39 val attrExported = element.getAttribute("android:${SdkConstants.ATTR_EXPORTED}") 40 if (attrExported != "true") { 41 val incident = 42 Incident(context, ISSUE) 43 .message("Missing exported=true in <service> tag") 44 .at(element) 45 context.report(incident) 46 } 47 } 48 49 companion object { 50 val ISSUE = 51 Issue.create( 52 id = "MissingServiceExportedEqualsTrue", 53 briefDescription = 54 "Missing exported=true declaration in the <service> tag inside" + 55 " the library manifest", 56 explanation = "Library-defined services should set the exported attribute to true.", 57 category = Category.CORRECTNESS, 58 priority = 5, 59 severity = Severity.ERROR, 60 implementation = 61 Implementation( 62 AndroidManifestServiceExportedDetector::class.java, 63 Scope.MANIFEST_SCOPE 64 ) 65 ) 66 } 67 } 68