Using Gradle with an existing Android project

∥☆過路亽.° 提交于 2019-11-30 17:25:36
rafaello

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') 
}
rivare

Just install gradle on your computer und make sure all paths are ok.

After that create a "build.gradle" file in your Project folder.

Here an basic example your can start from:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.+'
    }
}

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':yourModuleName') 
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('yourTestRoot')
    }
}

To adress your modules like I did in the dependenciey block, you have to create a settings.gradle file and reference your modules as project. Since i assume they have also a custom layout, you have to make build.gradle files on the module folder too.

For the settings.gradle setup you should visit the gradle documentation, nothing android specific here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!