build.gradle warning 'avoid using + in version numbers' - android

This is my build.gradle app-level file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "it.myco.app.myproj"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Android Studio highlights the line
compile 'com.android.support:appcompat-v7:26.+'
with the message
avoid using + in version numbers can lead to unpredictable and
unrepeatable builds
This line is auto-generated by Android Studio. Why this warning message? What I need to write to solve the warning ?

Someday, someone else may do a git pull on your code and then try build an apk. This apk may have different behavior or even get a build error or run time error, because the dependencies version can be different than the dependencies used when you created this code.
You should use an explicit dependency version. I suggest use the newest version. You can search on bintray jcenter to see the newest version. If you are using Android Studio, File -> Project Structure -> Modules -> (your module, app usually) -> Dependencies -> Add library dependency (click the + sign) will give you the newest version, even add library automatically.

You can use something like
compile 'com.android.support:appcompat-v7:26.1.0'

Replace it with the latest version of the library. Usually AS shows the latest version when you alt + enter on the warning.
More info on the reasons

Don't use + in dependency version. it means Android studio will use the latest library, which may cause problem in future. In android studio it's recommended to use all android library with same version, suppose you are using + and if any single library got updated then android studio will use that updated library, so the version for this library will be change. that cause problem to build application.
That's why gradle giving this warning. it's best practice to write full version number instead of +.

Why this warning message?
Here you can find a good blog about this topic.
Dynamic versions add nondeterminism to your build:
Dependencies can unexpectedly introduce behavior changes to your app.
The same source built on two
different machines can differ.
Similarly, builds built on the same machine but
at different times can differ.
Past builds cannot be
reproduced perfectly. This makes it difficult to revert safely.
There
are security implications if a bad actor introduces a malicious
version of a dependency.
What I need to write to solve the warning ?
Always specify your dependency versions explicitly.

It's a normal warning.
It's better you to choose a concrete version than going that way. This is because if you use "+" it will choose the newest, so in the new version it's possible you to get some deprecated or unexpected changes that will do your app to die. Think that if you are using it in a huge project you will have a lot of dependencies so it will be a chaotic environment.
Use specific versions and if there is a new one... update it manually.
To solve the warning and choose the last version, if you are using Windows, click Alt+Enter and it will choose the latest version.
If it doesn't work you will have to search for it on the Internet or in Project Structure > Modules >(your module) > Dependencies > +

Related

The sdk platform-tools version (26.0.2) is too old to check APIs compiled with API 27; please update

I'm using IntelliJ Idea Community Edition 2017.2.5 and am building an Android app.
My problem is that even if in my app (Gradle) I have:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.0"
defaultConfig {
applicationId "com.app.mtvtr"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:design:27.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:support-v4:27.0.0'
testCompile 'junit:junit:4.12'
}
Also, on first build of the project, compile for the appcompat, design and support was set to 27+ (which IMHO is bad practice and is already pointed by the Studio as a bad idea), so using which tools that I have, I have set it to 27.0.1, and later to 27.0.0 just for the testing if it might work..
In my Fragments and Activities I have:
And the warning says:
The sdk platform-tools version (26.0.2) is too old to check APIs
compiled with API 27; please update
Now, I know that there are topics on SO regarding old and new versions, but I have (to my belief) set the Gradle properly and I am nowhere using platform-tools version 26 or even 26.0.2...
I have tried Restarting and invalidating caches three times.
The project properly builds onto the device and everything works. But, of course, I want to fix the warning. (And not hide it like some would suggest..)
Two things that come to mind are:
I missed somewhere that I should have updated the version, maybe?
I need to start a new project from scratch.
Any ideas? :)
Android Studio 3.0 will be merged into IntelliJ IDEA 2017.3 version, which will make the com.android.tools.build:gradle:3.0.0 plugin compatible with it. That will make this error to go away.
For those of you who are experiencing this issue in Android Studio, check your root build.gradle file and make sure it is updated to the above mentioned plugin version (3.0.0), in order to be able to compile against API 27 with the current latest build tools (26.0.2).
You may consequently also have to update the Gradle wrapper to the latest (4.1 at the time of this answer).
You have just to launch the SDK Manager and download the Android SDK Platform Tools and SDK Tools.
Here the official doc.
It seems that it was an IDE problem because it worked without warnings inside Android Studio 3.0 as it was suggested by CrazyCoder.
It seems that at this moment IntelliJ Idea isn't updated enough to make this work properly. So even while I have been using Idea from the start, I will switch to AS for now. :)
Thanks for your input guys. :)

Porting from Eclipse and Upgrading Android Libraries

