joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Task 'generateJsonSchema2Pojo' fails after upgrading to Android Gradle Plugin 8.2.2 / Gradle 8.2 #1591

Open masranber opened 8 months ago

masranber commented 8 months ago

Issue

The plugin appears to be incompatible with newer versions the Android Gradle Plugin (8.0.0+ from what I can tell).

I'm using version 1.2.1 of the jsonschema2pojo plugin in an Android library. I'm currently in the process of upgrading the project from Android Gradle Plugin version 7.2.0 to 8.2.2, which also requires Gradle version >= 8.2.

After upgrading AGP and Gradle, building the project fails due to an exception in the generateJsonSchema2Pojo Gradle task:

Execution failed for task ':mylibrary:generateJsonSchema2PojoForDebug'.
> No such property: extension for class: com.android.build.gradle.LibraryPlugin

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':mylibrary:generateJsonSchema2PojoForDebug'. <30 internal lines>
Caused by: groovy.lang.MissingPropertyException: No such property: extension for class: com.android.build.gradle.LibraryPlugin
    at org.jsonschema2pojo.gradle.GenerateJsonSchemaAndroidTask.setTargetVersion(GenerateJsonSchemaAndroidTask.groovy:69)
    at org.jsonschema2pojo.gradle.GenerateJsonSchemaAndroidTask$setTargetVersion.callCurrent(Unknown Source)
        at org.jsonschema2pojo.gradle.GenerateJsonSchemaAndroidTask.generate(GenerateJsonSchemaAndroidTask.groovy:52) <118 internal lines>

BUILD FAILED in 2s

Here's the source code snippet where the exception is being thrown:

https://github.com/joelittlejohn/jsonschema2pojo/blob/72050f6b94d46d51d7c93c51e35383cdf5fc733f/jsonschema2pojo-gradle-plugin/src/main/groovy/org/jsonschema2pojo/gradle/GenerateJsonSchemaAndroidTask.groovy#L63-L73

Steps to reproduce

1) Create a new, empty Android library project. 2) In 'project structure' menu in Android Studio, set AGP version to 8.2.2 and Gradle version to 8.2. 3) Apply the jsonschema2pojo plugin to the project. 4) Include a dummy json schema in the project (just so the generateJsonSchema2Pojo task runs). 4) Sync Gradle with project files. 5) Build project. 6) Build will fail with the above exception.

Potential solution

In newer versions of the Android Gradle Plugin extension is no longer a member of the class com.android.build.gradle.LibraryPlugin. The com.android.build.gradle.LibraryExtension instance can instead be accessed via the android property of org.gradle.api.Project that gets added by the AGP.

Therefore, the following line:

configuration.targetVersion = project.plugins.getPlugin("com.android.library").extension.compileOptions.sourceCompatibility

should be changed to:

configuration.targetVersion = project.property("android").compileOptions.sourceCompatibility

In my limited testing (basically just running the code directly in my build.gradle script) this appears to work in the latest AGP version as well as the version I was previously using (7.2.0).

I believe this issue also applies to Android apps (com.android.application projects). Since the android property returns the correct project extension instance for the current project type (com.android.application or com.android.library) the above code should also work for com.android.application projects.

The setTargetVersion method would then look like:

void setTargetVersion(JsonSchemaExtension configuration) { 
   if (!configuration.targetVersion) { 
     if (project.hasProperty("android")) { 
       configuration.targetVersion = project.property("android").compileOptions.sourceCompatibility 
       logger.info 'Using android.compileOptions.sourceCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion 
     }
   } 
 }

I'm not sure if this issue appears elsewhere in the source code.

oscar-tu-lotero commented 7 months ago

Hi, @masranber , Have your found any way to workaround this issue in existing projects in some way, while we wait for a fix?

oscar-tu-lotero commented 7 months ago

In case it helps anyone, it has helped me to move the plugin to a new module of the application, of type "Java or Kotlin module"

masranber commented 7 months ago

@oscar-tu-lotero I haven't found a solution yet. The plugin is already in a separate module from the main application. Would you mind posting your project-level build.gradle and module-level build.gradle to see if I can replicate your fix?

unkish commented 7 months ago

Out of curiosity: does explicitly setting/providing targetVersion help (as that should prevent code from entering 'problematic' if block) ?