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

Library projects should use android.libraryVariants instead of android.applicationVariants #391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jsonSchema2Pojo {
// A fully qualified class name, referring to a custom annotator class that implements
// org.jsonschema2pojo.NoopAnnotator and will be used in addition to the one chosen
// by annotationStyle. If you want to use the custom annotator alone, set annotationStyle to none.
customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'
// customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'

// Whether to include JSR-303/349 annotations (for schema rules like minimum, maximum, etc) in
// generated Java types. Schema rules and the annotation they produce:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jsonSchema2Pojo {
// A fully qualified class name, referring to a custom annotator class that implements
// org.jsonschema2pojo.NoopAnnotator and will be used in addition to the one chosen
// by annotationStyle. If you want to use the custom annotator alone, set annotationStyle to none.
customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'
// customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'

// Whether to include JSR-303/349 annotations (for schema rules like minimum, maximum, etc) in
// generated Java types. Schema rules and the annotation they produce:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.gradle.api.tasks.TaskAction
class GenerateJsonSchemaTask extends DefaultTask {
def configuration

enum AndroidProject { APP, LIBRARY }

GenerateJsonSchemaTask() {
description = 'Generates Java classes from a json schema.'
group = 'Build'
Expand All @@ -38,19 +40,20 @@ class GenerateJsonSchemaTask extends DefaultTask {
configuration = project.jsonSchema2Pojo
configuration.targetDirectory = configuration.targetDirectory ?:
project.file("${project.buildDir}/generated-sources/js2p")

if (project.plugins.hasPlugin('java')) {
configureJava()
} else if (project.plugins.hasPlugin('com.android.application') ||
project.plugins.hasPlugin('com.android.library')) {
configureAndroid()
} else if (project.plugins.hasPlugin('com.android.application')) {
configureAndroid(AndroidProject.APP)
} else if (project.plugins.hasPlugin('com.android.library')) {
configureAndroid(AndroidProject.LIBRARY)
} else {
throw new GradleException('generateJsonSchema: Java or Android plugin required')
}
outputs.dir configuration.targetDirectory
}
}

def configureJava() {
project.sourceSets.main.java.srcDirs += [ configuration.targetDirectory ]
dependsOn(project.tasks.processResources)
Expand All @@ -61,25 +64,37 @@ class GenerateJsonSchemaTask extends DefaultTask {
configuration.sourceFiles.each { it.mkdir() }
}
}
def configureAndroid() {

def configureAndroid(AndroidProject androidProject) {
def android = project.extensions.android
android.sourceSets.main.java.srcDirs += [ configuration.targetDirectory ]
android.applicationVariants.all { variant ->

android.(getVariantProperty(androidProject)).all { variant ->
dependsOn("process${variant.name.capitalize()}Resources")
variant.javaCompile.dependsOn(this)
}

if (!configuration.source.hasNext()) {
configuration.sourceFiles = project.files(
android.sourceSets.main.resources.srcDirs.collect {
android.sourceSets.main.resources.srcDirs.collect {
"${it}/json"
}.findAll {
project.file(it).exists()
project.file(it).exists()
})
}
}

def getVariantProperty(AndroidProject androidProject) {
switch(androidProject) {
case AndroidProject.APP:
return "applicationVariants"
case AndroidProject.LIBRARY:
return "libraryVariants"
default:
throw new IllegalArgumentException("Passed an invalid android project type")
}
}

@TaskAction
def generate() {
logger.info 'Using this configuration:\n{}', configuration
Expand Down