Gradle: interchangeably use AAR or project dependency - android

I have an AAR library that I want to distribute to partners along with a sample project that uses it. I want to also use the same sample project for manual testing while developing the AAR library. Thus, I would like to be able to use the library project as a dependency when developing, and the AAR file as a dependency when distributing.
I tried to define two flavors in the sample application:
productFlavors {
aar {}
project {}
}
...and then define dependencies like this:
dependencies {
//other dependencies
projectCompile project(':myLibrary')
aarCompile 'com.example.mylibrary:myLibrary-release#aar'
}
The aar flavor builds if I comment out the dependency for the project flavor, but if I leave both uncommented, Gradle sync fails if the myLibrary directory is not present - despite the fact that the project build flavor is not part of the current build variant.
What is the correct way to do this? Or do I have to choose between creating a whole separate project for distribution or always referencing the AAR even when debugging/testing?

Related

Packing run time dependecies into aar library

I build an Android Application which contains a module as library. Most of the logic is inside the LibraryModule. The library module has several other dependencies which are included as gradle dependencies
Application
* LibraryModule
*implementation ('dependency1')
*implementation ('dependency2')
The library module is include in the application's build.gradle like
implementation project(":LibraryModule")
The application works fine then.
But when I first build the Library module as an aar file using
gradle :LibraryModule:assemble
and then using the applications build.gradle to include the aar
implementation(':LibraryModule#aar') The application compiles. But several of the classes of the dependencies (dependency1, dependency2) which are required at run time are missing from the aar.
Is there some way to include all the contents of the dependency1 and dependency2 in the aar file so that run time dependecies is also packed together in the aar file.
I have done some googling and found out that fat aar is an option. Is there some way to include all the class file from the dependeices also into the aar file which are also needed at run time ?
Building a fat-aar is not officially supported by Android. There are some similar discussions at Android gradle multiproject dependency resolution

Android imported AAR how to keep changes in sync with original AAR project?

If I have project-A which is a SDK and its being exported as a AAR into project-B which imports the module as a AAR how could i go about linking the two modules so changes made to the AAR in project-B don't have to be duplicated in the original project-A?
Create a Project A (library), create project B(application) and put them in the same project.
update the settings.gradle file like this:
include ':projecta', ':projectb'
And make sure the projects are in the same directory
.gradle
.idea
projecta
projectb
etc
By doing this you won't have to explicitly add the aar to project B because they will be in the same project and both automatically be built when you make a release.
Edit about linking with multiple projects
In the case you want to have links to multiple projects your best bet is to create a maven repository.
Doing so will allow you to use your project in the following way:
build.gradle
dependencies{
implementation com.mydomain.projecta:1.0.1
}
Any project you want to use you project A with can be used by writing the line above.
When you update project A you update your maven server and change the version to 1.0.2
You also will have to update the dependencies in the linked projects:
dependencies{
implementation com.mydomain.projecta:1.0.2
}
I think that is the most stress free way to distribute your SDK in one place and easily resuse it in multiple places.
Here are some links about setting up a maven server
https://inthecheesefactory.com/blog/how-to-setup-private-maven-repository/en
https://www.androidauthority.com/add-a-github-library-to-android-studio-using-maven-jcenter-jitpack-777050/

dependencies issue when adding library AAR

I've created an Android library that I want to be able to use in other Android applications. I've created a small test app in the same project as the library and everything works.
To test integration with other apps, I created a new application, and followed this guide to import my library to that new application by creating a new module from the library's AAR file.
build.gradle file:
dependencies {
...
compile project(':my-sdk')
...
}
settings.gradle file:
include ':app', ':my-sdk'
Compilation succeeded and I can use the library's API inside the test app, but when running it I get a runtime exception
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/MediaType;
I assume it's a dependencies issue, since the library uses okhttp as a dependency, which is probably not being packed in the AAR file. I just couldn't find anywhere how to compile this dependency so it would work in other projects.
I fix it adding jar files instead of using maven into my module, then assemble release aar, and in the app project disabling "Instant Run".

Multiple AAR files

I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.
If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}

Android Gradle main project and submodule flavors

I have a main Android project, which depends on a submodule.
The main project has flavors defined in Gradle.
Also the submodule has a few flavors defined. This should be logical - to be able to have flavors for both projects.
For example in the submodule:
productFlavors {
flavorName {
}
}
But this does not work - The build crashes with a message saying that submodule resources are not found in the main project.
But when I delete flavors from the submodule, everything works fine.
It seems to mix the build order when flavors are defined for the subproject, could this be true?
What am I missing? Is it even possible for both main and sub projects to have flavors?
When you reference your sub-modules as dependencies in your build.gradle file, be sure to specify which flavor of the sub-module you are referring to:
dependencies {
compile project(path: ':module', configuration:'yourflavorDebug')
}
then be sure in your build variants, you are building the flavor your main module depends on, and everything should work out.
Also be sure the libraries you are referencing have this in their build.gradle as well:
publishNonDefault true
Without it android studio doesn't seem to be able to depend on flavors of that module. More information here.

Categories

Resources