Issue with Android Studio Youtube imports. 'Yo

2019-02-15 15:55发布

问题:

I am trying to develop youtube functionality within an application using the Android Studio IDE. This is my code currently:

-

The issue is surrounding the imports. Now, I have the JAR file imported in the correct spot (I believe):

And I have also added it to the build.gradle file as was instructed:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.apis:google-api-services-youtube:v3-rev125-1.19.1'
}

AND I also, confirmed it is in the right project structure:

I have already looked at this stackoverflow, but there was no help there: YouTubePlayerSupportFragment cannot be resolved to a type

I have already 'cleaned' the app, closed and opened Android Studio, and rebooted the computer, but I am completely out of ideas as to why this is not recognizing the imports. Can anyone help me understand what I need to do to allow these imports to function correctly?

Thank you in advance!

-Sil

回答1:

Well, got it fixed, though it was due to trial and error. Basically, we had to force a gradle sync (even though the sync button in the top left did nothing, nor did rebooting).

Here are the steps I took:

First, I went into the build.gradle file (the WRONG one, the one that is for the project, not the module)

I added a compile statement there even though the comment below says not to do so, and clicked sync now.

Then, an error window popped up:

Which I did NOT click either of them. I went back into the build.gradle file:

Deleted the code I had written and then clicked try again.

After that, it magically fixed itself.

No clue why it fixed it or why it did not work in the first place, but if anyone ever comes across the same, try this and see if it helps.

-Sil



回答2:

I had a similar issue. When I finally resolved it, I did NOT need anything in my build.gradle file (either one). Putting the .jar file in the libs folder was all that I need to do. Looks like you found the same result, but I wanted to list out the entire sequence of steps for clarification to anyone who reads this post.

Essentially, this is all that is required:

  1. Download the .jar file from here.
  2. Make sure that there is a folder called libs that is a sibling folder to your src folder (both should be children of the app folder.
  3. Copy that .jar file into the libs folder.
  4. Make sure that your build.gradle file (the Module: app one, not the Project: one) has this line in the dependencies section:

    compile fileTree(dir: 'libs', include: ['*.jar'])

Rebuild the project, and it should work. It did for me, at least.



回答3:

compile fileTree(dir: 'libs', include: ['*.jar'])

Adding just these in Module:app just above implementation fileTree(dir: 'libs', include: ['*.jar']) solves my problem. Imports finally worked fine for me.