I have an Android application that we have been building in Eclipse for years, and I am finally porting it to Android Studio (I am new to Android Studio and Gradle, so forgive my ignorance). I have successfully ported it and the app is up and running in Android Studio, but now need to overcome a couple more obstacles:
I want to upgrade deprecated methods and also move to the latest versions of Support Library, Google Services and GSon (I guess I can live without upgrading GSon if need be).
To complicate matters, we have an Ant based build which takes the source code and generates numerous white label instances of application, with some pretty complex replacements. I plan on keeping that - I don't want to Gradle that just yet (and not sure Gradle could handle it?).
So, first it looks like my Gradle generated by the port has dependencies as follows:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.mydomain.myapp"
minSdkVersion 14
targetSdkVersion 23
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.4.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.android.gms:play-services:+'
[....other dependencies that don't matter here....]
}
My question is : do I still need to have the upgraded versions of these libs here or is this done via a Gradle addition for running out Android Studio? And if not by lib dependencies as listed, how can I have the Ant build pick these upgrades once I make them? I do have a further complexity in that I think the generated source code structure is different than Eclipse, but I think I can adjust the Ant build accordingly.
I have read these, and while helpful really don't quite answer my question:
Android white labeling
and
Android App White Labeling
Thanks!
UPDATE
So after a LOT of playing around I finally have upgraded modules and libraries and it apparently (I need to do a lot of testing) is working. Not sure if this will save anyone anytime in the future, but the key seems to be:
Match the compileSdkVersion and buildToolsVersion versions.
Match the com.android.support:support and compile and tools versions, as closely as possible.
For the Support lib, choose the version just below your minSDK version (in my case it is 14, so I chose v13). I might have it wrong there but that is what got it working for me.
Be careful in which modules and which version of the modules you bring in, and that they are compatible. The tool did a decent job of warning, but if you are bring in the entire play services I would recommend pulling in the pieces since that was where my incompatibilities were coming from. Here is what it looks like now:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.mydomain.myapp"
minSdkVersion 14
targetSdkVersion 23
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.android.support:support-v13:25.3.1'
compile 'com.google.code.gson:gson:2.7'
}
For anyone who finds the whole support library to be confusing and a mess (why would anyone think that?) this blog was very helpful:
http://martiancraft.com/blog/2015/06/android-support-library/
Thanks also to Gabriele Mariotti for pointing me in the right direction. I will address the White Labeling conversion to Gradle in another question. Please chime in if I have this all incorrect...I will be the first to admit this is all very confusing!
My question is : do I still need to have the upgraded versions of these libs here or is this done via a Gradle addition for running out Android Studio?
compile 'com.android.support:support-v4:23.4.0'
compile 'com.google.android.gms:play-services:+'
Using the latest version is always a good idea
You should avoid using the the wildcard + in your dependencies, because in this way you are not able to replicate the same build in different time (because it changes in the time).
Use the same version of support libraries. The gms libraries have a dependencies with the support-v4 and you should use the same version.
it is not a good idea to have all gms modules (com.google.android.gms:play-services). You can add only the required modules.
And if not by lib dependencies as listed, how can I have the Ant build pick these upgrades once I make them?
It is not so simple. In my opinion you should convert your ant script in gradle. It would be easier.

How to use Google Play Services 7.3.0 on AndroidStudio?

I've updated my Android SDK and also the PlayServices Library. Nothing seems to work right now.
The import com.google.android.*; does not exists, and I've added the 'compile' lines on the Gradle File.
Have someone experienced this? How can I solve this?
EDIT
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.google.code.gson:gson:2.3'
compile 'org.roboguice:roboguice:3.0.1'
compile 'com.google.android.gms:play-services:7.3.0'
}
That's the Gradle File for a Module.
If you use Android Studio and want to use the latest Google libraries via Gradle dependency, you will have to update the
You can do this via Tools -> Android -> SDK Manager -> You have to update the stuff under Extras
Also make sure that you are not using the Offline Mode of Gradle. You can find this option via (I did this on a Mac) Android Studio -> Preferences -> Gradle -> (Global Gradle Settings) Offline Work
Make sure this option is not checked.
Something which was mentioned by #CommonsWare already (and i add it just to sum up this answer) is to sync the project via the Gradle files. You can do this via the toolbar as seen here:
Or via the menu Tools > Android > Sync Project with Gradle Files
If nothing of the above is working for you, it would be really helpful if you can try to build the project via the command line or the terminal with the following command:
gradle clean build --debug
and then post the output here. It will hopefully show why it can't resolve the library.
Try Tools > Android > Sync Project with Gradle Files and see if that clears up your problem. There's a bug where Android Studio does not always warn you to do this with the yellow banner on the editor.

