Android Studio: Can't resolve imports (android.support.*) - java

AndroidStudio is driving me crazy again...
I got a one year old AS project (that worked just fine) from my Win 7 PC and want to add stuff to it on my Win 10 workstation but even though AS says that the build completed successfully, it won't resolve a couple of imports, which leads to loads of red squiggly lines:
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
Android Studio: 2.1.3
Build tools: 25.0.2 (was installed before installing AS), 23.0.3 (installed
after first errors), 27.0.3 (installed after the other 2)
APIs: 19, 21, 23, 24
Android Support Repository 47
Yes, there's an entry for Maven in the "build.gradle" file that is directly in the project folder.
The "app" gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.mycompany.myapp"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.+'
compile 'com.android.support:support-v4:23.0.3'
}
The first "compile" has a squiggly line too and the message says:
All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes).
Found versions 28.0.0-alpha1, 23.4.0.
If I use "compile 'com.android.support:support-v4:28.0.0-alpha1'" the build fails at ":app:processDebugResources" and it complains:
AAPT2 error: check logs for details
The logs begin with:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugResources'.
What exactly does AS want (apart from getting on my nerves)?

Have you tried "Invalidate Caches/ restart" in Android Studio??
I have also faced issues like this, which was solved by this.

Clear your cache and do a ./gradlew --refresh-dependencies at the root of your project.
Saved my ass pretty often.

compile 'com.android.support:support-v4:23.0.3'
remove above line and sync project : File > Sync project with gradle files

Related

I can't run an android app because of a seemingly missing unknown parent resource? [duplicate]

