Difference Between Library & Library Project & External Libraries - android

In the Android Studio IDE you can add a .jar file. You can also add entire projects that are 'Library Projects'. And there are also the Android libraries (from Gradle) added as External Libraries.
.jar
Library Project
External Library
I get that .jars are just that. But then how come other libraries (Library Projects) need to get added that have entire build files of their own (like gradle, res, src etc). And further complicating my understanding, the Gradle downloaded ones are added as 'External Libraries' and those have .jar files and a res folder.
Could you explain why libraries can be added as .jar, entire projects, or as external libraries?

Could you explain why libraries can be added as .jar, entire projects, or as external libraries?
You have different possibilities, because you can have different cases. Often the library is built by other team and you can't decide how it is distributed.
You can have a own library or a fork locally. In this case you have the code and you can add this library as a module
In this case just add in the module/build.gradle:
apply plugin: 'com.android.library'
and add the dependency in the main project like:
dependencies{
compile project(':module')
}
You can use a maven dependency. In this case someone uploaded the library in a maven repository.
Currently it is the best solution in my opinion.
In this case just add a dependency in your project
dependencies{
compile 'group:name:version'
}
This dependency can be a aar file, but also a jar file.
Also you can publish in a public or private maven your own libraries.
You can add an aar file in your project
In this case define your flat repository:
repositories {
flatDir {
dirs 'libs'
}
}
and add the dependency:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
You can add a jar file in your project
In this case usually you can put all jars in the libs folder and add the dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Related

why i should add dependencies that in aar file to my project?

I created the my Android Archive Library aar file that have it's own dependency in it's Gradle.
I use this library in many projects of mine, but why I should add aar dependency to my application dependencies? as we know benefit of using aar vs jar is aar can have and hold it's own resources and dependencies.
The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.
It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.
You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.

Reuse modules in Android Studio

Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.
Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?
You have different ways to achieve it:
using a local module referring the right path
adding an aar file
using a maven repo
CASE 1:
Using gradle and a local library, inside a project you can refer an external module.
Just use:
Project
|__build.gradle
|__settings.gradle
|__app (application module)
|__build.gradle
In settings.gradle:
include ':app'
include ':myLib'
project(':myLib').projectDir=new File('pathLibrary')
In app/build.gradle:
dependencies {
compile project(':myLib')
}
Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.
CASE 2:
Compile the library module, get the aar file, and then add to the main project:
Add the folder where you put the aar file as repository:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then add the dependency:
dependencies {
compile(name:'nameOfAarFile', ext:'aar')
}
CASE 3:
Another way is to to publish your module into a maven repository.
In this way, just use:
dependencies {
compile ('mypackage:mymodule:X.Y.Z')
}
I would compile your shared code as library project. So you will get an aar file which you can reference.
To create that Android Archive you need to build the project as a release build with this command:
gradlew aR
After that you have a file called <modulename>-release.aar this file is located in <projectroot>/<modulename>/build/outputs/aar. I rename those files to <modulename>.aar then you can put it into your lib directory of your module.
When done you can reference it from the module where you need it like this:
compile(name:'<modulename>', ext:'aar')
This also speeds up the build time since you don't need to compile the project anymore.

Get the issue with import external library in Android Studio

I want to import the library https://github.com/jpardogo/FlabbyListView
First, I download the Zip file, then I extract it then I copy it to the lib folder of my project, then I turn on my project in Android Stduio. I add this line in the build.gradle compile 'com.jpardogo.flabbylistview:library:(latest version)'. But the Android Studio show:
How can I fix it?
You are mixing two concepts. If you use the compile dependency you don't have to put the jar in libs, and if you put the jar, don't put the managed dependency.
What the line compile library:artifact:version does is putting in your classpath in compile time the corresponding library, downloading it for you from a maven repository.
That said, I suggest you to remove the .jar and change your line of compile to:
compile 'com.jpardogo.flabbylistview:library:1.0.0
if you are manually adding the library to android. then you will add it to you project also.
In you settings.gradle file for the project add
include ':nameoflibrary'
or
include 'lib:nameoflibrary'
Where lib is the name of the folder you added the library and nameoflibrary is the actual name of the library.
then in you actual module usually the app-module gradle file add
dependencies {
compile project(':nameoflibrary')
}
or
dependencies {
compile project('lib:nameoflibrary')
}
same analogy too.
But if you are adding it from the repository you only need to add this line of code in your app-module gradle
dependencies {
compile 'com.jpardogo.flabbylistview:library:(latest version)'
}

How is "libs" created and maintained in a gradle android project?

