简体   繁体   中英

Can't get application to find android.support.v7, won't find appcompat_v7 project

I'm trying to create an Android application in Eclipse (on Windows Vista), and I can't get it to find the android.support.v7 library stuff that I need. I think I'm setting things up correctly, and what I'm doing works on one computer but is not working at all on another, and I need help figuring out where things are going wrong.

In Eclipse, I followed the steps to create a new "Android Application Project", and selected API 11 as the minimum SDK, and went through all the other steps. (For "Create Project in Workspace", I unchecked it and asked for the files to be created in a location on a network drive, in case that matters.) This created another project, appcompat_v7 , which I understand is how things work now. I built that project. But then when I tried to build my application, I couldn't get past The import android.support.v7 could not be resolved errors.

Here's the symptom that concerns me: I right-clicked on the application project and selected Properties -> Android. The bottom part, "Library", has "C:\\users\\me\\workspace\\appcompat_v7" in the left (Reference) column, and a question mark in the right (Project) column. [This is the workspace directory, not the network-drive directory where I'm keeping the files.] Also there's a red X next to the pathname. I tried selecting Add, which brought up a list including the appcompat_v7 project that got automatically added. I selected that, and it got added to the list with a green check mark. Then I selected the other one and Remove'd it. Now the list had just one appcompat_v7 project, with a green check mark, and the correct name in the right column, so everything looked OK. I clicked OK (also tried Apply then OK). But when I went to Properties -> Android again, now the red X was back and the project name was ? again. So it seems that something it was able to find just a few seconds ago could no longer be found (?). Rerunning Eclipse hasn't helped. The workspace\\appcompat_v7 directory and its contents seem to show up normally in a dir command, although if there's something missing I wouldn't know what to look for.

Any suggestions about what to try, or what to check for?

By experimenting, I've found more information. 通过实验,我发现了更多信息。 Apparently unchecking "Create Project in Workspace", and then selecting a network drive, is what causes the problem. If I start with a clean workspace, and uncheck "Create Project in Workspace" and select a directory on the C: drive, everything works fine. If I do the exact same thing but select a directory on a different drive, such as a network drive or USB flash drive, the problems show up.

After more research, I've concluded that this is a symptom of Android Issue 16472 , in which things fail if you have a project on one drive and a library on another drive. The new mechanism, in which the ADT plugin creates appcompat_v7 automatically when you set up a new project, exacerbates the problem. If the workspace is on the C: drive but you want the project files to be on some other drive, by unchecking "Create project in workspace", the plugin will put the project files on your other drive but create appcompat_v7 on the C: drive, which automatically causes the problems reported for #16472.

It's probably best to set up the workspace on the same drive where you put the project files (but I haven't tested this; and in any case, having them on different drives hasn't caused a problem for me, for non-Android Java projects). However, I've found a way to work around this, by copying appcompat_v7 to the other drive:

  1. Use the Android Application Project wizard to create a new application, and let it create your project and a new appcompat_v7 project on different drives.
  2. Using Windows Explorer or some other method, copy the newly created appcompat_v7 from C: to someplace on the other drive.
  3. In Eclipse, delete the appcompat_v7 project.
  4. Import -> Android -> Existing Android Code into Workspace. Browse to the location where you've made a copy of appcompat_v7 , select it, and do the import. I found that it called the new project android-support-v7-appcompat , but this is OK. I waited for "Building Workspace" to complete (if you're set up not to build projects automatically, you may have to use Build Project on the new project).
  5. Right-click on your application project, "Properties", "Android". The "Library" section will probably have a path name (the path of appcompat_v7 on the C: drive) with a red X by it. Click on Add; a window with android-support-v7-appcompat should show up; click on that. It should be added to the library list with a green check mark. Now select the path name on the C: drive, and click Remove and then OK. I'd double-check by clicking on "Properties" again, to make sure the library shows up and still has a green check mark.

After you do this, if your workspace is set up to build automatically, this should build your application successfully. (Otherwise build it manually.)

If you later add another application to the same workspace, you don't need to repeat steps 2, 3, or 4. After the wizard creates your project and another new appcompat_v7 or appcompat_v7_2 or something, use Step 5 to add the android-support-v7-appcompat you've already imported and remove the old library reference, then delete the duplicate appcompat_v7 that it just created. (See also Remove v7 appcompat folder .)

Add the library to your application project:

  • In the Project Explorer, right-click your project and select Properties .
  • In the category panel on the left side of the dialog, select Android .
  • In the Library pane, click the Add button.
  • Select the library project and click OK . For example, the appcompat project should be listed as android-support-v7-appcompat .
  • In the properties window, click OK .

If you open Java Build Path dialog for the android-support-v7-appcompat project it should look like the one on the picture below.

eclipse中的java构建路径对话框

I know this is a bit old but if anyone had the same issue I fixed like this: From gradle leave just the last V7 (mine is compile 'com.android.support:appcompat-v7:23.1.1' and I deleted compile 'com.android.support:appcompat-v7:22.2.0' ) after that go in your class and delete the import v7...after sync again and you will see that Android Studio will ask automatically you if you want import the new Dialog. I hope this simple case will help someone.

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