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')
}
Related
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 > +
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.
I had my project working fine in Android Studio but then:
I upgraded to Windows 10
That broke my java install, but I have that
working as well now (removed everything then reinstalled)
I now also updaded to latest stable Android Studio
But when I try build my project now, I get 100+ errors in this:
W:\android-studio-projects\mycustomer\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\22.2.0\res\values-v21\values-v21.xml
Where each error/line is like this:
Error:(2) Error retrieving parent for item: No resource found that
matches the given name 'android:TextAppearance.Material.Inverse'.
...
Someone asked how my app/build.gradle looks like:
apply plugin: 'com.android.application'
android {
compileSdkVersion 'Google Inc.:Google APIs:17'
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "dk.company.app"
minSdkVersion 9
targetSdkVersion 17
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:+'
compile 'com.github.PhilJay:MPAndroidChart:v2.0.8'
compile 'com.android.support:appcompat-v7:22.2.1'
}
I just inserted the 4rth line in dependencies
compile 'com.android.support:appcompat-v7:22.2.1'
However, it has never been necessary in the past though and inserting it has not fixed my problem. (Why is it suddenly necessary anyhow. Could I maybe remove the dependency - that would fix the issue as well?)
...
Something else that I find peculiar, but probably happens because the project never compiles correct, is that this code:
import dk.company.app.R;
always highlights "R" in red - i.e.cannot resolve symbol 'R'
However, my guess is that it never got as far as generating the file, but stopped when encountering the other appcompat compile errors
...
I can see there are some known similar problems to this:
https://code.google.com/p/android/issues/detail?id=183122
https://medium.com/#Wingnut/installing-android-studio-and-an-ensuing-rabbit-hole-ac3d9a0d3115
However, the solutions proposed. I have removed API 22 and API 23 all places in Sdk manager and file system I could fine. I have changed build tools and appcompa version to 21.1.2
Nothing worked sofar
There's a couple things you can do in attempt to solve this:
Clean project option in Build
Rebuild (not build) project option in Build
Invalidate caches / Restart option in File
Try these, it's a glitch with Android Studio where it doesn't compile everything sometimes.
The com.android.support:appcompat-v7:22.2.1 dependency requires you to compile your project with at least API Version 22 (5.1 Lolipop).
You're currently targeting and compiling using API Version 17 (4.2 Jelly Bean). You can still leave the targetSdkVersion as 17 (if you want) but you must compile with API Version 22+.
Try changing:
compileSdkVersion 'Google Inc.:Google APIs:17'
to:
compileSdkVersion 22
Note: You may also need to download the API Version 22 platform.
Edit: I see you've updated your question that you've attempted to use the v7:21.x.x appcompat library instead. This would require you compile using at least API Level 21 compileSdkVersion 21.
I have gotten this error quite a few times. I compiled a list in this answer of every way I've ever gotten it working again.
EDIT: Just thought of another one that is more likely to be relevant to what you're describing. If you had been compiling against SDK 17, and when you reinstalled Java you upgraded to Java 8, this may cause incompatibilities. I believe only SDKs greater than 20 can compile against Java 8. Also, moving from SDK 17 to 20+ may break your styles, which will cause your xml files to be invalid, which will prevent R from building. Look in res/values/styles and see if it's failing to find your style. If so, change it to a valid style for your new SDK, clean and build.
I tried creating an Android app. using tabs in Android Studio. I ran into the issue where all my fragment imports were causing the error Cannot Resolve Symbol 'FragmentManager', and anything else associated with fragments within my program. I made sure I was importing the correct support files:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
I checked to make sure I was calling active fragments in my layout files, similar issue explained [here].
I made sure I had the latest Android SDK Build-tools, shown [here].
I [re-synced the Gradle Files].
Tools > Android > Sync Project with Gradle Files
I even imported a pre-made application from the Android Developer guides, [Creating Swipe Views with Tabs], and the error was not going away. Which made me think that there was possibly something wrong with Android Studio. I recently updated to Version 0.8.14 and I noticed the file structure changed a bit, it is now more streamlined.
Which lead me back to poke around within the gradle files. I opened up the build.gradle and verified that there were no issues from the minSdk version and then did one last check on possible errors with Gradle.
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "app.name"
minSdkVersion 15
targetSdkVersion 15
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Check out my answer for the [solution].
While this was not my question, I stumbled upon [this] thread, that suggests that you add other dependencies to the build for the support build library. Add this line within the dependencies section:
compile 'com.android.support:support-v4:18.0.0'
(For more clarification, using the previous code, you would add this beneath the compile fileTree(dir: 'libs', include: ['*.jar']).
Once you've added this, Android Studio would point out the gradle files have changed and that you should re-sync them. Once the sync is complete, it should resolve the not being able to find the fragments!
UPDATE:
For those facing this error in June 2019 upwards, make sure that if you are using the now new androidX library, you must make change android.support.v4.app.FragmentManager anywhere you call the library in your code to androidx.fragment.app.FragmentManager
For those of you stumbling across this now you may be interested to know that the support library had a split in version 24.2.0 (August 2016):
Note: Prior to Support Library revision 24.2.0, there was a single v4 support library. That library was divided into multiple modules to improve efficiency. For backwards compatibility, if you list support-v4 in your Gradle script, your APK will include all of the v4 modules. However, to reduce APK size, we recommend that you just list the specific modules your app needs.
v4 compat library
com.android.support:support-compat:24.2.0
v4 core-utils library
com.android.support:support-core-utils:24.2.0
v4 core-ui library
com.android.support:support-core-ui:24.2.0
v4 media-compat library
com.android.support:support-media-compat:24.2.0
v4 fragment library
com.android.support:support-fragment:24.2.0
Reference:
Developer.Android.com
Got the above error after downloading L preview release version in Android Studio, when my project had minSdkVersion 19.
Furthermore, when setting mindSdkVersion as below:
defaultConfig {
....
minSdkVersion 'L'
....
}
I get dozens of errors as below, regarding resources from AppCompat-v7-21 :
/home/user/workspace/project/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.0-rc1/res/values-v21/values.xml
Error:Error retrieving parent for item: No resource found that matches the given name '#android:TextAppearance.Material.SearchResult.Subtitle'.
So I have 2 questions:
Why does AS complain about minSdkVersion ? I presume because AppCompat-v7 21 supports only L release; but why ? Also, will it support only L release when it is going to be officially released in autumn too ? Because that would be a problem... Or is it just a temporary restriction in order for apps not be be pushed to Play Store, as specified in the Google I/O 2014 Keynote ?
Why does AppCompat-v7 21 complain about those errors, as I already set mindSdkVersion to L ?
compileSdkVersion 'android-L'
BOOM. Done.
LAST EDIT: As of Android 5.0 release, this is no longer an issue, just target API level 21 directly.
Edit for clarity: Indeed as David_E specified below, this solution only works for L version, if you try to deploy the app on a device below L (ex <=4.4.4) it will complain of OLD_SDK. In order for the app to work pre-L you still need to use the old v20 support lib + app compat + targetVersionSdk and compileVersionSdk
dependencies {
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.android.support:support-v4:20.+'
}
android {
compileSdkVersion 20
buildToolsVersion '20'
defaultConfig {
applicationId "com.example.application"
minSdkVersion 10
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
}
The answer stated here may not work properly, as it will make your app only work for Android L.
In order to conserve the backward compatibility with older Android OS versions, change the gradle dependecy
From:
compile 'com.android.support:appcompat-v7:+'
To:
compile 'com.android.support:appcompat-v7:20.+'
Note that the error complains about com.android.support:appcompat-v7:21.0.0-rc1, the reason for that is that using appcompat-v7:+ will tell gradle to import the latest dependency of the appcompat-v7 library, unfortunately this latest version is only working for Android L, so we have to point to a previous version.
Hopefully Google will fix this in the near future.
The way that worked for me was to install the Android Studio Beta (Not Preview Release, the Beta was launched today as part of IO).
Uninstall the preview release and install the Beta and open your project. It will ask you to use project SDK or Studio SDK, use Android Studio's SDK.
Go to your projects local.properties file and where you have sdk.dir ensure that any backslashes () are doubled up .e.g. \.
Go to Project Structure > SDK and ensure this pointing to the Android SDK on my Pc it was C:\Program Files(x86)\Android\android-studio\SDK.
Then in your projects and any libraries you have build.gradle files where you have
compile 'com.android.support:support-v4
Change it to
compile 'com.android.support:support-v4:20+
Resync the gradle files and all fixed
To force working with a working version
Just change the gradle dependencies like this:
dependencies {
compile 'com.android.support:gridlayout-v7:19.0.1'
compile 'com.android.support:appcompat-v7:19+'
...
}
was:
compile 'com.android.support:appcompat-v7:+'
N.B.
I updated Tools & Extras in the SDK manager.
after the project was broken I fixed it as shown above.
For now I do not update android L & 4.4W (enough bugs for today :-( )
Thanks David for your answer!
I had to change
compile 'com.android.support:support-v13:+'
To:
compile 'com.android.support:support-v13:20.+'
And everything work perfect now.
Just in addition: The other answers here did not fix the problem for me, but I found an alternate way to fix the android-l problem on Android Studio 0.8.0. After I did change the dependencies in the build.gradle, the problem sadly kept occuring, that the code would say OLD_SKD. I'm using a Galaxy S2.
Apart from changing all things to :19+:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19+'
compile 'com.android.support:support-v4:19+'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:mediarouter-v7:19+'
}
It still would not work, so I had to move back everywhere possible:
android {
compileSdkVersion 19
buildToolsVersion '19.1'
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName '1.0'
}
Now the app works again on the phone.
I should probably get a new mobile, but for now, I hope this may help someone.