We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.
Related
We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.
We're making some library, basicly for our API, that we would make life easier for our external developers.
So we created new library project and put Retrofit and some other libraries as dependencies.
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
Now when we build it, it produces aar file.
But now when we put the aar file to libs directory and set it as dependency, we still have to put the same dependency in user's build.gradle file, which sucks. It should be taken from the library, right?
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'ourlibrary', ext: 'aar') {
transitive = true;
}
}
How to make transitive = true work?
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.
In your case adding transitive=true doesn't resolve your issue for the reason described above.
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.
I'm developing an android library that depends on some third party aars and jars.
Currently, these dependencies are declared in the library module's gradle buildscript like so:
repositories {
flatDir{
dirs 'libs', 'android-libs'
}
}
dependencies{
compile(name: 'threetenabp-1.0.5', ext: 'aar')
compile fileTree(include: ['*.jar'], dir: 'libs')
}
However, this results in the dependencies' classes being built into the aar, potentially causing conflicts when the library is used in an application.
How can I reference these dependencies from my library without actually packaging them into the library?
I have tried changing the declarations from "compile" to "provided" and then compiling the files into the application, but when I do this my library fails to build.
After some reading at https://docs.gradle.org/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies I eventually figured out that using compile fileTree will package the dependencies into the output library. To avoid this, declare each dependency individually, using the following syntax:
dependencies {
compile: name: 'filename-without-extension'
}
And the dependencies will no longer be packaged into the output.
The project making use of the output aar will still need to include the flat-dir repository that holds the jar files, like so:
repositories {
flatDir{
dirs 'libs'
}
}
My project is including some library project. Library is using some aar files and its dependecny is already defined in the module: gradle file. I am facing problem in including this library in my project.
If I keep duplicate aar files in app->lib and define their dependency in app->gradle file then there is no problem. But it shouldn't be the right approach.
Please find below the error:
A problem occurred configuring project ':app'.
Could not resolve all dependencies for configuration ':app:_qaDebugCompile'. Could not find :api-release:. Searched in the following locations:
https://jcenter.bintray.com//api-release//api-release-.pom
https://jcenter.bintray.com//api-release//api-release-.aar
file:/D:/sample/sample-android-app/app/libs/api-release-.aar
file:/D:/sample/sample-android-app/app/libs/api-release.aar
Required by:
sample-android-app:app:unspecified > sample-android-app:misnapworkflow:unspecified
please find below the project structure:
sample
|-- app
|-- misnapworkflow
|
|-- lib
|-- api-release.aar
In app gradle file following has been mentioned to include the project
dependencies { compile project(':misnapworkflow') }
Please find below the misnapworkflow gradle file:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 23
consumerProguardFiles 'proguard-rules.pro'
}
lintOptions {
abortOnError false
}
// Publish both debug and release libraries
publishNonDefault true
buildTypes {
debug {
debuggable true
jniDebuggable true
minifyEnabled false
shrinkResources false
testCoverageEnabled true
}
release {
signingConfig signingConfigs.debug
debuggable false
jniDebuggable false
minifyEnabled true
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
task grantPermissions(type: Exec, dependsOn: 'installDebugTest') {
logger.warn('Granting permissions...')
commandLine "adb shell pm grant com.miteksystems.misnap.misnapworkflow.test android.permission.WRITE_EXTERNAL_STORAGE".split(' ')
commandLine "adb shell pm grant com.miteksystems.misnap.misnapworkflow.test android.permission.CAMERA".split(' ')
logger.warn('Permissions granted.')
}
tasks.whenTaskAdded { task ->
if (task.name.startsWith('connected')
|| task.name.startsWith('create')) {
task.dependsOn grantPermissions
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
// Add dependency for MiSnap external API
compile(name: 'api-release', ext: 'aar')
// Add dependency for MiSnap
compile(name: 'misnap-release', ext: 'aar') {
exclude module: 'appcompat-v7'
}
// Eventbus dependency
compile 'de.greenrobot:eventbus:2.4.0'
// Add OPTIONAL dependency for Manatee
compile(name: 'manatee-release', ext: 'aar')
compile(name: 'cardio-release', ext: 'aar')
}
repositories {
flatDir {
dirs 'libs'
}
}
The aar file doesn't contain the 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.
For Android studio
Follow this steps:
Step 1:
Import .aar
File ---> New ---> New Module ---> (select) import .JAR/.AAR package ---> Next --->(select .aar file then)Finish
Now your existing project is imported.
Step 2:
Add dependencies
File ---> Project Structure ---> (Now you will get module list in left side at bottom.) ---> (Select app module) ---> select dependencies tab ---> click on (+) button ---> select module dependencies ---> (select module which you added) ---> ok ---> ok
Note: To check dependency is added
your build.gradle looks like
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.squareup.picasso:picasso:2.5.0'
compile project(':ScreenSharingSDK')
}
If I keep duplicate aar files in app->lib and define their dependency in app->gradle file then there is no problem. But it shouldn't be the right approach.
You're right, your app shouldn't define your AAR library dependencies in build.gradle. That's a common practice for 3rd party libraries like OkHttp, Picasso or RxJava. Those libraries are, in fact, has their own dependencies, just like your AAR library.
So, how come OkHttp, Picasso or RxJava doesn't ask your App to include their dependencies? Because they have included their dependencies on a POM file. A POM file contains configuration file for your AAR, including your artifact, group name, version, and its dependencies.
Let's take OkHttp as an example. OkHttp and its dependencies are stored in other people computer. Go to mvnrepository.com and search for OkHttp.
You will find OkHttp and its POM file.
<project>
<modelVersion>4.0.0</modelVersion>
<parent>...</parent>
<artifactId>okhttp</artifactId>
<name>OkHttp</name>
<dependencies>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>...</build>
</project>
When you include a library in your build.gradle(), Gradle will search that library on repositories define in top-level build.gradle. For OkHttp it was stored in mavenCentral().
repositories {
google()
mavenCentral()
jcenter()
}
Gradle will download the dependencies automatically, you don't need to specify library dependency on your App project.
But it shouldn't be the right approach.
The right approach is:
Store your library and its dependencies in a Maven repository.
You can use local Maven repository, host your own Maven repo, or publish your library on Maven Central or Bintray. inthecheesefactory has a good tutorial for that.
Create a POM file for your library.
When you deploy your AAR you have to include POM file. It can be done manually.
mvn deploy:deploy-file \
-DgroupId=com.example \
-DartifactId=your-library \
-Dversion=1.0.1 \
-Dpackaging=aar \
-Dfile=your-library.aar \
-DpomFile=path-to-your-pom.xml \
-DgeneratePom=true \
-DupdateReleaseInfo=true \
-Durl="https://mavenUserName:mavenPassword#nexus.example.com/repository/maven-releases/"
Or using android-maven-publish Gradle plugin.
gradle yourlibrary:assembleRelease yourlibrary:publishMavenReleaseAarPublicationToMavenRepository
Share your library to your peers:
In app-level build.gradle add the GAV of your library.
dependencies{
implementation "com.example:yourlibrary:1.0.1"
}
You and your peers should be able to use yourlibrary now.
In my case, following thing worked:
Put your .aar file in the libs directory(create, if needed), then, add the following code in your build.gradle(app level):
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'your_arr_filename', ext:'aar')
}
Monika Moon put me on the correct path but I don't have enough rep points to comment inline above. sigh
So a default Android app built in 2020 with Android Studio 3.5.3 will have the following project structure via the Project View:
--[yourAppName]
--[app]
--[gradle]
build.gradle <this is the TOP LEVEL one and NOT the one you normally mess with>
In the top level build.gradle files add the 'flatDir' item :
allprojects {
repositories {
google()
jcenter()
// needed so it picks up my aar files
flatDir {
dirs 'libs'
}
}
}
Then in your 'app' folder shown above. You will have these two key resources:
-- [libs] folder where you should drop your aar files
build.gradle file that is the one you add your aar dependencies to.
The default project build will already contain a 'libs' include for you but just in case your version doesn't have it this is what you need to add:
dependencies {
implementation fileTree(dir: './libs', include: ['*.jar'])
implementation(name: 'fileNameBeforeExtension', ext:'aar')
This is clean and works as expected.
The AAR file I'm using is an in-house custom built for internal hardware and will never be on a public repot.
I'm creating an Android App. All the business logic is in a library that I'm adding to my app project importing its AAR file.
But this library has some compiled project dependencies I'd need to use from the app, but don't know how.
LIBRARY PROJECT
apply plugin: 'com.android.library'
dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
compile(name: 'lib1', ext: 'jar')
compile project(':lib2')
I'm exporting this library as mobile-release.aar.
APP PROJECT
dependencies {
compile(name:'mobile-release', ext:'aar')
How can I access from my app project to those lib1, lib2 from mobile-release?
I tried adding
compile(:mobile-release:lib1)
without success. Thanks!
the short solution is to include your library aar file as module dependency in the application project, and then add the library dependencies
//in app projet
dependencies {
//....
compile project(':mobile-release') {
dependencies {
compile(name: 'lib1', ext: 'jar')
compile project(':lib2')
}
}
}
there are other solutions like publish you library to a local/remote repository, or create a function in the library gradle file that takes the jar dependencies and add them to the generated aar