简体   繁体   中英

Cannot get ActionBar to work in Android project and Android ActionBar samples have so many compilation errors

I am trying to add an ActionBar to my Android app and following the instructions exactly but am having an issue. It tells me to add

compile "com.android.support:appcompat-v7:18.0.+" to the build.gradle file in the dependencies section but when I open the build.gradle file it is completely empty. So I create the dependencies section in it like it shows in the tutorial but when I hover over the word dependencies, a tooltip appears saying Cannot resolve symbol dependencies.

Why is my gradle file empty, and should it be empty and why is it giving me this error? And why don't any of the Android ActionBar samples build/compile/run in Android Studio, they all have missing dependencies or pages and pages of different errors? I've followed everything in the Android docs to get setup and started properly but Android Studio just seems completely broken.

First of all I share your frustration...Android Studio is still in early preview release mode and has many issues.
The build.gradle file should never be empty.
It can be auto generated from Eclipse ( Right click on project -> Export -> Android -> Generate gradle build files ) if you want to port eclipse projects into Android Studio,
or it is created if you use the Wizard for a new Android Application inside Android Studio.

Secondly, it is a different thing what you enter into a project Settings and what your build.gradle script contains. That said, if you have added a library from the project settings menu but not in the build.gradle, your project won't build. On the other way round though, if you add the dependency in your build.gradle script but not through the Project/module settings, the Android Studio may complain, but when you ran a build task (eg. .\\gradlew assembleDebug --info ) from the command line, it should be ok.
So to sum up, if you want to ran your app by pressing the green arrow, you must add the library in the build.gradle script as a dependency like this:

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

dependencies {

    repositories {
        mavenCentral()
    }

    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:support-v4:18.0.0'
    compile 'com.android.support:appcompat-v7:18.0.+'
....
}

and also add it in the module settings ( File->Project Structure->Modules->Dependencies-> Press green plus and add the library ).
If you ran your build from the command line though, you can omit step 2 (when you ran the build from the command line, an apk is created under /build/apk folder that you have to install manually to your testing phone).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM