简体   繁体   中英

How to create the best Android app project structure with Android Studio and Gradle?

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 clone d 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?

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.

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.

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