Android Studio: including AAR library from a library project - android-studio

In my Android Studio project I have two subprojects/modules: an Android application (App1) and an Android library project (LibraryProject1). App1 depends on LibraryProject1. So far so good.
However, LibraryProject1, in turn, needs to import an AAR library to work properly.
So my Configuration is as follows:
App1 includes LibraryProject1
LibraryProject1 includes dependency.aar
Now, to include dependecy.aar I use the method detailed here:
How to manually include external aar package using new Gradle Android Build System
So basically in my build.gradle for LibraryProject1 I have:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile (name:'dependency', ext:'aar') //my AAR dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
Obviously, I put my dependency.aar file in the libs directory of LibraryProject1
However, this doesn't work. It seems that the repository added by LibraryProject1 is completely ignored and the local "libs" folder is not included as a repository, causing compilation to fail.
If I add the repository from the App1's build.gradle it works, but I don't want to do that, it's LibraryProject1 that needs the AAR file, not App1.
How can I do this??

Well, I found a way, but since it's very "hacky" I'll leave the question open, in case anyone comes up with a better, "proper" solution.
Basically the problem is that the flatDir repository is ignored at compilation time if included from LibraryProject1's build.gradle script, so what I do is I use App1's build.gradle to "inject" the flatDir repository in LibraryProject1. Something like this:
//App1 build.gradle
dependencies {
//get libraryproject1 Project object
Project p = project(':libraryproject1')
//inject repository
repositories{
flatDir {
dirs p.projectDir.absolutePath + '/libs'
}
}
//include libraryproject1
compile p
}
This effectively allows LibraryProject1 to include the external AAR library without having App1 include it. It's hacky but it works. Note that you still have to put:
//LibraryProject1 build.gradle
repositories {
flatDir {
dirs './libs'
}
}
inside LibraryProject1's build.gradle otherwise, even if the project itself would compile fine, the IDE wouldn't recognize the types included in the AAR library. Note that the ./ in the path also seems to be important, without it the IDE still doesn't recognized the types.

I faced to the same issue, and I figure out it by putting all libraries on that depends LibraryProject1 in LibraryProject1/libs as a .jar.
I think that aar library cannot be linked to another aar library.
Hope that help you,
Best regards

Related

Android Studio - Too many libraries imported to project eventhough they are not compiled in dependencies

