integrating maven dependency into android Eclipse project, no JAR file - android

Ok, so, I'm developing an app for the Amazon FireTV, so I have to use Eclipse.
I'm trying to use this socket.io Java client library: https://github.com/nkzawa/socket.io-client.java
at the bottom of this post, i included the installation instructions, which I'm not really sure how to make work with my existing Eclipse project (I'm new to maven). so from my understanding, do i just add a pom.xml file and a test folder? Then paste in their "maven central code" into the pom.xml? Will this cause any issues with the other code in my project? Or, can I just copy and paste all their SRC code into my project, since it's MIT licensed? I'd rather learn how to do this the proper way. The project is not in JAR format, so I was thinking maybe copying the folder structure into my project then using the Project Properties, Add Library option to connect to my code? Maybe?
Their installation instructions, (available in their readme.md):
The latest artifact is available on Maven Central. Add the following dependency to your pom.xml.
<dependencies>
<dependency>
<groupId>com.github.nkzawa</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.3.0</version>
</dependency>
</dependencies>
Or to install it manually, please refer dependencies to pom.xml.
Add it as a gradle dependency for Android Studio, in build.gradle:
compile 'com.github.nkzawa:socket.io-client:0.3.0'

So, I learned that Maven Central has JAR files available for download. That way, you can just include them in your project via the Project Properties dependencies options. Without having to learn Maven.

