Multiple Android apps depending on android library with gradle

删除回忆录丶 提交于 2019-12-04 02:49:14

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.

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.

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