I've always programmed Android with Eclipse and decided to start migrating to Android Studio. I decided to use the same SDK I already had for Eclipse, then:
Started a new project
Set minimum SDK 4.0 (API Level 14)
Choose Blank Activity option
Used Default names for Activity Name and Layout Name
Hit Finish
After a few seconds Gradle finishes the build, and it throws me two errors with the following messages in file Teste4\app\build\intermediates/exploded-aar\com.android.support\appcompat-v7\23.0.0\res\values-v23\values-v23.xml:
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.
Under File -> Project Structure -> Modules: app (left column) -> Properties tab, I have the following versions set up:
"Compile Sdk Version": Android 5.1 (API Level 22)
"Build Tools Version": 23.0.2
What should I do in order to fix this?
I already tried what was suggested in Stack Overflow question appcompat-v7:21.0.0': No resource found that matches the given name: attr 'android:actionModeShareDrawable', but it didn't work.
Your compile SDK version must match the support library's major version.
Since you are using version 23 of the support library, you need to compile against version 23 of the Android SDK.
Alternatively you can continue compiling against version 22 of the Android SDK by switching to the latest support library v22.
This happens because after updates Android Studio uses API version 23 by default.
The following worked for me:
Press Ctrl + Shift + Alt + S to get to the project structure page. Go to the properties tab and change 23.0.0 to 22.0.1 (or equivalent to what you were using earlier) in the build tool area and rebuild your project.
If that doesn't work, go to gradle:app and then
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
Edit v7:23.0.0 to v7:22.2.1 as shown above and sync gradle. This will definitely work.
When you update your Android Studio, it uses API version 23 by default,
which is the main reason for its occurrence. So,
At first, check your AppCompat version in build.gradle(Module:app)
That is,
If after changing to 23 there occurs an error then just download
Compile Sdk Version to API 23, and Build Tools Version to 23.0.0
from SDK Manager. If already downloaded then:
1. Go to SDK Manager and
2. Under Project Structure, change *Compile SDK Version* to API 23, and *Build Tools Version* to 23.0.0
Click the SDK Manager Button and open the dialog.
Click SDK Platform and check if Android 6.0 is downloaded or not.
if not, then download that first. After completing the download, click Apply.
Now you need to apply changes to your project from setting.
Then press Ctrl + Alt + Shift + S to open setting
Click the app from the list.
Click properties
Change your Compile SDK Version to API 23
Change your Build Tools Version to 23.0.0
Don't forget to rebuild your project.
Then your error will be gone.
If you've tried to change target to a previous GooglePlayServices or AppCompatv7 version and it doesn't work, check if you have any project-libraries dependency, this project will be targeting the latest version of any of these libraries. It happened to me with the Google Maps Utils Library project:
replace:
compile 'com.google.android.gms:play-services:+'
to
compile 'com.google.android.gms:play-services:8.3.0'
Then you can continue full targeting API 22
If it still doesn't compile, sometimes is useful to set compileSdkVersion API to 23 and targetSdkVersion to 22.
I agree with the previous answer. Your compile SDK version must match the support library. Here is what I did.
You can go to SDK Manager and under SDK Platform, install the Android 5.X with API level 23.
Under Project Structure, change compile SDK Version to API 23, and Build Tools Version to 23.0.0
Then it should build without problem.
You need to set compileSdkVersion to 23.
Since API 23 Android removed the deprecated Apache Http packages, so if you use them for server requests, you'll need to add useLibrary 'org.apache.http.legacy' to build.gradle as stated in this link:
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
...
//only if you use Apache packages
useLibrary 'org.apache.http.legacy'
}
This issue is raised because now the compile SDK version must match the Android Support library's major version.
In my case i have the Android Support Library version 23, so i had to compile against the Android SDK version 23, and I had to change this in my build.gradle file:
Well some of you will need to install the SDK, Android 6.0 (API 23)
and donĀ“t forget to Sync project with gradle files
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
applicationId ""
minSdkVersion 14
targetSdkVersion 22
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.google.code.gson:gson:2.3.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.1'
}
I found the solution, Problem started when I updated sdk, after that I used different buildToolsVersion ('22.0.1') and I was using different support library like compile 'com.android.support:appcompat-v7:23.0.0', showing you in image below
This was raising problem of "android.widget.Material..."
Then I used same version for support library like compile 'com.android.support:appcompat-v7:22.0.1' and its DONE. (Showing you in below screenshot)
You should compile your project with latest version so update & install from your SDK. Sync your project with sync project with Gradle file Button.
You can also continue with the existing version but check it installed properly below image indicate to API 22 that is properly installed.
And sync your project if needed.
it may help.
As pointed out by Tanis.7x, the support library version (23) does not match the targetSdkVersion (22)
You can fix this by doing the following:
In the build.grade file of your app module, change the following line of code
compile 'com.android.support:appcompat-v7:23.0.0'
To
compile 'com.android.support:appcompat-v7:22.+'
This will use the latest version of the appcompat version that is compatible with SdkVersion 22.
Your compile SDK version must match the support library major version. This is the solution to your problem. You can check it easily in your Gradle Scripts in build.gradle file.
Fx: if your compileSdkVersion is 23 your compile library must start at 23.
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 340
versionName "3.4.0"
}
dependencies {
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:recyclerview-v7:23.0.1'
}
And always check that your Android Studoi has the supported API Level. You can check it in your Android SDK, like this:
In my case it was an higher version of Google-play-services. I set them to 7.0.0 (not 8.x) and all was ok.
I wanted to downgrade from API 23 to 22 and got this error. I had to change all build.gradle files in a project in order to compile.
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.yourapp.app"
minSdkVersion 14
targetSdkVersion 22
}
...
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.google.android.gms:play-services-gcm:10.0.1'
}
This is what helped me: Adding specific android platform
What should be done is the following... In my case it was cordova but the same is relevant for ionic, phonegap and other frameworks like these:
list all platforms installed for your project: cordova platform list. You'll see something like this:
remove the android platform: cordova platform remove android.
then add the specific android platform: cordova platform add android#5.0.0.
Good luck! :)
I got the same problems. I solved my problem by updating gradle.build for each sub-module to latest compiler version.
Another solution :
navigate to
\sdk\extras\android\m2repository\com\android\support\appcompat-v7\23.x.x
open .aar file with 7-zip or winrar , in res folder remove values-23 folder and save changes .
on module: app (Gradle)
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.namepack.nameappxxxxx"
minSdkVersion 16
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:21.0.3'
}
project: nameAppXXXX (Gradle)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
and edit your activity of AppCompatActivity to for example Activity:
package com.namepack.nameappxxxxx;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class NameClass extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
Everything is great but none of you explained where to download the SDK build tools
Make sure that all these are in upto date.
Upgrade Android Studio.
I had this issue with Android Studio 1.3.1 and none of the other answers worked for me, but after updating to 1.5.1 there were no problems.
If you are using phonegap(cross-platform) and got same issue above,
just remove the android platform using below command.
phonegap platform remove android
And add it again.
phonegap platform add android
Then problem solved for me.
If you are getting errors even after downloading the newest SDK and Android Studio, here is what I did:
Download the most recent SDK
Open file-Project structure (Ctrl + Alt + Shift + S)
In modules, select app
In the properties tab: Change compile SDK version to API 23 Android 6.0 marshmallow (latest)
I hope it helps someone so that he won't suffer like I did for these couple of days.
As others have already mentioned your compile sdk version must match your support library's major version. This is however, also relevant for subprojects should you have any.
In case you do, you can set your subprojects compile sdk versions with the following script:
subprojects { subproject ->
afterEvaluate{
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
Add this script in your root build.gradle file.

How to import project to Android Studio

I have this repository ( https://github.com/udacity/Just-Java/tree/Lesson_2 ) that I'm trying to import. I extract the folder from the zip file that I downloaded from there. Then I try to use "import project" function on Android Studio, but I am not sure which exact folder/directory to select for import. When I use the main directory I unzipped, it takes about half an hour to load it and then gives me an error saying "failed to find target with hash string 'android-24' in: [i]my appdata folder[/i]
I have very slow connection at the moment, so it takes ages to download this Android SDK Platrom 24. I am running the latest version of the Android Studio.
So the 'build.gradle' file (Assuming you are using gradle) would specify which is the target Android SDK.
Now I suppose you have some Android SDK and the corresponding build tools already installed. These would then not require a download. But then gradle requires an internet connection to actually search for libraries that it should download to satisfy the dependencies of the project.
Now once you have figured out which Android SDK and build tools you have, you can make the change in the build.gradle file. (This would be in the /app/). You would have to make an edit here.
compileSdkVersion 21 *replace with the version you have*
buildToolsVersion "21.1.2" *this should also start with the compileSdkVersion number*
Hope this helps!
This is your app module build.gradle file, change in this file according to what you have in your SDK.
apply plugin: 'com.android.application'
android {
compileSdkVersion 24 // Change what SDK Version you have
buildToolsVersion "24.0.2" // Change buildTool Version
defaultConfig {
applicationId "com.example.android.justjava"
minSdkVersion 15
targetSdkVersion 24 // Change targetSDK, can be same as compileSdk
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:24.2.0' //Change appcompact version
}

SafePassmaster ---> Error:(1, 0) Plugin with id 'com.android.application' not found

I have imported a android source code from Github and while syncing the gradle process I have got an error
Error:(1, 0) Plugin with id 'com.android.application' not found.
Open File
This is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "activities.safepassbeta"
minSdkVersion 16
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:20.0.0'
}
Try changing the classpath in the top level build.gradle file to 'com.android.tools.build:gradle:2.0.0-beta7' and the distributionUrl value in gradle-wrapper.properties file to https://services.gradle.org/distributions/gradle-2.10-all.zip.
Make sure that in the settings for gradle in the 'Build, execution and deployment' section, you have set the preference to 'Use default gradle wrapper'.
As the other answer described, try changing the classpath in the top level build.gradle file. The file you show is the application build.gradle, not the top level build.gradle file. For Android Studio 3, you can try using 'com.android.tools.build:gradle:3.0.1'. The problem is that the gradle dependency is not downloading properly, usually because of improper settings for the url to use to download gradle. This is a common problem. If the problem persists look into solutions for how to set up gradle, such as Manually install Gradle and use it in Android Studio
A quick-and-easy fix is to look at your previous projects which work on your IDE, look at what version of gradle they are using. If you use that version of gradle in this project it will most likely work.
However, the 'proper' solution is to set up the distributionUrl properly. Worst come to worst, if that does not work, you could also try manually downloading Gradle to use it in your project.

Execution failed for task :app:compileDebugJavaWithJavac

I picked up someone else's project and when I asked AS to run the project on my AVD, it pops me an error:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId 'ca.gggolf.aminutegolf'
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
I don't know what to do.. Everything else works fine and the app was working a couple of years ago when it was first made.
Enable multidex for your project. Add below line to gradle defaultConfig:
multiDexEnabled true
Also add multidex dependency to gradle's dependencies :
compile 'com.android.support:multidex:1.0.1'
Check your location address of exist project. if you use special character like :
( / | \ + = * ... )
In folder and sub-folders name, you should change it and check it out.
I changed my folder name from "UI/UX" to "UiUx" and its done :)
I'm back at this question simply because it got many views.
First, thanks all for the help. I managed to run the app by creating a new project on Android Studio and importing classes one at a time while making sure the code was still fine. Maybe creating a new project added some parameters that were not present in the older version of the project.
If someone has a better explanation, feel free to answer!
Check your lib versions. In my case it was old Realm plugin that was an issue.
Sometimes this error happens to me. I don't have any java compiler errors.
And seems this issue related to the build cache of a project.
So it can be solved like this
Build/Clean Project
Build/Make Project or Build APK
Build with below command, this command will point the exact point in source code where the compilation error is getting generated.
gradlew assembleDebug --stacktrace

AndroidStudio cannot resolve R and layout files

When I upgraded AndroidStudio from 0.8 (or 0.9) to 1.0.2 AndroidStudio was not able to resolve my R file and suggests importing it (import my.package.R). I'm using SDK Tools 24.0.2 and SDK Platform-tools 21.
I've tried cleaning, resynching and rebuilding the project, as well as checking the layout files for errors, but I didn't find anything.
Another weird thing is that when typing "R.layout" AndroidStudio suggests either "android.R" or "my.package.app.R". When chosing the former I'm not able to use my layout files, when chosing the later it automatically imports "my.package.app.R"
When importing R a co-worker of mine is even able to run the app...
This is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 16
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "my.package.app"
minSdkVersion 14
targetSdkVersion 20
versionCode 6
versionName '0.1.6'
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
I think it could be of use to some of you here.
Thanks!
Edit: I was told that it is perfectly normal to import one's own R class. Sorry for the inconvenience...
If is showing error (red signed) and is not compiling I'm pretty sure there is something wrong in at least one XML file (like a tag unclosed).
I had same kind of error in Android Studio , the reason it gives Red Mark is due to some small errors , ending or opening tags are not been properly closed or opened!

Categories

Resources