woot woot - 6 months ago 11
Android Question

Using Gradle with an existing Android project

I have an existing Android project with the following structure:

- ProjectName
-- AndroidManifest.xml
-- local.properties
-- project.properties

-- assets
-- libs (containing all jars)
-- modules (containing all library projects my project depends on)
-- res

-- src
---- com/namespace/projectname (all my classes including main activity are here)


I haven't been using any specific build system to build my project other than the one provided by default with the Android Studio IDE (though the project was originally created with IntelliJ CE.

I would like to use Gradle with the android plugin and do some work on my build process.
I have tried several configurations in order to achieve this and have failed to complete a successful build every time.

What's the recommended approach in this scenario? should I change my project structure? or is it possible to configure gradle using the existing structure?

Any help would be greatly appreciated.

Thanks!

Answer

What's the recommended approach in this scenario? should I change my project structure? or is it possible to configure gradle using the existing structure?

It's perfectly possible to configure gradle using your existing structure.

You need to add a build.gradle to each library project. You also need to add a build.gradle and a settings.gradle files to the project root folder.

Should be like this:

  ProjectName
    build.gradle     <<<<<<
    settings.gradle  <<<<<<
    AndroidManifest.xml
    local.properties
    project.properties
    assets
    res
    src
    libs    
    modules 
       module1
          build.gradle  <<<<<<
       module2
          build.gradle  <<<<<<
       module3
          build.gradle  <<<<<<

In settings.gradle you need to include all projects folders, if your main project was in a sub-folder it should also be included, which is not in the case.

settings.gradle should look like this:

include ':modules:module1', ':modules:module2', ':modules:module2'

build.gradle of the main project should have:

dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        compile project(':modules:module1') 
        compile project(':modules:module2') 
        compile project(':modules:module3') 
}
Comments