I didn’t work full-time with Android in the last couple of years, and now whenever I try to fork someone code on GitHub I get a lot of errors since android tools and Gradle syntax are changing frequently.
I wonder what is the best way to handle these changes, and be able to upgrade other GitHub projects and some of my old projects to work with the latest Android tools. Here are some of the things that I struggle with:
I noticed some of the issues are related to changes in the Gradle syntax. How can I know what Gradle version the build.grade syntax was written with? and then how to upgrade it to the current version (is there a migration guide for Gradle versions?).
Sometimes I get issues related to tools that are not compatible with others, how can I know which version are compatible with which? and whats the easy way to manage that? here are some of these tools:
Gradle
Android Plugin for Gradle
Build Tools
Android Studio
How can I know what Gradle version the build.grade syntax was written with?
If the project contains a gradle/wrapper/gradle-wrapper.properties file, that is the version of Gradle that the developer of the project is using.
is there a migration guide for Gradle versions?
I am not aware of much Gradle syntax that would have changed that would affect Android developers for garden-variety projects. Most of the changes are from the DSL added via the Gradle for Android plugin. You can find the version of the plugin that the project developer was using via the classpath statement in the project's top-level build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
The above snippet is requesting version 1.3.0 of the Gradle for Android plugin.
Migration documentation for the Gradle for Android plugin is minimal. What there is can be found up on http://tools.android.com.
how can I know which version are compatible with which?
Here is Google's statement on the issue, though this has not been updated in a few months.
and whats the easy way to manage that?
If the tools complain, change them to a set that you know is good (e.g., by copying values from a known-working project). If you need something that was introduced in a newer version of the tools, change them to a set that you know is good. Otherwise, don't worry about them.
Related
How do I specify the version used for Android Lint? From the lint report: <issues format="5" by="lint 3.5.3">
Lint is a part of ADT(Android Development Tools) and more specifically it depends on the Android Gradle Plugin Version. You cannot change Lint version independently from gradlePluginVersion. When you change gradlePluginVersion, Lint version will be changed automatically to the corresponding one from current version of the plugin.
If you want to disable some checks or write your own rules you may check this. Also you may create your own 'Lint' modules using "com.android.tools.lint:lint-api:$lintApiVersion"
and "com.android.tools.lint:lint-checks:$lintApiVersion" compileOnly dependencies from here
Hope it helps
Hi I'm fairly new to android development. I have a question regarding the Gradle version and the distribution url in gradle-wrapper.properties
classpath "com.android.tools.build:gradle:$gradle_version"
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
So every once a while I need to update gradle version (because android studio requires it). How do I find the new version number. Sometimes I need to update the gradle wrapper too, so how do I find the new distributionUrl that works with new gradle version?
It feels no one talk about this part of the android development and there isn't good documentations especially after google io
The Gradle Wrapper version can be changed by executing ./gradlew wrapper --gradle-version=4.9 as described here. This will automatically update the distributionUrl in gradle-wrapper.properties. If you want to manually set the distributionUrl, you can rely on the URL conforming to the same pattern as in your example. In other words, you can simply change the part of the URL that specifies the version (4.4 in your example) to the desired version. Check the repository, you will see that all releases are available at URLs that conform to the pattern https://services.gradle.org/distributions/gradle-X.Y-DISTRIBUTIONTYPE.zip (with capitalization indicating variable parts). The list of releases are also available in more reader-friendly format on the "Releases" page of the Gradle Website.
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
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.
i just switched to intellij and pretty new to android development.. I've been working on one app for 5 moth and now that i moved it to intellij android studio my options menus became invisible. I've been reading a lot and trying to catch up with newest features that are available now today.
By biggest pain is that im hitting this error ->
Failed to refresh Gradle project 'ActionBarCompat-ListPopupMenu' You are using Gradle version 1.8, which is not supported. Please use version 1.9. Please point to a supported Gradle version in the project's Gradle settings or in the project's Gradle wrapper (
I did search on this problem and some pages suggesting changing classpath to soething like
build:gradle:0.7.+ but that doest help..
What am i doing wrong? All i need is just to make those examples from android to work..
Thanks
The Gradle wrapper file is at the path (project-root)/gradle/wrapper/gradle-wrapper.properties. The distributionUrl property is where you set the Gradle version; it's embedded in the URL:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
First make sure you've updated to the newest Android SDK Build-tools version, the most current one is 19.0.3; if you haven't, then open the Android SDK manager and update.
Then look in the build.gradle file inside your project folder (not the one in the root folder). This first couple of lines should resemble something like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
Set the class path to com.android.tools.build:gradle:0.9.+.
Then further along in the same file:
android {
compileSdkVersion 18
buildToolsVersion "19.0.3"
...
}
Set buildToolsVersion to 19.0.3 (the newest version).
Make sure Android Studio syncs the Gradle file changes. If it doesn't, restart Android Studio and/or rebuild the project. Then you should be good to go.
I think i figured this out.. Big thanks to Scott and Jaap.
I did updateof my intellij to 0.5.2, also i installed 19.0.3 ( newest build tools).
On few of the existing projects i had to change distributionUrl to
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
After rebuilding the project it worked like a charm.
Just a note to say : Scorr Berba's reply was probably the correct one. Also I'm not sure 1.11 can be used - I used the wrapper (there is a lot of confusion here : the wrapper is the gradlew ["gradle*w*"rapper] to set the gradle used for my samples to 1.10 and you must set up your project to use the wrapper OR you can build from the command line e.g "./gradlew build".