16

I want to use HoloCircullarProgressBar as a library project in my android studio project. I tried to do it by copying into pre-created "library" folder in my project and then to add it to project in "Project Structure". But it's not working somehow.

Could anyone give a very specific step-by-step tutorial on how to do that in android studio 0.3.6?

enter image description here

user1685095
  • 5,787
  • 9
  • 51
  • 100
  • Go to `Module Settings` and choose `Import Module`, select your library's root folder and after the whole process is complete, add it as dependency to your main project using `Module Settings` and `build.gradle` file. – hardartcore Dec 01 '13 at 08:43
  • Well, I press "Open Module Settings" and get to this window. There is no "Import Module" option. If I would press the "+" button and the add a root folder of the library I would get /home/michael/AndroidStudioProjects/HoloPomodoro/HoloCircullarProgressBar/build/exploded-bundles/ComAndroidSupportAppcompatV71900.aar/classes.jar Something about classpath errors – user1685095 Dec 01 '13 at 11:59
  • Are you trying to add the library source code as a module, or do you have an .aar archive file for it? – Scott Barta Dec 01 '13 at 16:43
  • I'm trying to add it as source code, like I would do in eclipse. It's an eclipse project if that's important.I don't even heard about .aar files so far? I've just recently decided to give android studio a try, so I don't know muck about it. – user1685095 Dec 01 '13 at 16:46

1 Answers1

33

If you're importing a library as source code into a Gradle-based project, then at the moment there's no super-easy way to do it (sorry, it's on the to-do list, see https://code.google.com/p/android/issues/detail?id=62122) so you'll have to author your own build file for the library. Actually, it might be easier to use the New Module wizard to set up the build file and directory structure, then you can trim it down and copy the files over. This set of steps should get you up and running. It seems like a lot of steps but it should hopefully go pretty quick.

  1. From the File menu, choose New Module...
  2. From the wizard that comes up, choose Android Library
  3. From the next page of the wizard, give it the module name HoloCircularProgressBar, and uncheck the options for Create custom launcher icon and Create activity.
  4. Click Finish on the wizard.

It should add the new module to your project, so you'll end up with something like this:

Project view after adding new empty library

  1. Delete everything inside the src/main folder.
  2. Now copy AndroidManfiest.xml, ic_launcher-web.png, res, and src from the HoloCircularProgressBar source into the src/main folder.
  3. Rename the src folder that you just copied into src/main to java.
  4. The New Module wizard left some things in the build.gradle file in your HoloCircularProgresBar module/directory (make sure you're editing that one, not the one for your main app module) we don't need. Remove the dependencies block and the release block.

At this point you should hopefully be able to build successfully. Now if you want to actually use the module:

  1. Go to File > Project Structure... > Modules > Your main module > Dependencies.
  2. Click on the + button to add a dependency, choose Module dependency, and select HoloCircularProgressBar from the list.

Now import statements and usages of the library should work, and you should be good to go.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • 1
    There's an alternate way to do this where you could create the `build.gradle` file and directory structure for the library by hand, and edit the `settings.gradle` file by hand to include the new library. This has the advantage that you have to edit fewer files by hand to get where you need to go, but the disadvantage that you have to remove things the wizard added that you don't need. – Scott Barta Dec 02 '13 at 01:50
  • Hey Scott -- Thanks for the detailed notes. It all worked fine but my imports still don't work :-( (the project builds fine however) -- I guess I'll sleep on it and see how it looks in the morning (I'm new to android studio/gradle in general having spent the last years in IOS land) – loghound Dec 02 '13 at 08:18
  • If it builds from the command line but you still have problems with it not recognizing imports in the IDE, then often what you need to do is click the Sync Project with Gradle Files button in the toolbar. – Scott Barta Dec 02 '13 at 18:50
  • Hi Scott -- you are being a superstar but I'm still striking out. Here is the crazy thing. I have a version of the project I exported from eclipse and imported into AS and that builds. I wanted to do a new 'clean' project within (I'm still sort of learning android/AS so I figured it's a nice exercie) – loghound Dec 03 '13 at 07:15
  • (sorry -- first editing session ran out of time) My subproject builds OK but I can't import it into main app. I've gone through settings.gradle and build.gradle and compared the working eclipse imported project and it *looks* like it should work but I still can't import the library into my app :-( Is there anyplace else to look or set to tell my main project "app" to be able to import my library project "Rajawali" ? thx – loghound Dec 03 '13 at 07:25
  • Ok -- I sorted out a solution of sorts... Basically re-did the steps and now magically it seems to be working better. THe package name seemed to matter (or at least using a new package name seems to have fixed it) – loghound Dec 03 '13 at 07:48
  • The project is building but It's giving me this error when rendering layout with custom view. The following classes could not be instantiated: - de.passsy.holocircularprogressbar.HoloCircularProgressBar (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom views to skip code when shown in IDE And this one, when building the whole project. Execution failed for task ':HoloPomodoro:processDefaultFlavorDebugManifest'. > Manifest merging failed. See console for more info. – user1685095 Dec 05 '13 at 18:11
  • Those are two different errors; the custom view rendering problem you should write up as a separate question with lots of info and screenshots; that actually sounds like a good question that could help a lot of people. As for the manifest merging error, try looking at the Gradle Console view (View > Tool Windows > Gradle Console) and see if the output there helps you; if not, file a separate question for that as well. I've been working on manifest merging error handling in AS 0.3.7 and the upcoming Gradle plugin 0.7.0, so it will get better in the future. – Scott Barta Dec 05 '13 at 18:21