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
Related
After updating Android Studio and gradle to 3.1, I changed all compile statements to implementation. But when I build, android studio cannot resolve imports found in 3rd party libraries.
Scenario: Main projects imports sub-module which also import a jar file.
When I try to import a class from the jar file into the main project, android studio is not able to resolve it.
How can I import the single file without having to add the jar file as a dependency in the main project?
You should use api instead, it is the new compile or have the dependency directly in your main project. Just changing implementation to api will fix the issue but you consider using implementation wherever possible to improve build time.
You can see the difference between api and implemenation here.
The answer by #nongthonbam-tonthoi is correct but he does not explain why.
Short version
Implementation - hide this dependency from other modules(that depend on this module). if B depends on A, it cannot use any dep declared in A using implementation.
api - Make this available to other modules that depend on this module.i.e if you add say GSON as a dep in module A using api rather than implementation, all other modules that depend A can use GSON without declaring it again.
Long version
implementation is a way of declaring dependencies for only a given module. What this means is that, the dependency can only be used in that particular module. compile on the other hand "leaks" the dependencies to other modules so you can import and use the classes that dep brings in other modules. If you want this behavior, the new way of doing it is to use api.
This change is particularly targeted at multi-module projects as it can help gradle avoid re-compiling a module during a build when it does not change.
However if you're migrating from an old project, chances are you are (ab)using compile to use dependencies declared in other modules without explicitly declaring them again.
You can keep using compile but remember that it's is deprecated and will be removed soon.
See here for a deeper explanation.
Edit build.gradle (Module:app) and change SDK version to 27.1.1
Eg:
defaultConfig {
applicationId "com.projectname"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
implementation 'com.android.support:appcompat-v7:27.1.1'
and rebuild the project and restart android studio
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 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 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')
}
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