0
votes

i have a library which uses all of our apps. except for shared code and resources i wanted to share gradle code. each app is constructed: some_app/ | - settings.gradle - build.gradle | - mylib/ | -build.gradle - other_lib/ - app/ | - build.gradle

right now i'm talking about the build.gradle in the root dir. The one that defines buildScript closure with dependecies and allprojects{} closure as well.

i tried to use apply from: './mylib/gradle_files/baseProject.gradle' it works, but then my app can't find the android plugin. for some reason the classpath dependencies were not injected into the build script! i then tried to create a list and use that:

ext.projectBaseDependencies = ['com.android.tools.build:gradle:2.1.2', 'com.google.gms:google-services:3.0.0']

I then used the list in my closure:

apply from: './mylib/gradle_files/baseProject.gradle'

buildscript { repositories { jcenter() } dependencies { classpath projectBaseDependencies } }

it doesn't work, gradle does not find my variable defined in baseProject.gradle. I have a hunch that apply from works AFTER buildScript closure is evaluated :-(

Is there any way to share gradle script code across ALL apps ? if not apply from, what CAN work ? skip configuration and config in afterEvaluate {} clause ?

1

1 Answers

0
votes

Why not use a Gradle file in your common library? If your common library includes a build.gradle file, you can apply the android library plugin:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        consumerProguardFiles "proguard-rules.pro"
    }
}

dependencies {
    // Your common dependencies here
}

Then you can include the library in your project's settings.gradle:

include ':mobile'
project(':common').projectDir = new File("/Users/example/path/to/common")

Then add it to your dependencies in the project's build.gradle:

dependencies {
    compile project(':common')
    // Your other project dependencies
}