I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project.
Note: I'm using maven dependencies.
My question: Are people using custom scripts to generate this folder? I hardly think that this is generated once and then manually maintained when there is a newer version available.
Please enlighten me!
With Android Studio AND Gradle, there is no need to use libs folder (except for old .jar library).
In fact you can develop Android app whitout Android Studio as in your build.gradle there is already a apply plugin: 'com.android.application'
Gralde is using Maven or jCenter via gradle dependencies to import libraries. Gradle and Android Gradle plugin will automaticly download the libs as you sayed in a build/ folder. It is not static and can be clean with the Clean projet on Android Studio. Also, Android Studio will add a warning when a new library version is available automaticly in your build.gradle.
Dont miss the old libs folder used to import .jar library
I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project. Note: I'm using maven dependencies.
Don't confuse the libs folder with build/intermediates/pre-dexed/ folder.
Currently the gradle plugin for Android manages the build process and create these "internal" and intermediates folders.
My question: Are people using custom scripts to generate this folder?
I hardly think that this is generated once
You don't have to create this folder. The Gradle plugin for Android manage it for your. Also it will be deleted and recreated when your run a gradlew clean command.
and then manually maintained when there is a newer version available.
No. Your dependencies are defined in your build.gradle file.
When you define a new version, Gradle downloads the new dependency and updates the intermediate folders.
You can define your dependencies in many ways:
dependencies{
//You can create a folder and put jar files inside. You can use your favorite name, usually it is libs.
compile fileTree(dir: 'libs', include: ['*.jar'])
//The support libraries dependencies are in a local maven managed by SDK
compile 'com.android.support:appcompat-v7:23.0.0'
// A Maven dependency
compile 'com.squareup.picasso:picasso:2.5.2'
//A local library
compile project(':mylibrary')
//An aar file. It requires to define a repository.
//repositories{
// flatDir{
// dirs 'libs'
// }
//}
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}

How to copy or use native libs with Android Studio Gradle build?

I have a native lib in the
/libs/armeabi folder called libparser.so
and an associated jar file.
I changed the gradle build file to include the jar file, which seemsm to be easy (MYNEWJAR):
dependencies {
compile files('libs/android-support-v4.jar', 'libs/MYNEWJAR.jar')
}
But when I run the app, I think it cannot find the native lib:
E/AndroidRuntime(22569): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load parser from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.hybris.mobile.history-1.apk,libraryPath=/data/app-lib/com.hybris.mobile.history-1]: findLibrary returned null
E/AndroidRuntime(22569): at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime(22569): at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime(22569): at com.senstation.android.pincast.Pincast.<clinit>(Pincast.java:1299)
E/AndroidRuntime(22569): ... 17 more
Can you help me get the build file straight so it will include the native lib? This seems to be happening automatically on Eclipse, but i really want to use android studio.
Thx!
Sven
I found this answer from user Assaf Gamliel very useful.
And just made some changes to make it even more cleaner.
You don't need to rename the .zip file to .jar, just add it with a normal compile file dependency on build.gradle script. So, you would make a foo.zip file with a structure similar to this:
foo.zip ->
|--/lib
|--|--/armeabi
|--|--|--*.so
|--|--/x86
|--|--|--*.so
put it in your libs folder and then add it to gradle using compile files('libs/foo.zip'):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+' //gradle plugin update for Andoid Studio 0.2.+
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/foo.zip') //zip file should be in Module's /libs folder (not the Project's /lib)
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
while gradle does the build it just unzip the file you added preserving its structure.
To use an external jar library
Put the jar into the libs folder/drag the file onto libs from a file explorer
Right click it and select Add as library
Ensure that compile files('libs/your_jar.jar') is in your build.gradle file
To do this, modify build.gradle which is under [projectname]Project -> [projectname] in the project pane on the left.
For me, it was necessary to change
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
to
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile files('libs/universal-image-loader-1.8.5.jar')
}
Click Rebuild Project under the Build menu.
I did this today to get the Universal Image Loader library integrated with my project.
Create a directory called 'jniLibs' into 'app/src/main/' and put inside all the .so
app/src/main/jniLibs/
|---- armeabi-v7a/your.so
|---- armeabi/your.so
|---- x86/your.so
Hmm...I was HOPING that someone will provide a clear example, of
how to make a 3rd-party JAR file accessible to Android-Studio, by
showing the exact SYNTAX of what the resulting 'build.gradle' file's
dependency-clause would look like, after they've added their 'foobar.jar'
entry.
You know, something like:
=========
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'org.somedevs.foobar:Foobar.jar'
}
=========
[Otherwise, I don't stand a snowball's chance in hell of
guessing what the answers posted so far really mean. My
INTITAL clause contained the single 'compile' line...so my
GUESS would that one should add another such line!?!?]
EDIT: Yes, many THANKS, rebelious! I now, too, have it working.
[Instead of the 'drag/drop' onto the 'libs' in Studio, I have more
reliable results by just right-clicking on 'libs' in Studio and choose
"add as library...", after copying the JAR into that location, using cmd-line.]
The correct form for the dependencies clause is the form shown below:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile files ('libs/Foobar.jar')
}
Due to security reason, it's not possible to reference a local jar/aar file in an application project with the gradle android plugin.
For the support library, with the Android SDK Manager, you have to install the extra named Android Support Repository which will expose the support library inside a maven repository. Then you can add the support library in your project via :
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
For external libraries, you have 2 possibilities :
Build an aar file and deploy it to your local maven repository, then reference it in your project like you did with the android support library.
Put the library sources beside your application project and create a settings.gradle at root which will define the modules. (see the docs for more info).
On my side I would prefer build aar files because it's more modular.

Categories

Resources