Android aar file with dependencies - android

Currently I need to build an Android library .aar file. In the library have network request so that I have to add Retrofit as library's dependencies.
My library build.gradle file look like:
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}
But when using aar file by
compile(name:'sdk-release', ext:'aar'){transitive=true}
It's throw exception:
java.lang.NoClassDefFoundError
After research, I figured out that the Problem is "transitive dependencies".
Do we have any solution to fix this problem?
Many thanks.

Related

Migrating to Gradle 3.0.0: Error - D8: Program type already present

I am migrating my Android project to Gradle 4.4 and Android Gradle plugin 3.1.2.
It has a library module which depends on parceler library and defines its dependency as follows:
build.gradle of library module:
...
// parceler for serialization (https://github.com/johncarl81/parceler)
implementation "org.parceler:parceler-api:1.0.4"
annotationProcessor "org.parceler:parceler:1.0.4"
...
This seems to compile well and generates my aar file.
Further, my main app module also has a direct dependency on parceler module and contains above lines as dependencies in its build.gradle, along with above aar file.
build.gradle of main app module:
...
api(group: 'com.example.mylibrary', name: 'mylibrary', version: "1.0.7", ext: 'aar') {
transitive = true;
changing = true
}
// parceler for serialization (https://github.com/johncarl81/parceler)
implementation "org.parceler:parceler-api:1.0.4"
annotationProcessor "org.parceler:parceler:1.0.4"
...
Everything works until I try to generate my APK, which fails with the following error.
D8: Program type already present: org.parceler.Parceler$$Parcels$1
Task :MPCApp:transformDexArchiveWithDexMergerForRelease FAILED
When I expand my library project in Android studio, I see Parcels.class under org.parceler package. But it seems similar file is also generated by main app module under the same package which is causing the clash.
Upgrade to the latest (currently 1.1.10) - We got rid of the Parcels generated class.

Android .aar dependencies aren’t resolving in app project that depends on .aar

i have created an android library AAR in android studio 2.1.3 in which i use the following dependencies:
compile 'com.google.android.gms:play-services-vision:9.4.0+'
compile 'com.google.android.gms:play-services-wearable:9.4.0+'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'
now i am using this aar in an application but those dependencies were failing unless i add them to the dependencies of the new app.
i search here and i found that i need to add the following line:
compile (project(':LIBNAME-release')) {transitive = true}
but this didn't work. is there something i missed? or is it related to the obfuscation i did to the aar file? or is it a must to add these dependencies to the app?
Try to compile you project first:
dependencies {
compile project(':Name-Of-Your-Project')
}
This is Ashton Engberg's suggestion from that post

Cannot resolve symbol 'RequestQueue'

I am new to android studio and volley library so please bear with me.
I've added volley library by GitHub and then and added this line to build gradle file:
compile 'com.android.support:appcompat-v7:23.0.1'
And now I am trying some tutorials about request and response from api, but for some reason I cannot make an instance of RequestQueue why?
I am using Android 4.0.3 IceCreamSandwish
Thank you
add following to your build.gradle file
compile 'com.mcxiaoke.volley:library:1.0.19'
Google have added it to their own documentation pages and GitHub https://developer.android.com/training/volley/index.html
The dependency can be added using:
dependencies {
...
implementation 'com.android.volley:volley:1.1.0'
}
Add this volley repo clone to your gradle dependencies:
compile 'com.mcxiaoke.volley:library:1.0.19'
https://github.com/mcxiaoke/android-volley
Add the Volley library as a dependency in the module build.gradle file.
dependencies {
compile 'com.he5ed.lib:volley:android-cts-5.1_r4'
}
This is another mirror of the Google Volley library https://github.com/he5ed/volley
I would add it in the build.gradle:
compile 'com.mcxiaoke.volley:library:1.0.19'
you were adding
compile 'com.android.support:appcompat-v7:23.0.1'
which is a completely different thing (a backwards COMPATibility library for older API levels).

android studio how to use library dependencies already added in library module

--MyProject (module)
--build.gradle
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile project ':MyLib'
}
--MyLib (module)
--build.gradle
dependencies {
compile 'com.mcxiaoke.volley:library:1.0.16'
compile 'com.google.code.gson:gson:2.3.1'
}
In my library module called MyLib, I've added (for example) Gson & Volley as library.
I wish to use Gson and Volley api in the MyProject module without adding the libraries again as dependencies but I can't. And when I add Gson or Volley in the MyProject build.gradle, it causes an error:
multiple dex files define
How can I reuse dependencies that already added in the MyLib module? If this is not possible, how do I avoid this multiple dex files define error? I've tried Android Studio Gradle Error: Multiple dex files define
dexOptions {
preDexLibraries = false
}
but it didn't help.
Thanks in advance.
If you wish to use the Volley and Gson libraries in each of your modules, you will need to add both libraries as dependencies in both of your build.gradle files (MyProject and MyLib).
As for the multiple dex files define error, I would try taking a look at these potential solutions:
Android Studio build error - Multiple dex files define Landroid/support/v4/
Unable to execute dex: Multiple dex files define Lcom/myapp/R$array;
Good luck!

Using .aar NoClassDefFoundError But Class Exists and is Dexed

I have several projects which I build to create an .aar. I then import this .aar into into Android Studio under /libs. The build.gradle file for this dependency looks as follows:
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.android.support:multidex:+'
compile(name: 'customApi-debug', ext:'aar')
}
Since the library is quite large I have set multiDexEnabled = true. Android Studio finds the library and autocomplete works. Building works fine too but running the app gives the following error:
java.lang.NoClassDefFoundError: com.companyx.android.api.ui.vision.metaio.MetaIoView
at com.companyx.android.api.ui.vision.metaio.MetaIoView$$InjectAdapter.<init>(MetaIoView$$InjectAdapter.java:29)
I uncompressed and disassembled the .aar and dex files, respectively, and verified that the classes its complaining about actually exist. I've tried existing approaches for dealing with this problem but none of them worked.
Anyone else experienced this? Thanks in advance.
I run into the same issue. The fix is firstly to deploy the AAR file to a local maven (I utilized the plugin at https://github.com/dcendents/android-maven-gradle-plugin). Then I referenced to the local maven as described at https://stackoverflow.com/a/23045791/2563009. And eventually I declared the dependencies with a transitive option, like this:
dependencies {
compile('com.myapp.awesomelib:awesomelib:0.0.1#aar') {
transitive = true
}
}
The error would be gone then.
Just fyi, you can use a simpler syntax, which is valid too
compile 'com.myapp.awesomelib:awesomelib:0.0.1'
Don't forget to omit the #aar thing at the end of the library name

Categories

Resources