|
| 1 | +// Android API-level compatibility gate (a modernised github #336). |
| 2 | +// |
| 3 | +// Checks a module's base (--release 8) classes against an Android platform |
| 4 | +// signature with AnimalSniffer, failing the build on a reference to a |
| 5 | +// java.* / javax.* platform API absent from that API level - the class of |
| 6 | +// regression behind the BigIntegers.intValueExact NoSuchMethodError on older |
| 7 | +// Android (github #2369). Only the base classes are checked, never the |
| 8 | +// multi-release overlays, which are selected by JDK version at runtime and |
| 9 | +// never load on Android. |
| 10 | +// |
| 11 | +// The engine is org.codehaus.mojo:animal-sniffer's SignatureChecker, loaded |
| 12 | +// off a dedicated configuration so it never touches the build's own classpath. |
| 13 | +// Two per-module knobs configure it: |
| 14 | +// ext.androidIgnoredPackages - packages treated as available (the module's |
| 15 | +// own code and its bundled runtime dependencies), suppressed at source. |
| 16 | +// ext.androidAllowedRefs - substrings of the remaining, intentionally |
| 17 | +// platform-absent references that are permitted (e.g. the optional JNDI |
| 18 | +// fetchers, or bcmail's legacy java.awt DataFlavor). Anything not covered |
| 19 | +// by either fails the build. |
| 20 | + |
| 21 | +configurations { |
| 22 | + animalSnifferTool |
| 23 | + androidApiSignature |
| 24 | +} |
| 25 | + |
| 26 | +dependencies { |
| 27 | + animalSnifferTool "org.codehaus.mojo:animal-sniffer:${animalSnifferVersion}" |
| 28 | + animalSnifferTool "org.ow2.asm:asm:${animalSnifferAsmVersion}" |
| 29 | + androidApiSignature "net.sf.androidscents.signature:android-api-level-${androidApiLevel}:${androidSignatureVersion}@signature" |
| 30 | +} |
| 31 | + |
| 32 | +def androidApiCheck = tasks.register('androidApiCheck') { |
| 33 | + group = 'verification' |
| 34 | + description = "Checks the base classes for Android API level ${androidApiLevel} compatibility" |
| 35 | + dependsOn tasks.named('classes') |
| 36 | + |
| 37 | + def toolCp = configurations.animalSnifferTool |
| 38 | + def signatureCfg = configurations.androidApiSignature |
| 39 | + def classDirs = sourceSets.main.output.classesDirs |
| 40 | + def ignoredPackages = (project.findProperty('androidIgnoredPackages') ?: []) as List |
| 41 | + def allowedRefs = (project.findProperty('androidAllowedRefs') ?: []) as List |
| 42 | + def apiLevel = androidApiLevel |
| 43 | + def moduleName = project.name |
| 44 | + |
| 45 | + inputs.files(toolCp, signatureCfg, classDirs) |
| 46 | + def marker = layout.buildDirectory.file("animalsniffer/android-api-${androidApiLevel}.ok") |
| 47 | + outputs.file(marker) |
| 48 | + |
| 49 | + doLast { |
| 50 | + def loader = new URLClassLoader( |
| 51 | + toolCp.files.collect { it.toURI().toURL() } as URL[], |
| 52 | + (ClassLoader) getClass().classLoader) |
| 53 | + def checkerClass = loader.loadClass('org.codehaus.mojo.animal_sniffer.SignatureChecker') |
| 54 | + def loggerClass = loader.loadClass('org.codehaus.mojo.animal_sniffer.logging.Logger') |
| 55 | + |
| 56 | + def violations = [] |
| 57 | + def logger = java.lang.reflect.Proxy.newProxyInstance(loader, [loggerClass] as Class[], |
| 58 | + { proxy, method, args -> |
| 59 | + if (method.name == 'error' && args && args[0] instanceof String) { |
| 60 | + violations << (String) args[0] |
| 61 | + } |
| 62 | + return null |
| 63 | + } as java.lang.reflect.InvocationHandler) |
| 64 | + |
| 65 | + def ignoreSet = new HashSet<String>(ignoredPackages) |
| 66 | + ignoreSet.addAll(['org.bouncycastle.*', |
| 67 | + 'org.jvnet.animal_sniffer.*', |
| 68 | + 'org.codehaus.mojo.animal_sniffer.*', |
| 69 | + 'org.objectweb.*']) |
| 70 | + |
| 71 | + signatureCfg.singleFile.withInputStream { sigIn -> |
| 72 | + def checker = checkerClass |
| 73 | + .getConstructor(InputStream, Set, loggerClass) |
| 74 | + .newInstance(sigIn, ignoreSet, logger) |
| 75 | + classDirs.files.each { dir -> |
| 76 | + if (dir.isDirectory()) { |
| 77 | + checker.getClass().getMethod('process', File).invoke(checker, dir) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + def unexpected = violations.findAll { v -> !allowedRefs.any { a -> v.contains(a) } } |
| 83 | + .toUnique().sort() |
| 84 | + if (!unexpected.isEmpty()) { |
| 85 | + throw new GradleException( |
| 86 | + "Android API level ${apiLevel} incompatibilities in ${moduleName} (${unexpected.size()}):\n " + |
| 87 | + unexpected.take(60).join('\n ')) |
| 88 | + } |
| 89 | + |
| 90 | + def out = marker.get().asFile |
| 91 | + out.parentFile.mkdirs() |
| 92 | + out.text = "Android API ${apiLevel}: OK\n" |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +tasks.named('check') { dependsOn androidApiCheck } |
0 commit comments