Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import android packages into new Android module?

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:+'
}
like image 211
ScottyB Avatar asked Sep 06 '25 03:09

ScottyB


1 Answers

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.

like image 57
Scott Barta Avatar answered Sep 12 '25 20:09

Scott Barta