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

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

Open
masranber opened this issue Mar 4, 2024 · 4 comments

Comments

@masranber
Copy link

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.

@oscar-tu-lotero
Copy link

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
Copy link

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
Copy link
Author

@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
Copy link
Collaborator

unkish commented Apr 21, 2024

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

juherr added a commit to juherr/jsonschema2pojo that referenced this issue Aug 25, 2024
juherr added a commit to juherr/jsonschema2pojo that referenced this issue Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants