buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.2'
    }
}

// We need to apply the android plugin to be able to use $android for getSdkPath
apply plugin: 'com.android.application'

allprojects {
    ext {
        toolVersion = '21.1.2'
        sdkLoc = getSdkPath()
    }
}

project(':extern:UniversalImageLoader:library') {
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.10.0'
        }
    }

    apply plugin: 'android-library'

    android {
        compileSdkVersion 16
        buildToolsVersion toolVersion

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

project(':extern:zipsigner') {

    apply plugin: 'java'

    dependencies {
        compile project(':extern:spongycastle:core')
        compile project(':extern:spongycastle:pkix')
        compile project(':extern:spongycastle:prov')
    }
}

project(':support-v4') {

    apply plugin: 'maven'

    // The support-v4 library assumes certain things are defined in the 
    // root project (which is usually the android-support project, but 
    // this time it is the F-Droid project.
    rootProject.ext.supportRepoOut = ""

}

/**
 * Currently a bit hacky, because android.plugin is protected.
 * The end goal is to find something in the android BaseExtension class found here:
 *  https://android.googlesource.com/platform/tools/build/+/master/gradle/src/main/groovy/com/android/build/gradle/BaseExtension.groovy
 * which ends up asking their Sdk class for it's location. That class knows about
 * both ANDROID_HOME env variables, and also local.properties sdk.loc properties.
 * 
 * If in the future, the android.adbExe is found to be inappropriate, deprecated,
 * or a better way of finding the sdk path exists, we can change the implementation
 * of this method to reflect that.
 */
def getSdkPath() {
    new File("$android.adbExe/../../").canonicalPath
}

FileCollection getAndroidPrebuilt(String apiLevel) {
    files("$sdkLoc/platforms/android-$apiLevel/android.jar")
}

subprojects {

    // This is the hacky way which we force the subprojects to use the same build tools:
    // http://stackoverflow.com/a/21032272
    afterEvaluate {
        android {

            // The android build task only lets you configure the buildToolsVersion once, so if
            // we execute the closure below to configure our subprojects, it will fail when it
            // hits the second subproject. Therefore, we will only do it once, and I guess the
            // android plubin will re-use the existing value I set. 
            // https://android.googlesource.com/platform/tools/build/+/master/gradle/src/main/groovy/com/android/build/gradle/BaseExtension.groovy
            try {
                buildToolsVersion toolVersion
                logger.info("Set buildToolsVersion to $toolVersion")
            } catch (GradleException e) {
                logger.info("Tried to set the buildToolsVersion, however we were not allowed to: $e.message")
            }
        }
    }
}