【问题标题】:Import swagger-codegen project into existing Android project将 swagger-codegen 项目导入现有的 Android 项目
【发布时间】:2016-12-06 06:43:48
【问题描述】:

我正在尝试将 swagger-codegen 生成的“模块”项目集成到我的 Android 项目中。

从我的角度来看,之前没有用 gradle 做太多工作,swagger-codegen 创建了一个相当混乱的 build.gradle。

我很难找到有关如何执行此操作的文档。而且我觉得有点失落。

我使用了FAQ中描述的这种方法

mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
 -i http://petstore.swagger.io/v2/swagger.json \
 -l java --library=okhttp-gson \
 -o /var/tmp/java/okhttp-gson/ 

到目前为止,我尝试从由 swagger-codegen 生成的项目中复制源代码并合并两个 gradle 构建文件。我删除了 Junit 测试,因为我无法让 Junit 依赖项工作 (Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12)。但是后来我陷入了插件之间的一些冲突?

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

这是 build.gradle:

import static jdk.nashorn.internal.runtime.regexp.joni.ApplyCaseFold.apply

apply plugin: 'idea'
apply plugin: 'eclipse'

group = 'io.swagger'
version = '1.0.0'

buildscript {
   repositories {
       jcenter()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:2.1.2'
       // classpath 'com.android.tools.build:gradle:1.5.+'
       classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
   }
}

repositories {
   jcenter()
   maven { url 'http://repo1.maven.org/maven2' }
}


if(hasProperty('target') && target == 'android') {

   apply plugin: 'com.android.library'
   apply plugin: 'com.github.dcendents.android-maven'

   android {
       compileSdkVersion 23
       buildToolsVersion '23.0.2'
       defaultConfig {
            minSdkVersion 14
            targetSdkVersion 23
       }
        compileOptions {
           sourceCompatibility JavaVersion.VERSION_1_7
           targetCompatibility JavaVersion.VERSION_1_7
       }

       // Rename the aar correctly
       libraryVariants.all { variant ->
           variant.outputs.each { output ->
               def outputFile = output.outputFile
               if (outputFile != null &&    outputFile.name.endsWith('.aar')) {
                    def fileName = "\u0024{project.name}-  \u0024{variant.baseName}-\u0024{version}.aar"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

    dependencies {
        provided 'javax.annotation:jsr250-api:1.0'
    }
}

afterEvaluate {
    android.libraryVariants.all { variant ->
        def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
        task.description = "Create jar artifact for ${variant.name}"
        task.dependsOn variant.javaCompile
        task.from variant.javaCompile.destinationDir
        task.destinationDir = project.file("${project.buildDir}/outputs/jar")
        task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
        artifacts.add('archives', task);
    }
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

artifacts {
    archives sourcesJar
}

} else {

apply plugin: 'java'
apply plugin: 'maven'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

install {
    repositories.mavenInstaller {
        pom.artifactId = 'XxxxXxxx'
    }
}

    task execute(type:JavaExec) {
       main = System.getProperty('mainClass')
       classpath = sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile 'io.swagger:swagger-annotations:1.5.8'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
    compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'joda-time:joda-time:2.9.3'
   // testCompile 'junit:junit:4.12'
}

我在这里做错了吗?在我的项目中实现 swagger-codegen 代码的正确方法是什么?

【问题讨论】:

    标签: android-studio android-volley build.gradle okhttp swagger-codegen


    【解决方案1】:

    导入它的快速方法是编译 swagger 生成的项目,然后将 .jar 文件复制到我的 android 项目并将其添加为库。

    【讨论】:

    • 在打开 Swagger 项目后生成 .jar(并稍后导入)文件对我有用,谢谢!
    【解决方案2】:

    我很难找到有关如何执行此操作的文档。而且我觉得有点失落。

    您可以克隆 Android swagger-codegen 示例。

    (它确实使用了 Junit,所以我不确定你遇到了什么错误)

    除非你是这个意思

    到目前为止,我尝试复制源并合并两个 gradle 构建文件

    我问,哪两个 Gradle 文件?看起来您将 Android Gradle 文件与 Java Gradle 文件合并,这似乎会导致更多问题,因为您正在...

    “java”插件已应用,但与安卓插件不兼容。

    当你有这条线时,这似乎很不言自明

    apply plugin: 'java'
    

    除了检查构建目标之外,您在这里尝试做什么并不太清楚

    if(hasProperty('target') && target == 'android')
    

    【讨论】:

    • 感谢您的精彩提示!我稍微澄清了我所说的消息来源的意思。但我会看一下示例源代码。
    • 我的意思是这两个构建gradle文件是在swagger-codegen生成的项目中找到的和在原始Android项目中的一个
    • 我明白了。不过,我看起来您生成的是 Java 版本,而不是 Android 版本
    • 也许我生成的代码在某种程度上是错误的?也许这就是它不想使用 apply plugin: 'java' 的原因?
    • 我同意你的观点,但我需要更多地阅读 Swagger-codegen 才能给出明确的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多