3

I'm new to Android programming and new to Studio (0.4.4). For the first time, I found a class I wanted to incorporate into my project. Per other SO advice here, I created a new module by the same name, then replaced the Java template with the new class. So far so good.

Unfortunately, while it could find and import the java.* classes, it cannot find and import any of the android.* classes, specifically android.os.SystemClock and android.util.Log. (e.g., "Cannot resolve symbol 'X'") What steps do I need to take so that they can be found and imported? Thanks for any help.

enter image description here

Updated build.gradle:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
}
Community
  • 1
  • 1
ScottyB
  • 2,167
  • 1
  • 30
  • 46
  • Go to File > Project Structure > Android SDK and check if it pointing to right SDK. Also make sync your project with gradle using a tiny gradle button in tool bar. If it doesn't solve the problem please include Android Studio screenshot with project Structure in Question. – Piyush Agarwal Feb 14 '14 at 06:16
  • I added a screenshot of the project structure. I can see that the list of included Libraries with the module is much smaller than that of the app. Specifically, it doesn't include the android library which is what I need. How do I add that? Thanks again. – ScottyB Feb 14 '14 at 14:20
  • Please include the build.gradle file for the module you're adding. – Scott Barta Feb 14 '14 at 16:11
  • Included now. Thanks! – ScottyB Feb 14 '14 at 16:24
  • That's the top-level build.gradle file, not the build file for the module you're adding. Also, please attach in text form and not as screenshots wherever possible. Thanks. – Scott Barta Feb 18 '14 at 22:03
  • I replaced the image of the wrong build.gradle file with a text version of the right build.gradle. – ScottyB Feb 19 '14 at 01:40

1 Answers1

3

If you want to use Android classes in a module, it needs to be an Android module. The build.gradle file needs to specify:

apply plugin: 'com.android.library'

And you may also need:

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

though newer projects are built from a template that includes this in the top-level build.gradle file and it's not necessary to specify it again below; try without and see if it complains or not.

In any event, with an android-library module type, you'll need an AndroidManfiest.xml file and the associated trappings of an Android project -- resource directories and the like. This is probably what you want -- that way if your library has resources or additions to the manifest file, they will get properly merged at runtime.

lcsvcn
  • 1,184
  • 3
  • 13
  • 28
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • I am really surprised how complex it is (at least to me) to add a module to an Android app! So I replaced "apply plugin: 'java'" with "apply plugin: 'android-library'" and the Event Log contained "Main Manifest missing from C:\xampp\htdocs\hvb\HelloWorld\SntpClient\src\main\AndroidManifest.xml". Not sure what needs to go in it, but I copied the existing app AndroidManifest.xml and the Event Log said the project refresh failed: "Cause: android.compileSdkVersion is missing!" Since I'm just trying to add a new Class, is there a simpler way? – ScottyB Feb 19 '14 at 05:03
  • You can just choose "New Module" from the File menu and follow the wizard to add an Android library. – Scott Barta Feb 19 '14 at 05:39
  • Ah. The first time, I used the wizard to add a Java library, not realizing my module needed Android classes. (You can't have both yet apparently.) So I started over, and it worked after three attempts to build with no changes in between. I suppose that's expected with an "early access preview." However, I finally learned that I can easily add a new class via a .java file by the same name in my project's main source folder (alongside Activity .java files). 50 points for you! – ScottyB Feb 20 '14 at 03:29