How to import a maven module to an Android Studio project - maven

I would like to include retrofit as a module in my Android Studio project. The problem is that retrofit is a maven project and so Android Studio won't let me import it. Is there a way around this?
A similar question has been asked before, but it received no answers.

Use a custom group and/or artifact in the POM of your clone, so your clone cannot be confused with the original.
Build and install your clone of Retrofit using Maven as usual: mvn install. (Using the command line or an IDE other than Android Studio.) You have to build your Retrofit clone manually after each change you make to it, for Gradle to see the changes.
Add the local Maven repository to your Gradle script. See https://docs.gradle.org/2.5/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html#org.gradle.api.artifacts.dsl.RepositoryHandler:mavenLocal():
repositories {
mavenLocal()
}
Add the GAV of your clone as a dependency to your Gradle script:
dependencies {
compile 'com.yourgroup:retrofit:1.9.0-custom'
}

Go to your project then goto the app. You will see a build.gradle file under app (DO NOT use the gradle under gradle folder but the ine under app folder). Add this line.
dependencies {
....
compile 'com.squareup.retrofit:retrofit:1.9.0'
...
}
Then, make sure that you define the repository details in directory and add the url.
repositories {
flatDir {
dirs 'libs'
}
maven { url 'http://download.crashlytics.com/maven' }
}``

See Migrating from Maven to Gradle. Just execute gradle init.

Just add it to the dependencies { } block of your application's build.gradle file.
dependencies {
compile 'com.squareup.retrofit:retrofit:1.9.0'
}

In android studio simply go to project structure -> module you want to add retrofit -> dependencies tab -> plus(add) -> library dependency and then type retrofit in text box and select com.squareup.retrofit:retrofit and add to your dependencies

another soluation , just download latest jar file from https://github.com/google/retrofit then
goto new module ->create module of jar->select the path of that jar file->then on your project module gradle dependency add(implementation(':name retrofit module').

Related

What is maven { url "https://jitpack.io" } in build.gradle file meaning?

What is maven { url "https://jitpack.io" } in build.gradle file meaning?
Do I have control over project or I depend on jitpack.io developer account/libraries?
jitpack.io compile source code from github
By adding this repository to your project, you can use public's github repository in your project.
I don't know if it is possible to use a private repository requiring an account.

How to check where gradle is downloading its dependencies from?

I have a project using gradle from building and due to some recent jcenter issues, we want to move all our dependencies to our artifactory. Now i have all the configuration ready and declared inside the gradle build files. I have also removed all traces of other maven repositories.
repositories {
maven {
url "${artifactory_contextUrl}/repo_name-generic"
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
}
}
After doing a gradle clean build, i expected a lot of errors as i have never added those dependencies to our artifactory repo, but none came. The project builds fine and i can see them being downloaded.
My question now is, why is it working?
Is there a way i can check from which source the dependencies are coming from?
Also in artifactories GUI i cannot locate the packages inside the specified repo
Thanks!
If you ant to print the repositories a project is using, the following task will do it for you:
task printRepos {
doFirst {
project.repositories { repositories ->
repositories.forEach { repo ->
println("${repo.name}: ${repo.url}")
}
}
}
}
If you add it to your gradle build and invoke it gradle printRepos, you should get the repositories printed out
For more information regarding the API, check: https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/RepositoryHandler.html
https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
Also thanks to Bjorn for pointing out the doFirst/doLast problem.
rm -rf ~/.gradle/caches
./gradlew assembleDebug -i > ~/Desktop/dump
search dependency lib in dump file to check where it was downloaded from
For example, flexbox-2.0.1 was downloaded from jcenter:

Multi-project gradle build with Nexus artifact repository

Consider the following multi-project gradle build, where ProjectA depends on ProjectB, and ProjectB depends on ProjectC. Dependencies are specified in the form compile project(':ProjectX'). This requires that each project be checked out in order to build, say, ProjectA.
I want to use an artifact repo such as Sonatype Nexus, to make the build simpler for developers so that if they are only working on ProjectA, then they do not need to have the dependent projects checked out, and they can be retrieved from Nexus. Similarly, if you are working on the dependent projects and they are checked out locally, I want them to be build instead of being retrieved from Nexus.
In summary, the dependency resolution strategy is: If project dependency and checked out, build locally, else retrieve from Nexus
How can I achieve this in gradle?
I got this working by flipping my dependency definitions from compile project(':ProjectX') to compile "my-group:ProjectX:version", and use the following dependency substitution resolution strategy:
configurations.all {
resolutionStrategy.dependencySubstitution.all { DependencySubstitution dependency ->
if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == "my-group") {
def proj = "${dependency.requested.module}"
if (new File(rootDir, '../' + proj).exists()) {
dependency.useTarget findProject(":${proj}")
}
}
}
}

