Import swagger-codegen project into existing Android project - android-studio

Im trying to integrate a "module"-project generated by swagger-codegen, into my Android project.
Haven't worked that much with gradle before and the swagger-codegen creates a quite messy build.gradle from my point of view.
I have a hard time finding documentation on how to do this. And I feel a bit lost.
I used this method described in the 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/
So fare I tried to copy the source from the project that was generated by swagger-codegen and merge the two gradle build files. I removed the Junit tests because I couldn't get the Junit dependency working (Implementing Swagger-codegen project - Error:(23, 17) Failed to resolve: junit:junit:4.12). But then I got stuck with some conflict between the plugins?
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Here's the 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'
}
Am I doing something complete wrong here? What is the correct way to implement swagger-codegen code into my project?

The swift way to import it was to compile the swagger generated project then copy the .jar file to my android project and add its as a library.

I have a hard time finding documentation on how to do this. And I feel a bit lost.
You could clone the Android swagger-codegen example.
(which does use Junit, so I'm not sure what error you got)
Unless that's what you mean by
So far I tried to copy the source and merge the two gradle build files
To which, I ask, what two Gradle files? It looks like you merged an Android Gradle file with a Java Gradle file, which seems to causing more issues because you are getting...
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Which seems pretty self explanatory when you have this line
apply plugin: 'java'
It's not too clear what you are trying to do here other than check the build target
if(hasProperty('target') && target == 'android')

Related

Flutter is not running?

I have some problems while running flutter in android studio
This is my error
Launching lib\main.dart on Moto C Plus in debug mode...
Initializing gradle...
Resolving dependencies...
Finished with error: Please review your Gradle project setup in the android/ folder.
* Error running Gradle:
Exit code 1 from: D:\FlutterPrograms\app\android\gradlew.bat app:properties:
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to E:\Sdk\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.
Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.
FAILURE: Build failed with an exception.
Where:
Build file 'D:\FlutterPrograms\app\android\app\build.gradle' line: 25
What went wrong:
A problem occurred evaluating project ':app'.
Could not resolve all artifacts for configuration 'classpath'.
Could not find lint-gradle-api.jar (com.android.tools.lint:lint-gradle-api:26.1.2).
Searched in the following locations:
https://jcenter.bintray.com/com/android/tools/lint/lint-gradle-api/26.1.2/lint-gradle-api-26.1.2.jar
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 1s
This is my buid.gradle file
buildscript {
repositories {
maven {
url 'https://dl.google.com/dl/android/maven2'
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is my gradle error
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.shanmukh.app"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
There were lots of similar issues raised the past days, that could be solved by adding the google() repository in first position in the repositories block of the build scripts.
See detailed explanation in the following answers:
couldn't locate lint-gradle-api-26.1.2.jar for flutter project
Could not find play-services-basement.aar
https://stackoverflow.com/a/52982816/6899896
Could not find com.android.tools.build:aapt2:3.2.0
The root cause , related to missing libraries in Jcenter, is explained in detail here : https://stackoverflow.com/a/50885939/6899896
Open the root flutter folder in your computer, and open the gradle folder in that root folder (For example C:\flutter\package\flutter_tool\gradle) and add 'google()' to the buildscript. It fixed the issue for me.
Flutter root folder:
Add 'google()' to the buildscript:
I found a solution!
Go to your flutter folder. After you need to find flutter.gradle. On my PC:
C:\src\flutter\packages\flutter_tools\gradle
Open flutter.gradle with note or notepad and change
buildscript from this:
repositories {
jcenter()
maven {
url 'https://dl.google.com/dl/android/maven2'
}
}
to this:
repositories {
maven {
url 'https://dl.google.com/dl/android/maven2'
}
jcenter()
}

how to integrate with npm web app and spring boot with gradle?

Please Help me to integrate with node ui module and Spring boot using gradle.
I just want to deploy war file with web jar file.
My Project structure is like
myproject
api
src/main/java
src/main/resources
build/libs
web
<--- node files
dist
build/libs
gradle
build.gradle
gradlew
gradlew.bat
settings.gradle
api module is rest-api java application.
And web module is npm node app.
I want to do this senario.
If I type ./gradlew clean build
then :web project compile first and make dist directory and then make jar file.
And then :api project make war with this jar file. I will deploy api war to server.
Maybe above step is not right because I'm not good at it.
How should I make code to do it?
I have to write script in one build.gradle file.
There is only one build.gradle file. I have to use only this file.
buildscript {
ext {
springBootVersion = '1.5.7.RELEASE'
}
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/plugins-release'}
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE'
classpath "com.moowork.gradle:gradle-node-plugin:1.2.0"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
}
project('api') {
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'propdeps'
dependencies {
compile project(':web')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
optional('org.springframework.boot:spring-boot-configuration-processor')
}
compileJava.dependsOn(processResources)
}
project('web') {
apply plugin: 'com.moowork.node'
node {
version = '6.11.4'
npmVersion = '3.10.10'
download = true
distBaseUrl = 'https://nodejs.org/dist'
}
task nodeBuild(type: NpmTask) {
args = ['run', 'build']
}
jar {
from ("dist/")
into ("${rootProject.project('api').projectDir}/src/main/resources/")
includeEmptyDirs = true
}
clean {
delete 'dist/'
}
nodeBuild.dependsOn(npm_install)
build.dependsOn(nodeBuild)
}
Have a look at the Gradle Node Plugin.
Include it in your build dependencies:
buildscript {
...
dependencies {
classpath "com.moowork.gradle:gradle-node-plugin:1.1.1"
}
}
Apply the plugin:
apply plugin: 'com.moowork.node'
Configure it to fit your project structure:
node {
version = '6.10.2'
npmVersion = '3.10.6'
download = true
workDir = file("${project.buildDir}/node")
nodeModulesDir = file("${project.projectDir}")
}
Provide a Gradle task to run NPM:
task build(type: NpmTask) {
args = ['run', 'build']
}
build.dependsOn(npm_install)
You can find a working example with an Angular app integrated in a Gradle build here.

Android Studio build works, Gradle command line fails

I have ported an app from Eclipse to Android Studio, implemented my needed flavors and have it allow working in Android Studio. Due to the number of flavors I would prefer to build it using command line Gradle. However when I run the following command to build my release APKs
gradlew.bat assembleRelease
I get the following error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\whatever\build.gradle' line: 1
* What went wrong:
A problem occurred evaluating project ':MyCompany'.
> java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
I have run with --stacktrace and --debug and it really does not offer any other information, other than pointing the error to be with the first line of the build.gradle:
apply plugin: 'com.android.application'
I have attempted the following based on other stackoverflow responses to this error:
Upgraded to JavaVersion.VERSION_1_8
Added Jack the build.gradle (and that slowed everything down, but it was required for 1.8, even though it is deprecated?)
Increased memory for heap (org.gradle.jvmargs=-Xmx4096m) and dex (javaMaxHeapSize "4g").
I am running Android Studio 2.3.3
Here is my build.gradle file. I have left only a couple of the flavors to save space, and renamed some of the stuff to protect the innocent. I have left in the dependencies in case that might be the problem? The only funkiness is that I am renaming the APK and pushing it to a different folder, but that is all working when I run the release build directly out of Android Studio. The build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
signingConfigs {
config {
keyAlias 'whatever'
keyPassword 'imnotgoingtotellyou'
storeFile file('C:/whereever/mycompany.keystore')
storePassword 'yeps'
}
}
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
jackOptions {
enabled true
additionalParameters('jack.incremental': 'true')
}
applicationId "com.mycompany.default"
minSdkVersion 14
targetSdkVersion 23
versionCode 44
versionName "2.1.44"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 }
signingConfig signingConfigs.config
}
dexOptions {
javaMaxHeapSize "4g"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.config
}
}
productFlavors.whenObjectAdded { flavor ->
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
productFlavors {
Flavor1 {
signingConfig signingConfigs.config
directoryPath = 'flavor1'
}
Flavor2 {
applicationId 'com.mycompany.flavor2'
signingConfig signingConfigs.config
directoryPath = 'flavor2'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def path = "C:/AndroidBuilds/MyBuild.Build/" + variant.productFlavors[0].directoryPath + "/"
logger.error("Path = " + path)
def SEP = "-"
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
def version = variant.versionCode
def newApkName = path + version + SEP + flavor
logger.error("newApkName = " + newApkName)
output.outputFile = new File(newApkName + ".apk")
}
}
}
dependencies {
compile project(':androidpdfview100')
compile 'com.android.support:support-v13:25.3.1'
compile 'com.google.code.gson:gson:2.7'
compile 'joda-time:joda-time:2.5'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile files('libs/httpmime-4.2.5.jar')
compile files('libs/itextg-5.4.4.jar')
compile files('libs/js.jar')
compile files('libs/logentries-android-2.1.1.jar')
compile files('libs/universal-image-loader-1.8.6.jar')
compile files('libs/xmlworker-5.4.4.jar')
compile files('libs/zbar.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.6.8#aar') {
transitive = true;
}
}
UPDATE
The project structure looks like this:
It was generated automatically by Android Studio when I ported the code from Eclipse:
When you drill into the App itself it looks like this:
For anyone facing this issue in the future, for the command line build make sure your JAVA_HOME is set to the same version as Android Studio is using. E.g. JDK v8:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
Or wherever your 1.8 JDK is. It is a beast and sucks up memory, but it was the only way to get around this build error. I would NOT recommend that you build in Android Studio with 1.8 since it seems to require Jack which is just plain slow. And sadly it is deprecated to boot.
I work on macOS with Zsh as my shell and have just downloaded Android Studio 3.6.2.
I added the following to my ~/.zshenv file, and everything worked.
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
I believe updating ~/.profile would do it as well, but I haven't tried it.

Gradle sync issue with Android Studio 0.5.1

Since updating to AS 0.5.1, I have been unable to sync up my project. Gradle gives me the following warning:
No signature of method: static com.google.common.collect.ArrayListMultimap.create() is applicable for argument types: () values: []
Possible solutions: clear(), grep(), get(java.lang.Object), get(java.lang.Object), getAt(java.lang.String), isCase(java.lang.Object)
It does not point to anything in my build.gradle file that may be causing the issue, so I am rather lost as to where to begin.
Edit: When attempting to run gradle build, I subsequently get this issue when applying the Android plugin:
A problem occurred evaluating root project 'tve-android'.
> Could not create plugin of type 'AppPlugin'.
My build.gradle file:
buildscript {
repositories {
mavenLocal()
maven {
url "http://nexus.products/nexus/content/groups/public"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'jacoco'
apply plugin: 'pmd'
android {
compileSdkVersion 19
buildToolsVersion "19.0.2"
defaultConfig.minSdkVersion = 16
defaultConfig.targetSdkVersion = 19
defaultConfig.testInstrumentationRunner = 'android.test.InstrumentationTestRunner'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main.java.srcDirs = ['src/main/java']
main.resources.srcDirs = ['src/main/resources']
main.res.srcDirs = ['src/res']
main.assets.srcDirs = ['src/assets']
androidTest.java.srcDirs = ['src/test/java']
androidTest.resources.srcDirs = ['src/test/resources']
androidTest.res.srcDirs = ['src/res']
androidTest.assets.srcDirs = ['src/assets']
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.+'
compile 'commons-io:commons-io:2.+'
compile 'commons-lang:commons-lang:2.+'
compile 'com.loopj.android:android-async-http:1.4.+'
compile 'com.jakewharton:disklrucache:1.0.0'
compile 'net.jpountz.lz4:lz4:1.1.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1'
androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
androidTestCompile 'org.mockito:mockito-core:1.9.+'
}
Upgrading gradle to v1.10 is also needed.
I had the same issue and I quickly fix it just by re-import my gradle project.

Android Studio removes modules falsely identified as not backed by Gradle

I have a problem with Android Studio 0.3.0 and my project folder containing a plain old java library project, two Android library projects, and three Android apps. It's all built with Gradle.
The problem is that the initial import into Android Studio works fine (using Android Studio's Import Project..., then choosing my settings.gradle file), but when I press the refresh button in the Gradle sidebar, I get the message "The modules below are not backed by Gradle anymore. Check those to be removed from the ide project too:", and then it lists ALL my modules for removal. Everything builds fine from the terminal.
Output of gradle projects is (with edited names):
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'root'
+--- Project ':android-lib1'
+--- Project ':android-app1'
+--- Project ':android-app2'
+--- Project ':android-app3'
+--- Project ':android-lib2'
\--- Project ':java-lib'
In the root folder, I have settings.gradle:
include ':java-lib'
include ':android-lib1'
include ':android-lib2'
include ':android-app1'
include ':android-app2'
include ':android-app3'
My build.gradle in the root folder:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
allprojects {
repositories {
mavenCentral()
ivy {
name 'repo'
artifactPattern 'http://repo.example.com:8081/artifactory/libs-release-local/[organisation]/[module]/[revision]/[type]s/[artifact].[ext]'
credentials {
username 'example'
password 'example'
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.8'
}
build.gradle for java-lib:
apply plugin: 'java'
apply plugin: 'eclipse'
compileJava.options.encoding = 'UTF-8'
group 'example'
version '0.1.0'
status 'release'
sourceCompatibility = '1.6'
targetCompatibility = '1.6'
dependencies {
compile 'com.google.protobuf:protobuf-java:2.5.0'
compile 'com.google.guava:guava:15.0'
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
uploadArchives {
repositories {
add project.repositories.repo
}
}
build.gradle for the two Android libs (they are the same apart from dependencies and version numbers:
apply plugin: 'android-library'
dependencies {
compile project(':java-lib')
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
versionCode 1
versionName '0.1.0'
minSdkVersion 9
targetSdkVersion 18
}
}
And finally, build.gradle for the Android apps (again, almost identical):
apply plugin: 'android'
dependencies {
compile project(':android-lib1')
compile project(':android-lib2')
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
versionCode 1
versionName '0.1.0'
}
signingConfigs {
release
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
if (project.hasProperty('storeFile')) {
android.signingConfigs.release.storeFile = file(storeFile)
}
if (project.hasProperty('storePassword')) {
android.signingConfigs.release.storePassword = storePassword
}
if (project.hasProperty('keyAlias')) {
android.signingConfigs.release.keyAlias = keyAlias
}
if (project.hasProperty('keyPassword')) {
android.signingConfigs.release.keyPassword = keyPassword
}
Perhaps it's a bug in Android Studio 0.3.0? I didn't experience it in earlier versions, but I want to make sure it's not just something in my build files.
Thanks a bunch for reading!
This was a bug which has now been fixed (working for me since Android Studio 0.3.5): https://code.google.com/p/android/issues/detail?id=61453
Also got this or very similar error (don't know if I did refresh) I see you posted bug at code.google.com and agree it must be their bug. My work around fix was just to cancel out of "not backed by Gradle" message and then I ran gradle build from command line. Then everything worked.

Resources