17

I'm trying to switch to Android Studio and Gradle, but I have quite some issues with both integration into Studio and build with Gradle.

  • I've got an app that relies on several libraries.
  • I'd like to use Android Studio, and Gradle build system.
  • I'm using git
  • Most of my libraries are directly git cloned from their github location

Currently, what I have is:

Main Project
 ├── GH Lib 1
 │     ├── <some stuff from the lib>
 │     └── library
 │           ├── AndroidManifest.xml
 │           ├── res
 │           └── src
 ├── GH Lib 2
 │     └── <same structure as the lib 1>
 ├── GH Lib 3
 │     └── <same structure as the lib 1>
 │── GH Lib 4
 │     └── <same structure as the lib 1>
 └── My App folder
       └── AndroidManifest.xml
       └── res
       └── src
       └── libs

Each of the 'GH Lib X' directory is the result of a git clone from GitHub (for example: ActionBarSherlock).
'My app folder' contains directly res, src, AndroidManifest.xml, libs (with jars), etc.

1st question

I would like to understand how I can integrate all of this in Studio, with Gradle. Currently each lib is a module, and contains a build.gradle file. My App contains also a build.gradle file, however I can't reference dependencies from other folders, because they are in the parent folder, and this AFAIK can't be done with Gradle.

Would this structure be better?

My App Folder
  │── AndroidManifest.xml
  │── res
  │── src
  │── libs
  └── dependencies
        │── GH Lib 1
        │── GH Lib 2
        │── GH Lib 3
        │── GH Lib 4
        └── My App folder

My second question related to this is integration with git. Currently all libs are git submodules, is it a good idea?

Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
  • That is a good question, this is mine: http://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-buildpath – TacB0sS Jul 04 '13 at 23:42

2 Answers2

2

You should look at the multiproject example for the layout attached in the doc.

http://tools.android.com/tech-docs/new-build-system

http://docs.google.com/viewer?a=v&pid=sites&srcid=YW5kcm9pZC5jb218dG9vbHN8Z3g6NDYzNTVjMjNmM2YwMjhhNA

Essentially you want a top level settings.gradle that tie all the pieces together. Ideally they should be in one single git repo to make your life easier. However you probably can use symlink to tie them into a common build repo that contain your top level settings.gradle.

Phuong Nguyen
  • 909
  • 7
  • 20
  • I didn't notice there were examples bundled with the docs. I'll definitely have a very close look to them, especially the multiproject one. Thank you very much! – Benoit Duffez Jun 11 '13 at 20:32
  • About your remark for git: aren't git submodules made for that kind of stuff? See my question: http://stackoverflow.com/questions/16951165/how-to-push-commits-from-changes-made-to-a-submodule-into-main-git-repo – Benoit Duffez Jun 11 '13 at 20:34
  • I have not tried using git submodules yet so I'm not sure how that would work. – Phuong Nguyen Jun 11 '13 at 21:29
  • OK. I'm now getting (from within Studio): `Gradle: FAILURE: Could not determine which tasks to execute. * What went wrong: Task 'assemble' not found in root project 'MyMine'. * Try: Run gradle tasks to get a list of available tasks.`. I did run `$ ./gradlew assemble` in a shell, it worked... Ran again in studio, still not working. – Benoit Duffez Jun 11 '13 at 21:31
  • Do ou also have a top level build.gradle and build it from the top level? For example to build lib1 ./gradlew :lib1:assemble Studio does not play well with multi project / modules yet. I'm struggling with this myself. – Phuong Nguyen Jun 11 '13 at 21:33
  • I see. I have an empty top level build.gradle. I'm trying to build from top level. I hate that Studio is not very stable. I knew it wasn't 100% ready, but I couldn't help :) – Benoit Duffez Jun 11 '13 at 21:35
  • Adding more details in your answer would be much appreciated. Google Doc link seems currently broken. – melvynkim Dec 30 '13 at 00:50
  • @melvkim, the samples has since changed. You can find the updated links from http://tools.android.com/tech-docs/new-build-system – Phuong Nguyen Jan 04 '14 at 00:18
2

This structure will work just fine. I have a similar structure and everything OK in Ubuntu 13.04 and Android Studio 130.729444. You should provide settings.gradle file in root project with references (basically it's a relative path) to each module which should be built.

include ':my-app', ':gh-lib-1:library', ':gh-lib-2:library'

Root build.gradle file should contain tasks/configuration which will be common for all projects. More about multi-project setup can be found here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup

Right now your source directories location does not conform to the default Android Studio setup. You can either move your src, res directories or setup sourceSets configuration in your build.gradle. It should be done for each project. More about project structure: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure

After these steps you may try to import your project to Android Studio by selecting root build.gradle file in the 'Import project' dialog.

It's possible that at first you will be unable to build the project in IDE due to Task 'assemble' not found in root project error. This is a bug in Android Studio. Fortunately, there is a workaround for this - just include task assemble{} in build.gradle files for all 'root' projects.

Prizoff
  • 4,486
  • 4
  • 41
  • 69