0

我试图将swagger-codegen生成的“模块”项目集成到我的Android项目中。将swagger-codegen项目导入到现有的Android项目

以前没有用过这么多的工具,而且从我的角度来看,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/ 

所以车费我试图源从由招摇,代码生成生成的项目复制和合并两个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代码的正确方法是什么?

回答

1

我很难找到如何做到这一点的文档。我感觉有点失落。

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

(它使用JUnit,所以我不知道你得到了什么错误)

除非那是你的

意思到目前为止,我试图复制源和合并两个gradle这个构建文件

对此,我问,哪两个Gradle文件?它看起来像你合并了Android Gradle文件与Java Gradle文件,这似乎导致更多的问题,因为你越来越...

'java'插件已应用,但它不兼容Android插件。

这似乎非常自我解释当你有这条线

apply plugin: 'java' 

,这不是太清楚你正试图在这里做除了检查构建目标

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

非常感谢您的提示!我澄清了这个问题,关于我的意思是什么。但我会看看示例源。 – emmanuel2004

+0

这两个构建gradle文件我的意思是在swagger-codegen生成的项目中找到的那个文件,以及在我看到的原始Android项目 – emmanuel2004

+1

中的文件。我看起来像你生成了一个Java,但不是Android的一个 –

0

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