Support library version

I try to inject support libraries into my Android Studio project.
If I try anything lower than 20 it says:
library should not use a lower version then targetSdk version.
If I use compile 'com.android.support:support-v4:20' I get:
Failed to find: com.android.support:support-v4:20
If I use compile 'com.android.support:support-v7:20.0.+' I get:
Avoid using + in version numbers, can lead to unpredictable and unrepeatable builds.
So the simple question is:
where can I find up-to-date, ready to use, version numbers that Do work?
If I try anything lower than 20 it says: library should not use a lower version then targetSdk version.
That is because you set your targetSdkVersion to something higher than 19. If you did so intentionally, fine. If you did not do so intentionally, consider dropping it back to 19 for now, and use compile 'com.android.support:support-v4:19.1.0' (if you are using the backport of fragments) or compile 'com.android.support:support-v13:19.1.0' (if you are not using the backport of fragments).
If I use compile 'com.android.support:support-v4:20' I get: Failed to find: com.android.support:support-v4:20
That is because the Android Support package uses X.Y.Z semantic versioning, as do most artifacts in repositories. 20 does not match the X.Y.Z pattern.
If I use compile 'com.android.support:support-v7:20.0.+' I get: Avoid using + in version numbers, can lead to unpredictable and unrepeatable builds.
That is merely a warning. If you are using version control for your project files, and you feel that it is important to be able to check out some earlier version of your code and be able to reproduce that build, then using the + notation is not a good idea. OTOH, if being able to reproduce historical builds is not essential, using the + wildcard, as you are doing, ensures that you get updates automatically. Having the + in the third position (Z in X.Y.Z) means that you will automatically get patchlevel updates.
where can I find up-to-date, ready to use, version numbers that Do work?
On your hard drive, in $ANDROID_SDK/opt/extras/android/m2repository/com/android/support/$LIBRARY/, where $ANDROID_SDK is wherever you installed the Android SDK and $LIBRARY is whichever Android Support package library you are interested in (e.g., support-v13).
To see the current Android Support Library revision number ...
Android Studio > Tools > Android > SDK Manager ...
Extras > Android Support Library: See the Rev. number e.g. (21.0.3).
For a quick insertion of the right revision number when in your gradle file you currently have something like 'com.android.support:appcompat-v7:21.0.+' with the 'Avoid using + in version numbers ...' warning, use the relevant IntelliJ Inspection ...
Place your cursor over 'com.android.support:appcompat-v7:21.0.+' (anywhere in the colored section of the warning).
Alt + Enter > "Replace with specific version".
There are plans to include the latest specific version number in the warning. See Issue 78737: Have the "Avoid using + in version numbers" gradle library warning suggest the version currently in use.
you can get the version list of support-v4 at here: \sdk\extras\android\m2repository\com\android\support\support-v4
You can see the versions at Google's Maven Repository. Just find the item entry com.android.support and click the plus to expand it. Scroll down and click on any artifact such as support-v13 and you'll see folders named after the version number.
This will work:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.myApp"
minSdkVersion 19
targetSdkVersion 20
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0')
}

Android Studio: Failed to find: 'com.android.support:support-v4:19.1.0'

I want to build an app in Android Studio with the support library but I get the following error when adding the dependency for the support library:
Error:Failed to find: com.android.support:support-v4:19.1.0
Here is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion '20'
defaultConfig {
applicationId "sample.myapplication"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:19.1.0'
}
I have downloaded the support library via the sdk manager.
Ok, found the problem. The Android support repository was missing. After installing and restarting Android Studio it worked.
If you are using Android Studio, then as an addition to changing the build.gradle file manually, you can also lookup the dependency via the library dependencies under the Project Structure menu item.
Double clicking that dependency will generate this line in build.gradle:
dependencies {
compile 'com.android.support:support-v13:+'
}
And also, if you are wondering what this library is about, it's described at the developer pages at developer.android.com; Support Library.
My Android Studio version is 1.1. I select tools->Android->SDK Manager, check the Android Support Library then click Install packages, solved this issue.
In my case the solution was as simple as running Build:Make Project. No amount of gradle syncing or clearing caches would do it. Of course, that required getting my project into a state where it would build successfully.
In my case I needed to add Google Maven repository.
It shows as part of the error in Android Studio and only needed to click on it to add itself.
Then Gradle built the project on its own.
Following the instruction here helped me. For whatever reason when I had to reinstall the latest version of android studio the initial download of the extras section android support library failed. I simply retried it. Followed the steps mentioned and verified it was added to the build.gradle file and did a rebuild project and good to go.
http://developer.android.com/tools/support-library/setup.html

Categories

Resources