Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Android support #99

Merged
merged 14 commits into from
Nov 16, 2024
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ Example projects (with intentional errors to see output):

#### Compatibility

**IMPORTANT**: Plugin only works when `java-base` plugin (activated by any java-related plugin like `java-library`, `groovy`, `scala`, `org.jetbrains.kotlin.jvm`, etc.) is enabled,
otherwise nothing will be registered.
There is **no support for the Android plugin** (`java-base` plugin must be used to perform animalsniffer check).
Support for the Android plugin requires Android plugin version `7.4.0` or greater.

For *kotlin multiplatform* plugin enable java support:

Expand Down
21 changes: 21 additions & 0 deletions examples/android-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Android library project with animalsniffer check

Output:

```
> Task :android-app:animalsnifferDebug

4 AnimalSniffer violations were found in 2 files. See the report at: file:////{PATH_FROM_ROOT}/gradle-animalsniffer-plugin/examples/android-app/build/reports/animalsniffer/debug.text

[Undefined reference | java18-1.0] invalid.(Sample.java:9)
>> String String.repeat(int)

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(Sample.java:9)
>> String String.repeat(int)

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(Sample.java:15)
>> java.nio.file.Path java.io.File.toPath()

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(SampleKotlin.kt:9)
>> java.nio.file.Path java.io.File.toPath()
```
34 changes: 34 additions & 0 deletions examples/android-app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id 'com.android.application' version '7.4.0'
id 'org.jetbrains.kotlin.android' version '1.9.23'
id 'ru.vyarus.animalsniffer'
}

android {
compileSdk 33
namespace 'com.example.namespace'
def javaVersion = JavaVersion.VERSION_1_8

compileOptions {
sourceCompatibility(javaVersion)
targetCompatibility(javaVersion)
}

kotlinOptions {
jvmTarget = javaVersion.toString()
}
}

animalsniffer {
// debug = true
// only show errors
ignoreFailures = true
}

repositories { mavenCentral(); google() }
dependencies {
signature 'org.codehaus.mojo.signature:java18:1.0@signature'
signature 'net.sf.androidscents.signature:android-api-level-21:5.0.1_r2@signature'

implementation 'org.slf4j:slf4j-api:1.7.25'
}
2 changes: 2 additions & 0 deletions examples/android-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest/>
17 changes: 17 additions & 0 deletions examples/android-app/src/main/java/invalid/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package invalid;

import java.io.File;

public class Sample {

public static void main(String[] args) {
// method added in 11
"".repeat(5);
}

public void someth() {
// not available in android
File file = new File("");
file.toPath();
}
}
11 changes: 11 additions & 0 deletions examples/android-app/src/main/java/invalid/SampleKotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package invalid

import java.io.File

class SampleKotlin {
fun someth() {
// not available in android
val file = File("")
file.toPath()
}
}
21 changes: 21 additions & 0 deletions examples/android-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Android application project with animalsniffer check

Output:

```
> Task :android-lib:animalsnifferDebug

4 AnimalSniffer violations were found in 2 files. See the report at: file:////{PATH_FROM_ROOT}/gradle-animalsniffer-plugin/examples/android-lib/build/reports/animalsniffer/debug.text

[Undefined reference | java18-1.0] invalid.(Sample.java:9)
>> String String.repeat(int)

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(Sample.java:9)
>> String String.repeat(int)

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(Sample.java:15)
>> java.nio.file.Path java.io.File.toPath()

[Undefined reference | android-api-level-21-5.0.1_r2] invalid.(SampleKotlin.kt:9)
>> java.nio.file.Path java.io.File.toPath()
```
34 changes: 34 additions & 0 deletions examples/android-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id 'com.android.library' version '7.4.0'
id 'org.jetbrains.kotlin.android' version '1.9.23'
id 'ru.vyarus.animalsniffer'
}

android {
compileSdk 33
namespace 'com.example.namespace'
def javaVersion = JavaVersion.VERSION_1_8

compileOptions {
sourceCompatibility(javaVersion)
targetCompatibility(javaVersion)
}

kotlinOptions {
jvmTarget = javaVersion.toString()
}
}

animalsniffer {
// debug = true
// only show errors
ignoreFailures = true
}

repositories { mavenCentral() }
dependencies {
signature 'org.codehaus.mojo.signature:java18:1.0@signature'
signature 'net.sf.androidscents.signature:android-api-level-21:5.0.1_r2@signature'

implementation 'org.slf4j:slf4j-api:1.7.25'
}
2 changes: 2 additions & 0 deletions examples/android-lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest/>
17 changes: 17 additions & 0 deletions examples/android-lib/src/main/java/invalid/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package invalid;

import java.io.File;

public class Sample {

public static void main(String[] args) {
// method added in 11
"".repeat(5);
}

public void someth() {
// not available in android
File file = new File("");
file.toPath();
}
}
11 changes: 11 additions & 0 deletions examples/android-lib/src/main/java/invalid/SampleKotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package invalid

import java.io.File

class SampleKotlin {
fun someth() {
// not available in android
val file = File("")
file.toPath()
}
}
2 changes: 2 additions & 0 deletions examples/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
google()
maven { url 'https://jitpack.io' }
}
}

enableFeaturePreview "STABLE_CONFIGURATION_CACHE"

include 'java', 'groovy', 'kotlin', 'scala',
'android-lib', 'android-app',
'buildSignature:fromClasses',
'buildSignature:fromJars',
'buildSignature:fromSignatures',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
*/
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
Set<File> sourcesDirs
FileCollection sourcesDirs

/**
* Annotation class name to avoid check
Expand Down Expand Up @@ -298,7 +298,7 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
@CompileStatic(TypeCheckingMode.SKIP)
private Set<File> collectSourceDirs() {
Set<File> res = [] as Set
res.addAll(getSourcesDirs())
res.addAll(getSourcesDirs().getFiles())
// HACK to support kotlin multiplatform source path for jvm case (when withJava() active)
// this MUST BE rewritten into separate support for multiplatform
if (project.plugins.findPlugin('org.jetbrains.kotlin.multiplatform')) {
Expand Down
Loading
Loading