Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 786
Benoit Duffez Avatar asked Jun 11 '13 19:06

Benoit Duffez


People also ask

What's the structure of an Android project?

Every Android project contains several folders: src: This folder contains the Java source files. gen: Generated Java library, this library is for Android internal use only. Res: Here we can store resource files such as pictures, XML files for defining layouts, and so forth.

Does Android Studio use gradle?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.

Which gradle is required for Android project development?

If you are using Gradle for Android, you need to move to version 3.3 or higher of both the Android Gradle Plugin and Android Studio.


1 Answers

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.

like image 114
Phuong Nguyen Avatar answered Sep 22 '22 22:09

Phuong Nguyen