根据与摇篮的Andr​​oid库中的多个Android应用

根据与摇篮的Andr​​oid库中的多个Android应用

问题描述:

我仍然在学习摇篮但从我看过,我想知道如果这是可能的。

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

我有多个Android应用(APP1,APP2,APP3),这依赖于Android的库(L)。而Android库(L)将依赖于外部库,比如排球,和应用程序将依赖于外部库,比如毕加索。

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/

但我不知道我对APP1 build.gradle文件会是什么样子,因为项目的依赖关系(库)好像它需要的APP1文件夹内。

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?

您可以使用此结构:

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

settings.gradle

在settings.gradle

In settings.gradle

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

在APP1 / build.gradle

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

在库/ build.gradle

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

通过从行家AAR(毕加索),摇篮会发现同样的依赖性的多个实例,并管理你的东西。 随着瓶(如排球),你一定要注意。不能添加两次。 如果你的库项目有一个罐子的依赖,那么APK项目将继承这种依赖性。

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.