JavaFXPorts project as a library for other JavaFXPorts projects

I currently struggle with probably very simple problem: How can I use one JavaFXPorts specific project as a dependency within another project of the same kind?
With pure separated projects, I have no idea how to combine them (have to admit, I'm not as up-to-date with Gradle, as I probably need to...).
I thought about using the apply plugin: 'maven' to install and grab the library to and from the local maven cache. But then there would be no separation of the platform specific code (everything - main, android, ios, desktop - would be merged together into one single JAR file.
Maybe if I reduce the problem to the pure part of the main source tree, I could create a standalone JavaFX-only project, this should be easy...
Or I could access the other project via a relative path. But for whatever reason I kind of dislike that idea.
What is the best practice for JavaFXPorts? I simply don't what to copy some stuff over and over into new projects (obviously).
Thanks in advance,
Daniel
This is a very simple example of how you can set a Gradle multi project, containing two subprojects:
Common: a regular JavaFX (gradle) project, with common code that can be reused later on in other projects.
GluonApplication: a simple Gluon Mobile project, that makes use of the common one.
I've used NetBeans to create the Gradle Root project and add the subproject, but it can be done from other IDEs or command line as well.
1. Gradle Root Project
Create a Gradle Root Project. Set the project name (GradleProject in this case), the location, and a Maven Group ID (com.gluonhq in this case), and a Maven Version (default 1.0-SNAPSHOT).
2. Gradle Common Subproject
Create a new Gradle Subproject. Choose a name (Common), and make sure that the location of this project is the GradleProject folder. Select a main class (com.gluonhq.common.Common).
Add some code:
package com.gluonhq.common;
public class Common {
public static double sum(double a, double b) {
return a + b;
}
}
3. Gluon Mobile Subproject
Add a Gluon Mobile Subproject with the Gluon plugin for your IDE. For instance select Single View project. Choose a name (GluonApplication), and again make sure that the location of this project is the GradleProject folder. Select the package name (com.gluonhq.application) and the main class (GluonApplication).
You can run this project as is, from command line in the project root: gradle :GluonApplication:run, or from your IDE (tasks -> Run).
4. Include a Common dependency
Edit the build.gradle file from the Gluon Mobile subproject, and add the Common dependency.
Since both subprojects belong to the same root project you can simple do:
dependencies {
compile 'com.gluonhq:charm:4.3.7'
compile project(":Common")
}
Save and reload the project. Now on your code you can call Common.sum:
button.setOnAction(e -> label.setText("3 + 4: " + Common.sum(3, 4)));
Run again and see that it works. You can deploy to mobile as well.
5. Installing the Common module
If you plan to reuse the Common project in this or other Gluon Mobile projects, you can install it in your .m2 repository.
On command line, from the project's root, run:
gradle :Common:install
and you'll see that the project is installed under <user>/.m2/repository/com/gluonhq/common/1.0-SNAPSHOT, including the common-1.0-SNAPSHOT.jar file.
6. Reusing the Common jar
Finally, you can include the common jar in any of your projects.
For that, edit the build.gradle file from the Gluon Mobile subproject, include the local repository, and add the Common dependency.
repositories {
mavenLocal()
jcenter()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
}
dependencies {
compile 'com.gluonhq:charm:4.3.7'
compile 'com.gluonhq:common:1.0-SNAPSHOT'
}
Save and reload the project. And your common code will be available.
Of course, this works locally on your machine, but you can publish the artifact to a private or public repo as well, and then you should just include the proper repo in the above list.

How can integrate gradle project wih Eclipse (maven) based instrumentation tests

I have an android project in AndroidStudio, but we must outsource the Instrumentation test to Eclipse (simple Java projet which builded by maven). I don't would like to tell the whole development and QA story about why, I'm just kindly asking can anyone tell me, who can I integrate an maven independed instrumentation test project with my Android Studio based project ?
In gradle you can make a .jar from your project by building it, and then you can publish the .jar artifact to any maven repo (check this if your are not familiar with artifact publishing).
Something like this:
artifacts {
archives file("path/to/your/project_output.jar")
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://${System.env.HOME}/.m2/repository/")
pom.groupId = 'com.yourcompany'
pom.artifactId = 'yourartifactid'
pom.version = '0.1.0'
}
}
}
This will publish your project's compiled jar files to your local maven repository.
Look for the .jar file in the build/intermediates/... folder (classes.jar its default name I think)
Also, you should publish your .apk file too if your test project needs it.

Resources