Using android.support.v7.widget.CardView in my project (Eclipse)
Asked Answered
N

12

62

I'd like to use the new CardView widget that was introduced with the new Android L Developer Preview Support Library (As mentioned here), which seems to be part of a new revision of the v7 support library.

I have now updated my SDK version, downloaded the newest Support Library package, but I still cannot find the new CardView widget.

I already searched the web and the official docs, but could not get any hints on where I can get the new support library.

Any help is highly appreciated!

EDIT: I'm using ADT/Eclipse

Norenenorfleet answered 27/6, 2014 at 10:13 Comment(5)
There is a note in Android Developer website, maybe it helps: The Eclipse ADT plug-in requires Java 7 if your compilation target is the L developer preview.Bantamweight
@FD_: I'm curious why you're still using Eclipse. What's wrong with Android Studio?Garnierite
@dannyroa: I switched in the meantime ;) At the time of posting the question, I did not want to switch because at that time AIDE, the Android IDE did not support the new project structure to a usable degree.Norenenorfleet
@FD_: Haha! I hope your life is better with AS. Mine is.Garnierite
I didn't know CardView needs its own compatibility library. I assumed AppCompat would have handled this view as well.Noahnoak
N
41

I finally found a way to use CardView in ADT/Eclipse. It's actually pretty easy:

  1. Create a new project in Android Studio
  2. Add the CardView dependency as explained in the other answers to this question
  3. Open ADT and create a new library project with package name android.support.v7.cardview
  4. Delete all resources ADT auto-created
  5. Find the exploded-aar folder in Android Studio and copy the following files to these locations:
    • res/values/values.xml to the same location in your ADT project
    • classes.jar to libs/ in your ADT project
    • AndroidManifest.xml use it to replace the auto-generated manifest in ADT
  6. Add classes.jar to the build path and make sure it's exported
  7. Add a reference to the library project in the project you want to use CardView in. You can follow the steps provided under Adding libraries with resources here: https://developer.android.com/tools/support-library/setup.html

As an alternative to having to create a new Android Studio project in order to get the AAR's content, you could also simply find and unzip the AAR from the local maven repo. Just follow the steps provided by Andrew Chen below.

Please note the CardView library might not be available in source- and ADT-compatible-form because it's still only a preview and a WIP. As there might be bug fixes and improvements in following releases, it's important to keep the library up-to-date, which is easy using the Gradle dependency, but must be done manually when using the steps provided above.