You need to first understand how Maven works (and what pom.xml stands for). Maven is a tool that helps you automatically install dependencies (files need) for a given project. E.g if a project needs to process json files, it will need to "import" a json library which will then be a dependency for that project. When you add the dependency file above to your project, and run Maven install, it goes and fetches all the dependencies for your socket.io-client to work.
Unfortunately, Maven does work very well in building android application projects and can be fairly complex to setup correctly (from my limited experience). I would advise that you manually download the jar dependencies and then add them to your android classpath if you are not keen on investing a lot of time learning to use Maven.
To manually install the files .. you can create a default maven project (http://www.tech-recipes.com/rx/39279/create-a-new-maven-project-in-eclipse/) in eclipse, add the dependency file above to your pom.xml and run Maven-install. This will download the dependencies you need to your Maven local repository. You can then copy them from there to your android project.
Regarding installing the socket.io client you can find more on these steps here
http://denvycom.com/blog/socket-io-java-android-without-maven/

Related

volley error when importing module

I've been trying to add the volley library to my android project by downloading this repository https://android.googlesource.com/platform/frameworks/volley and then importing the module to my project by following this guide:
First get latest volley with git (git clone >https://android.googlesource.com/platform/frameworks/volley).
In your current project (android studio) click [File] --> [New] -->[Import >Module].
Now select the directory where you downloaded Volley to.
Now Android studio might guide you to do the rest but continue guide to verify >that everything works correct
Open settings.gradle (find in root) and add (or verify this is included):
include ':app', ':volley'
Now go to your build.gradle in your project and add the dependency:
compile project(":volley").
When gradle tries to sync the project after I've imported the module i get the following error message : Error:Could not download layoutlib-api.jar (com.android.tools.layoutlib:layoutlib-api:24.3.1): No cached version available for offline mode.
I believe you want to use Volley and not contribute to the volley library. if that is the case there are better ways to get volley in your project. let Gradle handle it. Gradle is Build Management tool just like Maven but better, As it gives you more freedom and flexibility.
How to add libraries to gradle in android studio. this link shows a how to add libraries to your android project.
As Android Studio uses Gradle things are pretty neat as it uses the middle ground of ANT and Maven for Build and Management tool.
for volley you can add
compile 'com.mcxiaoke.volley:library:1.0.19'
to the app module of your project. this library can be looked up on Github
or you can do that using android studio's GUI, click on File -> Project Structure
example

Cling how to install from github?

I trying include to project Cling, but before I never used manually install from maven.
On page instructions is:
Install Maven 3.2.3 or newer.
Install the Android SDK and set the ANDROID_HOME environment variable
to the SDK install directory.
Clone the Cling source:
git clone https://github.com/4thline/cling.git
Change into the cling/ directory.
Install everything into your local ~/.m2 Maven repository (this will
take a few minutes if all dependencies have to be downloaded for the
first time).
mvn clean install
If your build fails with Android/dex packaging errors, you forgot the clean.
Use Cling in your pom.xml with:
don't know why, but pom.xml not insert here
you can see pom.xml on github page
I have done 1,2,3,4 steps, but what is "Install everything" in step 5, how to do it ?
And last step with pom.xml, where need to put it?
Step 5 comes down to running the command mvn clean install from the command line.
Maven is configured with the help of a file, called the POM file. It is an XML file named pom.xml. This file contains everything that Maven will do during the build. One of those things is to compile the Java sources into a final artifact. To compile the source code, it needs to resolve its dependencies; that is, other libraries that Cling depends on. All of those required libraries are declared in this POM file.
Maven will automatically download every dependency of the project. It will store them (or install them in the Maven jargon) into a local repository. This repository is just a directory structure on your local drive that will contain every JAR and POM that Maven will have downloaded from the Internet (more precisely from remote repositories configured for the project).
Maven will only do that process once. When all the dependencies are installed in your local repository, it won't download them again (by default). That is why the very first build will be longer that the subsequent builds.
So, to go through step 5, you need to:
Open a command prompt
Go into the directory where you checked out Cling with the command git clone https://github.com/4thline/cling.git at step 3.
Go into the cling subdirectory.
There should be a pom.xml file here. This is the main entry point for Maven. Run the command mvn clean install from this location.
Step 6 targets the project you are building. When steps 1 to 5 are done, you have compiled and installed the latest version of Cling. Now is the time to use it then!
Well to use it, you need to create a Maven project (there are facilities for that with every major IDE like Eclipse or IntelliJ) and declare that your project will have a dependency on Cling. That declaration is done with this bit of XML in the POM file of your project.
<dependencies>
<dependency>
<groupId>org.fourthline.cling</groupId>
<artifactId>cling-core</artifactId>
<version>2.1.1-SNAPSHOT</version>
</dependency>
</dependencies>
I strongly suggest that you read the Maven book from Sonatype to get you acquainted with using Maven.

add library from Git to Android Studio

I know this must be a pretty basic question, but I'm new to Android Studio and gradle, and I can't find any up-to-date info on this.
I'm trying to add this library to my project: android-segmented-control.
It doesn't look like I can add it to my build.gradle file (correct?). I'd like to do it that way, of course, and not download the project if possible.
If I do need to download the project, how do I link it up with my existing project? Again, I haven't been able to find anything that is current that describes this process for Android Studio 0.5.3
The library you mentioned does not seems to be pushed on maven central or any other maven repository. As this library contains resources files, you cannot add it as a jar.
The only way to use it is clone the git repository and add it as a module to your android app project.
Meanwhile, you can ask the author to make it available on a Maven repository like OSS sonatype
Thanks #Thomas Bouron for the hint !
I have pushed my library to maven center, so you just need to add the following dependency to your build.gradle.
dependencies {
compile 'info.hoang8f:android-segmented:1.0.0'
}
(A little late for #workInAFishBowl but it may be helpful for others.).

The best way to integrate third party library in Android studio

We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:
Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".
Is there a general rule to add third party source or jar files into an existing android studio project? Thanks
I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to build.gradle file:
Specify that you want to use maven central repository for your dependency
repositories {jcenter()}
Add compile dependency to gson 2.6.2
dependencies {compile 'com.google.code.gson:gson:2.6.2'}
Android Studio as well as your CI server should easily build your project now. And you can continue app development.
I prefer to use central repository for dependencies management because:
easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
lighter repository and easier dependency upgrade
Examples:
Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars
I should notice:
Not many libraries are in maven central and you should make some effort to use them such way in your project
You could much easier get to "class already added" error during dexing with central repository approach
You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system
This series of steps was taken from Android Studio: Add jar as library? and is not my original answer. I am posting them here, again, because your question was the third in search results on Google when looking up this same topic. Hence, copying.
All credits to the one who wrote the steps.
Download & Copy Your .jar file in libs folder then adding one line to build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
compile 'com.google.code.gson:gson:2.3.4' ----------> I added this one
}
Do not forget to click "Sync now"
I´m using Android Studio 1.1.0
Download and copy your jar to libs folder then add the following to your app.gradle file and SYNC.
dependencies {
compile 'com.google.code.gson:gson:{version_you_need}'
}
repositories{
flatDir{
dirs 'libs'
}
}

State of Android project dependency management

We are standardizing our infrastructure for Android development and we are trying to incorporate dependency management to our Android library projects. My current track is using the maven android plugin with m2e-android. We have uploaded the Android artifacts to our Artifactory repository with the Android SDK Deployer. We also have an internal framework with a few libraries we can import into our projects and for most part it works fine.
The issue we are facing now is that apklib dependencies containing resource files are hard to set up. The maven plugin can correctly configure the classpath but if the apklib has resource files that needs to be referenced by the parent Android project, Eclipse is unable to find them unless you checkout the Library Project and link it to the parent project through ADT.
After reading m2e-android discussion on issue https://github.com/rgladwell/m2e-android/issues/8, https://stackoverflow.com/questions/6269816/creating-closed-source-android-libraries#answer-6270768 and APKLIB does not get installed in Maven Repo, I'm not convinced maven is the way to go until ADT properly support closed source apk libraries.
I'd like to know how are you handling these kind of dependencies on your Android projects. What strategies are there other than using Maven?
For reference, here's what we have tried so far.
No dependency management. All required jars are stored into the lib folder and pushed to the source control repo. Library projects are set up as subfolders and pushed to the source control repo for each project they are used in. Eclipse project settings are also pushed. Project built with standard ADT Ant script.
Jar dependencies into libs folder and library dependencies as git submodules. Project built with standard ADT Ant script.
Dependency management with maven, including library projects with apklib packaging. Issue with resource files in apklibs.
You can have an insight on how Facebook Android developers address their dependencies issues in this video: How Facebook Built Facebook for Android.
They use Buck for that. Buck is a build system for Android that encourages the creation of small, reusable modules consisting of code and resources. Buck is in github
This might not be the best solution for you but maybe for someone else.
With the advent of Android Studio and Gradle, we are no longer facing issues with project dependencies; Android or otherwise.
Gradle supports Maven dependencies in jar or apklib formats. Popular libraries have been exported to the apklib (aar) format and made available through Maven.

Categories

Resources