Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Android apps depending on android library with gradle

I am still learning about gradle but from what I have read, I am wondering if this is possible.

I have multiple android apps (app1,app2,app3), that depends on a android library (L). The android library (L) would depend on external libraries like volley, and the apps would depend on external libraries like picasso.

I dont want multiple copies of library and volley for each app. Here is what I was thinking my folder/gradle structure would look like:

app1/
  settings.gradle
  build.gradle
  src/
    com/
    test/
app2/
app3/
library/
  settings.gradle
  build.gradle
  src/
    com/
    test/
external/
  volley/
  picasso/

but I am not sure what my build.gradle file for app1 would look like since project dependencies (library) seems like it needs to be inside the app1 folder.

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':library')
}

android {
    buildToolsVersion "17.0"
    compileSdkVersion 18

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

        instrumentTest.setRoot('tests')
    }
} 

What would be the best way for me to structure my projects to use gradle?

like image 432
heero Avatar asked Nov 12 '13 18:11

heero


People also ask

How many build gradle file is there in one android project?

gradle files in an Android Studio project?

What is the difference between AAR and jar?

Unlike JAR files, AAR files offer the following functionality for Android applications: AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.

How do I add a dependency in build gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is classpath in gradle?

Gradle dependencies are grouped into sets called configurations. Different configurations are used for building classpath for the major two tasks — compile classpath is used for compilation and runtime classpath is used for running the application.


2 Answers

You could use this structure:

app1/
  build.gradle
app2/
app3/
library/
  src/
     main
  libs
  build.gradle

settings.gradle

In settings.gradle

include ':library' ,':app1' , ':app2', ':app3'

In app1/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19            
    }
}

dependencies {
   //Library
   compile project(':library')
   // Support Libraries
   compile 'com.android.support:support-v4:19.0.0'
   //OkHttp
   compile 'com.squareup.okhttp:okhttp:1.2.1'
   //Picasso
    compile 'com.squareup.picasso:picasso:2.1.1'
}

In library/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19

    }
}

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

With aar from maven (as picasso), Gradle will detect multiple instances of the same dependency, and manage things for you. With jar (as Volley) you have to pay attention. You can't add it twice. if your Library project has a dependency on a jar, then the APK project will inherit this dependency.

like image 153
Gabriele Mariotti Avatar answered Nov 18 '22 19:11

Gabriele Mariotti


You're confused about settings.gradle. It should be at the root of your multi-module folder structure.

So you have two choices:

  • Create a single multi-module setup where you have 4 modules (library, app1, app2, app3). In this case you'll always open all 4 projects in studio, you'll be able to build all for apps at the same times, and they will all use the same version of the library. This probably mean you need to have each app be developed at the same time, and maybe release at the same time (depending on the development cycle of the library project)

  • Create 4 independent projects. In this case you are developing the library separately and publishing it in a (internal) repository using whatever versioning you want. Then each app can depend on a different version of the library.

like image 21
Xavier Ducrohet Avatar answered Nov 18 '22 19:11

Xavier Ducrohet