Norenenorfleet answered 28/6, 2014 at 15:7 Comment(23)
Your step #5 refers to Studio. Is step #1 supposed to be "create a new project in Android Studio"?Ulphiah
Also, classes.jar will have compiled references to the AAR's value for R constants, and I fail to see how this approach will do anything to teach Eclipse to not change those R constants.Ulphiah
@Ulphiah Right, I corrected #1, #5 should be right.Norenenorfleet
@Ulphiah Hm, actually, I naively just tried copy-pasting and it worked. As far as I understand, AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name), so I assumed AAPT would generate the same identifiers as they are contained in the AAR's jar.Norenenorfleet
"AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name" -- not that I am aware of.Ulphiah
Okay, right, that does not apply when the project references library projects. However, it seems there are no compiled references in the jar. The classes inside that jar get their resource identifiers from the R.java that is generated for the library project in Eclipse.Norenenorfleet
Very interesting. Maybe it's something with the explosion process. I had looked at trying to find a way to get something from an AAR that could be used in Eclipse and I had convinced myself that the precompiled-R references would be a show-stopper. I'll try your technique next week sometime (after the dust settles from all the tools explosions this week). Many thanks!Ulphiah
I finally got an opportunity to try this on a more complex example, mediarouter-v7, which has a ton of resources. And it seems to work (the crash I was getting I also get when compiling against the AAR and seems to be something out of sync between Play Services and Chromecast in my test environment). In my earlier research, I got seriously concerned by the R.txt file, which lists a bunch of resource IDs. I'm not sure what it's there for, but it doesn't seem to be required for converting an AAR->library project. Nice work!Ulphiah
@Ulphiah yeah, was a bit confused by the R.txt initially as well. Anyway, glad it worked for you as well! I'm sorry to ask, but could you possibly have a look at another question of mine on Proxying R.java? (https://mcmap.net/q/197213/-proxying-r-java)Norenenorfleet
Sorry, I haven't a clue on that proxying bit. With regards to your answer, I cobbled together a Ruby script to automate the conversion, and I have written a blog post about your recipe and the script: commonsware.com/blog/2014/07/03/consuming-aars-eclipse.html Again, nice work!Ulphiah
@Ulphiah Okay, thanks for answering anyway! Let me express what great honour it is to be mentioned on your blog (I'm a regular reader)!Norenenorfleet
@Norenenorfleet Why can't Google provide normal samples as it used to , to support Eclipse users too?Broome
@android developer: Well, I guess they either want to force devs to Android Studio (You really should consider changing if you don't have any good reasons to stay with ADT), or they just don't want to release two versionsas long as the library is still in preview.Norenenorfleet
@Norenenorfleet too bad. Google seems to neglect Eclipse . I really like Eclipse, and I've tried Android studio before. It had very cool features, but it's not comfortable compared to Eclipse and isn't friendly for people moving from Eclipse. Plus it doesn't support any plugin of Eclipse.Broome
@android developer: I tried AS once when the first preview was released and found it so buggy that I gladly sticked with Eclipse. However, I gave it another try since I wrote the question, and it worked quite stable, so I finally did the move. AS has received an Eclipse-project importer since then that worked flawlessly for my projects.Norenenorfleet
@Norenenorfleet I see. Maybe I will try again. Does it support NDK now? the documentation seems a bit old and maybe things have changed. Also, how important is it to learn how to use Gradle on AS ?Broome
@android developer: No, NDK is not supported yet but announced to be coming soon. I highly recommend learning the new gradle build system. It just takes a few hours to understand the basics (eg different build types and product flavors are really cool).Norenenorfleet
@Norenenorfleet ok, thank you. i will try it. still sad about Eclipse being neglected.Broome
@Norenenorfleet any solution found now,on how to use Cardview using Eclipse.Venetis
@Venetis : in the latest android-sdk folder "\extras\android\support\v7\", we now have "cardview" folder. Follow the above steps given in the answer. Instead of step 5, import the project from "cardview" folder. Rename it to "android-support-v7-cardview". Set it as lib. And then import the same lib in your eclipse project.Suburban
@Norenenorfleet is this still the process to follow? I've just got a similar error and I'm kinda surprised this is so complex even now nearly a year after the preview went out.Kaufman
@Kaufman Yes, as far as I know, if you are still using Eclipse, you still have to follow this procedure. I switched to Android Studio though.Norenenorfleet
@Norenenorfleet please answer this too: #36394040Banquer
U
52

Using Gradle or Android Studio, try adding a dependency on com.android.support:cardview-v7.

There does not seem to be a regular Android library project at this time for cardview-v7, leanback-v17, palette-v7, or recyclerview-v7. I have no idea if/when Google will ship such library projects.

Ulphiah answered 27/6, 2014 at 10:27 Comment(5)
Thanks for the answer! The problem is I'm still using ADT/Eclipse. Is there any way using it there?Norenenorfleet
@FD_: Not presently -- that's what my second paragraph was referring to. I also don't see this stuff out in the Android source repo, so it's not like we can package up our own library projects. AARs cannot readily be converted into their original library projects, as they contain compiled code with numeric references to R-style constants.Ulphiah
Okay. Thanks then, will test using Android Studio.Norenenorfleet
I finally found a way to do it! It was no problem to copy the resources from the AAR into a library project in Eclipse and as the included sources were kept in their own jar, I could simply re-use that as well. I've added my own answer.Norenenorfleet
Addesd in android studio and worked perfect! Thanks!Pyjamas
N
41

I finally found a way to use CardView in ADT/Eclipse. It's actually pretty easy:

  1. Create a new project in Android Studio
  2. Add the CardView dependency as explained in the other answers to this question
  3. Open ADT and create a new library project with package name android.support.v7.cardview
  4. Delete all resources ADT auto-created
  5. Find the exploded-aar folder in Android Studio and copy the following files to these locations:
    • res/values/values.xml to the same location in your ADT project
    • classes.jar to libs/ in your ADT project
    • AndroidManifest.xml use it to replace the auto-generated manifest in ADT
  6. Add classes.jar to the build path and make sure it's exported
  7. Add a reference to the library project in the project you want to use CardView in. You can follow the steps provided under Adding libraries with resources here: https://developer.android.com/tools/support-library/setup.html

As an alternative to having to create a new Android Studio project in order to get the AAR's content, you could also simply find and unzip the AAR from the local maven repo. Just follow the steps provided by Andrew Chen below.

Please note the CardView library might not be available in source- and ADT-compatible-form because it's still only a preview and a WIP. As there might be bug fixes and improvements in following releases, it's important to keep the library up-to-date, which is easy using the Gradle dependency, but must be done manually when using the steps provided above.

Norenenorfleet answered 28/6, 2014 at 15:7 Comment(23)
Your step #5 refers to Studio. Is step #1 supposed to be "create a new project in Android Studio"?Ulphiah
Also, classes.jar will have compiled references to the AAR's value for R constants, and I fail to see how this approach will do anything to teach Eclipse to not change those R constants.Ulphiah
@Ulphiah Right, I corrected #1, #5 should be right.Norenenorfleet
@Ulphiah Hm, actually, I naively just tried copy-pasting and it worked. As far as I understand, AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name), so I assumed AAPT would generate the same identifiers as they are contained in the AAR's jar.Norenenorfleet
"AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name" -- not that I am aware of.Ulphiah
Okay, right, that does not apply when the project references library projects. However, it seems there are no compiled references in the jar. The classes inside that jar get their resource identifiers from the R.java that is generated for the library project in Eclipse.Norenenorfleet
Very interesting. Maybe it's something with the explosion process. I had looked at trying to find a way to get something from an AAR that could be used in Eclipse and I had convinced myself that the precompiled-R references would be a show-stopper. I'll try your technique next week sometime (after the dust settles from all the tools explosions this week). Many thanks!Ulphiah
I finally got an opportunity to try this on a more complex example, mediarouter-v7, which has a ton of resources. And it seems to work (the crash I was getting I also get when compiling against the AAR and seems to be something out of sync between Play Services and Chromecast in my test environment). In my earlier research, I got seriously concerned by the R.txt file, which lists a bunch of resource IDs. I'm not sure what it's there for, but it doesn't seem to be required for converting an AAR->library project. Nice work!Ulphiah
@Ulphiah yeah, was a bit confused by the R.txt initially as well. Anyway, glad it worked for you as well! I'm sorry to ask, but could you possibly have a look at another question of mine on Proxying R.java? (https://mcmap.net/q/197213/-proxying-r-java)Norenenorfleet
Sorry, I haven't a clue on that proxying bit. With regards to your answer, I cobbled together a Ruby script to automate the conversion, and I have written a blog post about your recipe and the script: commonsware.com/blog/2014/07/03/consuming-aars-eclipse.html Again, nice work!Ulphiah
@Ulphiah Okay, thanks for answering anyway! Let me express what great honour it is to be mentioned on your blog (I'm a regular reader)!Norenenorfleet
@Norenenorfleet Why can't Google provide normal samples as it used to , to support Eclipse users too?Broome
@android developer: Well, I guess they either want to force devs to Android Studio (You really should consider changing if you don't have any good reasons to stay with ADT), or they just don't want to release two versionsas long as the library is still in preview.Norenenorfleet
@Norenenorfleet too bad. Google seems to neglect Eclipse . I really like Eclipse, and I've tried Android studio before. It had very cool features, but it's not comfortable compared to Eclipse and isn't friendly for people moving from Eclipse. Plus it doesn't support any plugin of Eclipse.Broome
@android developer: I tried AS once when the first preview was released and found it so buggy that I gladly sticked with Eclipse. However, I gave it another try since I wrote the question, and it worked quite stable, so I finally did the move. AS has received an Eclipse-project importer since then that worked flawlessly for my projects.Norenenorfleet
@Norenenorfleet I see. Maybe I will try again. Does it support NDK now? the documentation seems a bit old and maybe things have changed. Also, how important is it to learn how to use Gradle on AS ?Broome
@android developer: No, NDK is not supported yet but announced to be coming soon. I highly recommend learning the new gradle build system. It just takes a few hours to understand the basics (eg different build types and product flavors are really cool).Norenenorfleet
@Norenenorfleet ok, thank you. i will try it. still sad about Eclipse being neglected.Broome
@Norenenorfleet any solution found now,on how to use Cardview using Eclipse.Venetis
@Venetis : in the latest android-sdk folder "\extras\android\support\v7\", we now have "cardview" folder. Follow the above steps given in the answer. Instead of step 5, import the project from "cardview" folder. Rename it to "android-support-v7-cardview". Set it as lib. And then import the same lib in your eclipse project.Suburban
@Norenenorfleet is this still the process to follow? I've just got a similar error and I'm kinda surprised this is so complex even now nearly a year after the preview went out.Kaufman
@Kaufman Yes, as far as I know, if you are still using Eclipse, you still have to follow this procedure. I switched to Android Studio though.Norenenorfleet
@Norenenorfleet please answer this too: #36394040Banquer
B
33

You need to add this in your build.gradle:

dependencies {
    ...
    compile 'com.android.support:cardview-v7:+'
}

And then Sync Project with Gradle Files. Finally, you can use CardView as it's described here.

Bantamweight answered 27/6, 2014 at 10:29 Comment(0)
B
27

I have done following and it resolve an issue with recyclerview same you may use for other widget as well if it's not working in eclipse project.

• Go to sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1 directory

• Copy recyclerview-v7-21.0.0-rc1.aar file and rename it as .zip

• Unzip the file, you will get classes.jar (rename the jar file more meaningful name)

• Use the following jar in your project build path or lib directory.

and it resolve your error.

happy coding :)

Branchia answered 2/7, 2014 at 21:8 Comment(1)
That will not work if the AAR in question has resources. Now, it so happens that the RecyclerView AAR does not presently have resources, and so this works, but that's not assured for future versions of the AAR.Ulphiah
H
19

https://github.com/yongjhih/CardView

A CardView v7 eclipse project. (from sdk/extras/android/m2repository/com/android/support/cardview-v7)

The project was built by steps:

cp {sdk}/extras/android/m2repository/com/android/support/cardview-v7/21.0.0-rc1/cardview-v7-21.0.0-rc1.aar cardview-v7-21.0.0-rc1.zip
unzip cardview-v7-21.0.0-rc1.zip
mkdir libs/
mv classes.jar libs/cardview-v7-21.0.0-rc1.jar
Hog answered 2/7, 2014 at 3:40 Comment(0)
M
10

I was able to work it out only after adding those two TOGETHER:

dependencies {
...
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
...
}

in my build.gradle (Module:app) file

and then press the sync now button

Materialism answered 20/11, 2016 at 10:54 Comment(0)
L
3

Although a bit hidden it's in the official docs here where can the library be found among the sdk's code, and how to get it with resources (the Eclipse way)

Lussier answered 5/11, 2014 at 0:16 Comment(0)
H
2

From: https://developer.android.com/tools/support-library/setup.html#libs-with-res

Adding libraries with resources To add a Support Library with resources (such as v7 appcompat for action bar) to your application project:

Using Eclipse

Create a library project based on the support library code:

  • Make sure you have downloaded the Android Support Library using the SDK Manager.

  • Create a library project and ensure the required JAR files are included in the project's build path:

  • Select File > Import.

  • Select Existing Android Code Into Workspace and click Next.

  • Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.

  • Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.

  • In the new library project, expand the libs/ folder, right-click each .jar file and select Build

  • Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.

  • Right-click the library project folder and select Build Path > Configure Build Path.

  • In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.

  • Uncheck Android Dependencies.

  • Click OK to complete the changes.

  • You now have a library project for your selected Support Library that you can use with one or more application projects.

  • 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.

Headset answered 12/11, 2014 at 3:1 Comment(0)
G
2

Maybe it's a little bit late to add answer here. But I think this answer will help the later ones and especially those who don't want to use Android Studio.

Although the documents says that RecyclerView and CardView are part of v7 appcompat library. But as I tried and found, RecyclerView and CardView are actually depend on v7 appcompat library. So if you want to use RecyclerView or CardView, you need to add both v7 appcompat library and RecyclerView/CardView.

Referencing the link here, if you want to use CardView in your Eclipse android project, you need to import both v7 appcompat library and CardView into Eclipse workspace and make them as library projects. Then make CardView project depends on v7 appcompat library project and make your project depends on CardView project.

Groggy answered 24/6, 2015 at 9:0 Comment(0)
O
2

Simply add the following line in your build.gradle project

dependencies {
    ...
    compile 'com.android.support:cardview-v7:24.0.0'
}

And sync the project with gradle.

Ostracod answered 6/4, 2016 at 5:29 Comment(0)
D
0

I did what FD_ said and it crashed with errors as it was looking for "Landroid/support/v7/cardview/R$styleable;", which was not compiled with it

If you really want to use CardView before in eclipse before it gets its own library, you can extract the classes from the classes.jar, copy and paste them into your project, with the values.xml from above from Android Studio and change all the references to android.support.v7.R to yourpackagename.R in the copied classes. This worked and ran for me

Daman answered 28/6, 2014 at 19:57 Comment(2)
What package name did your Eclipse library project have? Did the auto-generated R.java in your Eclipse library project contain the <declare-styleable> tag from the AAR's res/values/values.xml file. I had the same crash initially but resolved it by correcting the library project's package name. How did you extract the classes from the jar? Did you decompile it?Norenenorfleet
It did have the same name, so it might have just been a quirk, and yes it did have the correct resources. I decompiled the JAR using a standard decompiler (JD)Daman
B
0

In the most latest android, we implement card view like this:

build.gradle file:

dependencies {
    ...
    compile 'com.android.support:cardview-v7:24.0.0'
}

and in the xml file its referenced as:

<androidx.cardview.widget.CardView
…>
…
</androidx.cardview.widget.CardView>

this means its class has changed to: androidx.cardview.widget.CardView

Burble answered 2/6, 2020 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.