I am creating a new Android application that using Firebase. I followed goolge guide to add Firebase SDK to my project.
below are my project gradle:
buildscript {
repositories {
jcenter(){
url "http://jcenter.bintray.com/"
}
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
And application build.gradle
....
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
/* For Google Play Services */
//Firebase
//addd firebase notification - messaging.
//add firbaes dynamic link:
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.google.android.gms:play-services-safetynet:9.0.2'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.firebaseui:firebase-ui:0.2.2'
compile 'com.google.firebase:firebase-messaging:9.0.2'
compile 'com.google.firebase:firebase-invites:9.0.2'
compile 'com.google.android.gms:play-services:9.0.2'
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
I checked in External library of my project. There are too many library that I don't not need such as: play-service-location-9.0.2, play-service-maps-9.0.2, play-service--nearby-9.0.2...
enter image description here
Could you explain and help me reduce unused library that I don't added into my project ?
Try removing the dependencies you don't need by deleting the dependency from the build.gradle one by one, if you get an error after removing one of your dependencies, add the dependency you just removed back.
As long as you get no errors or problems when removing the dependencies you don't need, everything will be fine.

Download and commit gradle dependencies and plugins in Android Studio

This is an excerpt from a build.gradle file for one of my modules. I'm using android-studio-1.5.1 and gradle 2.10.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I also have classpath 'com.google.gms:google-services:3.0.0' in the project level build.gradle file.
I'm trying to gather all the associated jars into a directory which I can commit to my git repo. Something like:
task copyRuntimeLibs(type: Copy) {
into "${projectDir}/libs"
from configurations.compile
}
(This does not work)
Also, I'm not trying to download the sources or javadocs.
I need to be able to commit all dependencies so that the project can be shared on an intranet without internet access.
I've written a plugin which will download all jars and poms. See the code here to download all jars and all poms from a Gradle Configuration
Note: There's a failing test here which shows that the parent pom's are not being downloaded. I've raised an issue here on Gradle's github.
I will likely improve the plugin to invoke the Maven Model Builder APIs to get the parent poms.
FYI - I've already integrated the ModelBuilder APIs successfully with Gradle (see here and here) so shouldn't be too difficult.

Where do i add my dependencies? In which build.gradle to put them?

I am requested to add a few dependencies. I know They should be added on build.gradle, but in the dependencies section is written:
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
I am new to android so from my small experience and googling there are supposed to be two build.gradle files, and only in one of them I should add dependencies, but I can not find an extra build.gradle file!?
I will be happy for help! Where should I add my dependencies and where did my second build.gradle disappear?
Gradle is a bit of an odd tool.
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
They state here how it works. There is only one build.gradle per project that will pull and manage dependencies for you.
Android Studio extends this. There is one 'main' build.gradle for the entire project, and then for each submodule there is a build.gradle since they are run as separate programs. in the master project build.gradle, put dependencies that effect everything you are doing in the build process, and then for each module dependencies specific for those modules. That's what it's saying.
http://developer.android.com/tools/building/configuring-gradle.html
**EDIT: **
Android Studio docs:
Declare dependencies
The app module in this example declares three dependencies:
dependencies {
// Module dependency
compile project(":lib")
// Remote binary dependency
compile 'com.android.support:appcompat-v7:19.0.1'
// Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar']) }
Each of these dependencies is described below. The build system adds all the compile
dependencies to the compilation classpath and includes them in the
final package.
Gradle docs:
Example 7.1. Declaring dependencies
build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Dependencies can be listed in a bunch of different ways.

How Can I Write A Dependency List On The Fly

I occasionally get code drops from a third party as a zip file of jars that I need to use as dependencies.
I want to unzip the jars into a flatDir repository and update a dependencies.gradle file automatically. I can do the unzip just fine, the writing to the file is the tricky bit.
Lets give an example using open source libraries:
If the dependencies.gradle originally looked like this:
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
compile 'commons-codec:1.2'
compile 'commons-logging:1.2'
compile 'log4j:1.2.17'
}
Then a Zip file arrived containing commons-codec-1.10.jar, commons-logging-1.2.jar and guava-18.0.jar. The dependencies.gradle would have to be updated to:
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
compile 'commons-codec:1.10'
compile 'commons-logging:1.2'
compile 'log4j:1.2.17'
compile 'guava:18.0'
}
I want to have specific versions and not just glob the whole flatDir as there might be different versions of the same library in the flatDir.
I can't use normal filtering as the # symbol wont be in the file.
The challenge is to recognise when a dependency already exists and needs updating, when a dependency already exists and doesn't need updating and when a dependency doesn't exist and needs adding.
I am using a Copy task with a zipTree to unzip so my thought is to use eachFile with a closure to do the work. However, I'm not quite sure how to do the steps outlined in the paragraph above.

Google licence Verification Library not getting referenced, android-studio 0.8.2

Being new to android and android studio(0.8.2), I am trying hard to implement Google LVL for my project.
I have followed the link:- http://developer.android.com/google/play/licensing/setting-up.html and lots more help from web.
I want to add LVL as library project and I followed the steps mentioned in this link:--
How do I add a library project to Android Studio?
1) Created a new module for LVL.
2) copied the code from the downloaded com directory to the newly created modules com directory.
3)I have added the proper dependency using dependency tab on main project.
Following imports are not compiling.
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
If I verify my configuration files, all looks proper.
Following is my android.xml
`
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
</application>
`
build file of this LVL module is:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
build file of the main app shows the added dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':apkExpansion')
}
settings.gradle has, which also looks proper:
include ':app', ':licensing', ':apkExpansion'
project(':licensing').projectDir = new File('library/licensing')
project(':apkExpansion').projectDir = new File('library/apkExpansion')
My project as the following setup:
MyProject/
| settings.gradle
+ app/
| build.gradle
+ librarary/
+ licensing
| build.gradle
+ apkExpansion/
| build.gradle
After looking your files closely, I see the probable cause is the package name in Android.xml of LVL library module.
When you create a new module for any existing library, the package name should match the package of library or else if you wish, you need to edit all files package deceleration.
You need to compile your library module separately to see if everything goes well.
Also, other than com folder you need to copy the res folder to your library module to avoid any further issues.
Hope this helps.

Resources