If i have say the appcompat-v7 dependency in my build.gradle and then I have another dependency that also uses the appcompat-v7 library are both of those compiled or is just one compiled and the other is ignored?
Reason I ask is I ran the gradle command that gives you your dependency tree and there were a lot of duplicated dependencies that are in other libraries but already declared in my app
Gradle resolves the dependencies according to some rules:
If they have the same version number, there is no problem and the dependency is added once with the given version number.
If they are imported twice with different version number, gradle uses a default conflict strategy to choose the "best one".
In all case, a given library is always added only once.
Related
I have only one dynamic feature module in my project called search, But when I try to build project, I get that Error:
[:search, :search] all package the same library [androidx.recyclerview:recyclerview].
Multiple APKs packaging the same library can cause runtime errors.
Placing each of the above libraries in its own dynamic feature and adding that
feature as a dependency of modules requiring it will resolve this issue.
Libraries that are always used together can be combined into a single feature
module to be imported by their dependents. If a library is required by all
feature modules it can be added to the base module instead.
Of Course, the first thing I did is to research about people who had the same problem And I found:
1- This Question
2- This medium article
Both Introduce the same solution (Use Android Gradle Plugin 4.0) and my project uses AGP 4.0.1, But the problem is that I have only one dynamic module called search. I don't have any other dynamic modules, even further I don't have the dependency of RecyclerView: androidx.recyclerview:recyclerview in my search gradle file, So this is maybe a transitive dependency.
Also, you can find that duplicated dependency in one of two ways:
1-Navigate to: PROJECT_NAME/module_name(In my case: search)/build/intermediates/
and then search for "deps.txt" file in that directory, Open the file and you will see all your module dependencies direct and transitive ones
2- run ./gradlew :module_name:dependencies task
If you tried to remove that duplicated line: androidx.recyclerview:recyclerview from "deps.txt" file, it gets generated again after each build.
That being said, I need some rule in my packagingOptions {} like exclude to prevent that conflict between search.aar and and any other search.* format
Can anyone help, please?
In my case, I removed the below from one of the modules
Note: not from the base module
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
I made a library, but when I tried to use it with implementation 'com.example:mylibrary:1.3.0' in my app's build.gradle, I keep getting an error saying the ConstraintLayout dependency (which the library uses but not the app) is not found. However it was explicitly defined in the library's build.gradle with implementation.
When I ran gradlew app:dependencies on the terminal, it shows that the library has no dependencies, even though it actually has 2. This seems to be the source of the problem, Gradle can't detect the dependencies of the library.
I didn't run into this problem for a while but when I decided to remove the ConstraintLayout dependency from my app an error appears during build.
When you're using implementation for the dependencies in your library, the project which is dependent with it will not see the dependencies. You need to use api instead of implementation.
Let say I have added Facebook and Twitter dependencies in my app.
com.facebook.android:facebook-android-sdk:4.22.1
com.twitter.sdk.android:twitter:2.1.0
When i look at Gradle tree, They come up with bunch of other transitive dependencies.
Now If Facebook uses com.android.support:support-annotations:24.1.1 and twitter uses com.android.support:support-annotations:25.0.3
Then which version gradle will use.
In gradle tree, It shows -> in front of older version of dependency. I learnt that this means gradle will use the newer version, and will not use the older version.
But this can be a problem, because some libraries are meant to run on the specific versions, and i have faced this problem.
In one of article i found out how npm manages these dependencies conflicts, but i am still unsure how gradle will manage between different version of same library.
You can't have different versions of the same library inside an apk.
As you mentioned, by default, Gradle puts the newest one to the build. If you want to specify a concrete version of the library that should be used in your project, you can add a direct compile (or implementation / api for Android Gradle Plugin v3+) statement with a required version to get one.
Also, you can force version using a special syntax, but it can lead to some problems later. You can find more information about version conflicts resolution in this post
I'm using Tommy Buonomo's excellent library (here on GitHub and this SO question where he introduced it) to add a position indicator to a ViewPager, however I am getting a gradle error about support libraries versions which raises a bigger question for me.
The specific error is:
Error:(39, 20) All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.0, 25.3.1. Examples include 'com.android.support:support-compat:27.0.0' and 'com.android.support:animated-vector-drawable:25.3.1'
(Note that this is flagged as an Error by Android Studio BUT the app still compiles and runs.)
The bigger question this raises is how do you properly handle conflicts in support library versions like this?
I have added the library simply by adding:
compile 'com.tbuonomo.andrui:viewpagerdotsindicator:1.0.1'
to my dependencies in my app level build.gradle and as I say everything is working well except for the error.
I'd like to keep my app as error and warning free as possible so I'd like to understand what I can do to resolve this.
This is the first time I have used a third party library in one of my projects so this is the first time I have encountered this specific issue.
So, some questions.
Must a library developer create various versions of their library that target specific other support library versions or can they simply create one library that supports all versions up to a specific version? (It would seem impractical to have to create specific versions as you don't know what versions a library user is going to be using).
Is a user usually able to change the version of other support libraries that the third-party library calls? I cannot seem to find anywhere in the external libraries section of the project tree that would let me do this for Tommy's library so I assume many libraries are like this.
Is the only option to wait for the library developer to create a new version that uses the same API level support libraries that my app uses? (Or to change the support library version that my app uses).
Would another option be to fork the library on github and change the support library versions it uses in the new fork? This then raises the issue of learning how to compile and use libraries and to republish them for others to use (maybe as a pull request to the original author so that they could incorporate the changes - this would be most in the spirit of Tommy having released the library anyway) - and would also require an understanding of all of questions 1-3.
I feel like having got this error I have opened a can of worms here, but I would like to understand more about how this should be fixed if I am to do things properly.
(FYI, I am building my app with compile and target SDK v27 in order to follow best practice of targetting latest API level. Tommy's library already has a pull request to support API v26 which is awaiting action)
You can force the dependencies to use specific version of support library, try adding this in your project level build.gradle file:
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "yourSupportLibVersion"
}
}
}
}
If you're using support library version 27.0.0 replace yourSupportLibVersion with 27.0.0
Explanation :
Why're we using subprojects?
In a multi-project gradle build, you have a root project and the subprojects. The root project is where the build starts from. Everything else, either added as a dependency in gradle or external libraries added manually, will be processed as a subproject by gradle.
What's ResolutionStrategy?
Quoting from the docs, ResolutionStrategy defines the strategies around dependency resolution. For example, forcing certain dependency versions, substitutions, conflict resolutions or snapshot timeouts.
Why exclude multidex?
The reason why we've excluded multidex from getting forced to our required version is that,the dependency name for multidex also contains com.android.support, which is com.android.support:multidex:someVersion where someVersion isn't the same as the support lib version, it's independent from it and that's the reason we're excluding it from being forced to the support lib verison. Note : If you've more such dependencies, you should add them in the if condition above.
and after we've insured that the dependency is indeed a support library ,then we're simply telling gradle to use our desired version for it.
The library you mentioned is using v25.3.1 of the appcompat library and hasn't been updated in last 4 months.
To avoid the support library version conflict, I think you should clone/download the library and include it manually as a module dependency in your project so that you can update the version of the appcompat library and use it without any problem.
Clone the git repository into another directory.
In Android Studio choose File → Import Module and choose the module directory (the one containing file build.gradle)
Then right click in your module app → open module settings → dependencies → add module dependency
Is there an easy way to get gradle to update dependencies to their latest available version?
For build reproducibility all my dependencies are defined with a version number like this in my build.gradle file:
dependencies {
compile 'namespace:package1:version'
compile 'namespace:package2:version'
compile 'namespace:package3:version'
}
Periodically I want to update every package to their latest version. Typically this is the first thing I do for a new sprint after making a release.
It's a real pain doing this manually for each package. Ideally I would like a command to update the build.gradle file for me but at the very least a command that prints out which package needs an update and what the latest version number is.
In ruby land I would run bundler update.
This is all I've been able to come up with. I will happily accept another answer if there is a less manual method of doing this.
In Android studio I replace every dependency version with a plus example: compile 'namespace:package1:+'
Sync or build the project which will cause all the dependencies to be resolved to their latest version.
In Android Studio place the cursor on each dependency line in build.gradle and press alt+enter a menu pops up and you can select Replace with specific version
Add to build.gradle:
plugins {
id 'com.github.ben-manes.versions' version '0.17.0'
}
Then you can do gradle dependencyUpdates to get a report of new versions. Unlike the eponymous Maven plugin, there doesn't seem to be a way of automatically updating the build.gradle yet.
More documentation: https://github.com/ben-manes/gradle-versions-plugin
It is not a really good practice as libraries can include changes that may break your code.
A common "tolerated" syntax for
compile 'namespace:package:major_version.minor_version.revision'
would be like
compile 'namespace:package:1.0.+'
considering revision is used by the library authors as bug fixes and improvements updates
Note:
I just did that and you could do
compile 'namespace:package:+'
Edit:
A Proof Of Concept of my latest comment you may want to test.
This was made in 5 minutes, so don't expect it to be perfect nor flexible.
I suffer from it, too. And the best way to check dependencies, even manually, is to go through Project Structure and search for the dependency name and see if there is a newer version.
The problem that this query only checks for the dependencies present in the Maven repository. At least it already goes for Google's.
Note: If you choose to add the dependency with the new version, this will add a duplicity in the your App Gradle, so be sure to delete the old dependency row.
###################
Another possible quick fix is through the command line:
./gradlew app:dependencies
This will generate an output like the one below. Note that the asterisk points to a possible new existing version.