joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Home Page:http://www.jsonschema2pojo.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

masranber opened this issue · comments

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:

void setTargetVersion(JsonSchemaExtension configuration) {
if (!configuration.targetVersion) {
if (project.plugins.hasPlugin("com.android.application")) {
configuration.targetVersion = project.plugins.getPlugin("com.android.application").extension.compileOptions.sourceCompatibility
logger.info 'Using android.compileOptions.sourceCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
} else if (project.plugins.hasPlugin("com.android.library")) {
configuration.targetVersion = project.plugins.getPlugin("com.android.library").extension.compileOptions.sourceCompatibility
logger.info 'Using android.compileOptions.sourceCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
}
}
}

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).
  5. Sync Gradle with project files.
  6. Build project.
  7. 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.

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

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"

@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?

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