8

I am getting started with Android development. I have followed this Getting Started guide and use Android Studio (not eclipse).

I ran Hello World on my device, so far so good. But..

Problem starts when adding this import: (as instructed by the guide)

import android.support.v4.app.NavUtils;  // cannot resolve symbol 'support'

Seems its needed by this line (commenting it and auto-resolving the import didnt work)

NavUtils.navigateUpFromSameTask(this);

The support import is red underlined, mouseover tells me Cannot resolve symbol 'support'

Build wont work either ofcourse. I have seen answers to similar questions such as; suggesting clearing cache and retarting (tried that), suggesting running SDK Manager as Administrator and updating (tried that), and some other problems/solutions that seem eclipse specific.

I am new to Android development and the IDE. How about fixing this in Android Studio v0.2.9 ?

Edit:

Contents of my build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

dependencies {

    // You must install or update the Support Repository through the SDK manager to use this dependency.
    // The Support Repository (separate from the corresponding library) can be found in the Extras category.
    // compile 'com.android.support:appcompat-v7:18.0.0'
}
Barry Staes
  • 3,890
  • 4
  • 24
  • 30
  • First I would suggest updating to the most recent version (0.3.1). Let me know how that goes. – Adam Johns Oct 24 '13 at 15:03
  • 1
    Do you have the support library installed? Can you post you build.gradle file? – redGREENblue Oct 24 '13 at 15:05
  • 1
    Please post your build.gradle and check if you have installed the support library from the SDK Manager. – fasteque Oct 24 '13 at 15:32
  • @redGREENblue i added the build.gradle file contents. – Barry Staes Oct 25 '13 at 06:15
  • Seems i cant edit the comment so.. @fasteque i added the build.gradle file contents, and i have the `Extras/Android Support Library` installed in SDK Manager. (and updated) @Adam-Johns Is the Early Access Program stable enough for production development? I cant afford losing time on IDE bugs or corrupted project files. I am on the Milestone Releases (fka Dev Channel ?) right now. – Barry Staes Oct 25 '13 at 06:26

3 Answers3

15

Modify your gradle file like below and try if it works.

......

    dependencies {
       compile 'com.android.support:support-v4:18.0.0'

        // You must install or update the Support Repository through the SDK manager to use this dependency.
        // The Support Repository (separate from the corresponding library) can be found in the Extras category.
        // compile 'com.android.support:appcompat-v7:18.0.0'
    }
redGREENblue
  • 3,076
  • 8
  • 39
  • 57
  • Thanks! This fixed the problem. Just uncommenting the v7 line resulted in problems with `R.`. Can you tell me why this works and that doesnt? What does the v7 mean, is it a version number? Why does the gradle file mention v7 instead of v4? Is this a problem that occurs more often when using import? Or is it by design even? How can i throubleshoot this myself in the future? – Barry Staes Oct 25 '13 at 06:49
  • 1
    They are both different support libraries with different feature support. V4 for API level 4+ and V7 for Api level 7+. I suggest you go through this link for more details http://developer.android.com/tools/support-library/features.html – redGREENblue Oct 25 '13 at 07:04
  • I have since had IDE warnings about `R.string.foobar` that i did properly define in `strings_foobar.xml`, build worked OK though. After i upgraded to the latest Android Studio (0.3.1 via Early Access Preview) a cache-rebuild and restart fixed that. – Barry Staes Oct 30 '13 at 08:06
  • I am having the same problem, but i cant seem to find this `build.gradle` file, i am using IntelliJ IDEA 13.1.1 – Shairyar May 04 '14 at 18:57
  • I made this align with my buildToolsVersion - seemed like a good idea. – Ghoti Aug 05 '14 at 19:47
8

Fix in Android Studio using the GUI, without the direct editing of the Gradle files (validated for Android Studio starting v1.0.1 to v2.2.3):

  1. Right-click your module in the project tree. It is one, in most cases the first, of the root nodes. By default, it is called app.

  2. In the menu choose Open Module Settings:

    enter image description here

  3. Switch to Dependencies tab.

  4. Click the add button (+) at the bottom of the dialog window.

  5. Choose Library Dependency.

  6. Choose support-v4 from the list.

  7. Click OK and rebuild.

FireAphis
  • 6,650
  • 8
  • 42
  • 63
6

Instead of editing the build.gradle I did it the maven way with right mouse on your root project -> Open Module Settings -> Dependencies Tab -> + -> Maven Dependency -> Search For "NavUtils" and select com.google.android:support-v4:r7@jar.

SDK Version 19 and Android Studio 0.4.2

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
Sebastian Roy
  • 65
  • 1
  • 5
  • 1
    Also, in the SDK Manager you need to have installed these two: "Android Support Repository" and "Android Support Library" (they are under the "Extras" category) – Daniel Nov 08 '14 at 18:54