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

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')
}

Related

Android Studio Gradle dependency doesn't show up in External Libraries

I'm using Android Studio 3.0.1 and I'm trying to add an online dependency and while Gradle initially syncs without a problem it doesn't show my dependency in External Libraries and my code that references the dependency doesn't work.
Here's a snippet of what my build.gradle file looks like:
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/groups/public/' }
}
dependencies {
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
}
I'm pretty new to android development (took over an existing project from a dev who quit without leaving any documentation) so I'm not sure if this is a mistake with how to add a project dependency or if there is a problem with the dependency that I'm trying to add. Any help would be greatly appreciated!
I was able to get this to work by changing the dependency declaration to:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT', classifier: 'jar-with-dependencies'
The library artifacts up on the repository include an apklib and a JAR with a special classifier. The apklib format is not supported by Android Studio, and unfortunately the classifier on the JAR means that it's not accessible simply using the group-name-version format when declaring dependencies.
Your build.gradle file seems fine. If you want to keep the library specified as an external library, you can try and define the dependency using the alternative notation, replace:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
with:
compile 'com.fortysevendeg.android:swipelistview:1.0-SNAPSHOT'
The alternative approach is to download the jar file yourself and use it as a local dependency. If you navigate to the maven repository you can inspect the package which is included as a dependency and download the jar directly. Place the jar file in the libs folder of your project and add the following to your build.gradle file:
compile fileTree(dir: 'libs', include: ['*.jar'])
For further details on how to configure the dependencies of your gradle project, check out the Android Studio documentation here.
Based on the information you have provided, this should fix your issues. If this does not solve the error then there may be other issues with the project.
Your dependencies should not placed in the top-level build.gradle file where the repositories are defined. There is even a comment in that file that says so, by default.
You app dependencies should be the module's build.gradle along with the others like android-support
Additionally, that library is very old, and is a SNAPSHOT build, meaning it isn't meant to be generally used in a release environment. You should find an alternative... And there are plenty of other ListView swiping ones

Import github project into android studio

I want to use this library in my android application which has been developed in android studio IDE. I downloaded the zip file from githup and extracted, then paste the unzip file in a optional folder called 'subProject' in application root.
image.
Next I added this line to setting.gradle:
include ':app', ':subProject:rtree-3d'
and then change my application build.gradle to this as new dependency:
compile project(':subProject:rtree-3d')
But after cleaning and rebuilding my project I have this error:
Error:Configuration with name 'default' not found.
I also change top line to compile fileTree(dir: 'subProject', include: ['rtree-3d'])but there was no succeed.
How can I add this github project to my android application to use? Any help will be appreciated.
This isn't a gradle-project (but Maven (see the pom.xml?)), so the gradle build won't succeed, because it is looking for build.gradle-files.
You could build it with Maven, and add the resulting .jar file to your lib-folder of your project, in order to let gradle pick it up.
Or, even better, just add it as a dependency to your apps build.gradle
// https://mvnrepository.com/artifact/com.github.davidmoten/rtree
compile group: 'com.github.davidmoten', name: 'rtree', version: '0.8-RC10'
latest stable version is
compile group: 'com.github.davidmoten', name: 'rtree', version: '0.7.6'
Edit
Oh! my bad, actually rtree und rtree-3d istn't the same thing! So you would need to fork the rtree-3d project from github, build it with maven (clean install). Depending on your configuration gradle can access your local maven repo and pick the installed artifact from there.
But for builds on different machines you would need something like a shared Sonatype Nexus. This is something to do, so you could just copy the rtree-3d.jar from your local maven repo to the lib-folder of your gradle project and add it to your subversion/git.

How to download libraries in Android Studio?

Whenever I have to add certain library from the internet to my Android project, I add them inside the dependencies in the app level gradle script and it downloads the library for me. Is it possible to download these library files so that I can use them in other projects as well without downloading the whole library and dependency files again?
Just go to Maven central and download the libraries.
For example, here is Volley. Just click the download JAR button.
I would strongly recommend sticking with Gradle / Maven, though, to keep consistency with versions and appropriately handle additional dependencies for the libraries you want to download. They are called package managers for a reason, and they do their job well.
The libraries are actually downloaded to disk only once and shared between projects, they aren't downloaded for every new project.
Put library's jar file inside libs folder.
Add this line in module level build.gradle (if not present):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Other libraries
}
Find the gradle dependency for your library and put it in dependencies of your build.gradle file. For Example,
dependencies {
// other dependencies
compile 'com.google.code.gson:gson:2.6.2'
}
and then build the gradle file.
I'd just run into an issue where a coworker's AS instance wasn't syncing with the server. In that case, we simply had to manually invoke the "Sync Project with Gradle File". For whatever reason, his AS instance wasn't doing that automatically.

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'
}
}

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