Showing posts with label Gradle. Show all posts
Showing posts with label Gradle. Show all posts

Tuesday, January 7, 2014

Install Android Library on maven with gradle

If we need to hide the password or any other confidential information, we can move the info into gradle global properties. To do so, we put the info in the file at $GRADLE_HOME/gradle.properties. If it doesn't exist just create a new one.

Here is the content of file %GRADLE_HOME/gradle.properties.

signing.keyId=1C5E1752
signing.password=topsecret
signing.secretKeyRingFile=C:/Users/xxx/AppData/Roaming/gnupg/secring.gpg

artifactoryAndroidDev=http://artifactory.dev.cba/artifactory/android-dev/
artifactoryUsername=xxx
artifactoryPassword=topsecret
nexusSnapshotRepo=http://localhost:8081/nexus/content/repositories/snapshots/
nexusReleaseRepo=http://localhost:8081/nexus/content/repositories/releases/
nexusUsername=admin
nexusPassword=xxx

The we refer the info in the build.gradle file. Here is the gradle build file I used to install slidingmenu library.

buildscript {
    repositories {
        mavenCentral()
  maven {
            url 'http://localhost:8081/nexus/content/repositories/central/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android-library'

repositories {
    maven {
        url 'http://localhost:8081/nexus/content/repositories/central/'
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
}

android {
    compileSdkVersion 19
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    sourceSets {
        main {
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']

            manifest.srcFile 'AndroidManifest.xml'
        }
    }

}

apply plugin: 'maven'
apply plugin: 'signing'
version = "1.3-release"
group = "com.jeremyfeinstein"
configurations {
    archives {
        extendsFrom configurations.default
    }
}

//Singing the library for release version.
signing {
    required { version.contains("release") && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

uploadArchives {
    configuration = configurations.archives
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

//nexusReleaseRepo, nexusUsername and nexusPassword are defined in gradle global properties which are stored in $GRADLE_HOME/gradle.properties.
        repository(url: nexusReleaseRepo) {
            authentication(userName: nexusUsername, password: nexusPassword)
        }

        pom.project {
            name 'Android SlidingMenu Library'
            packaging 'aar'
            description 'Sliding menu library for Android applications'
            url 'https://github.com/jfeinstein10/SlidingMenu'

 //           scm {
 //               url 'scm:git@github.com:VandalSoftware/android-cache-lib.git'
 //               connection 'scm:git@github.com:VandalSoftware/android-cache-lib.git'
 //               developerConnection 'scm:git@github.com:VandalSoftware/android-cache-lib.git'
 //           }

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'jfeinstein10'
                    name 'Jeremy Feinstein'
                    email 'jfeinstein10@gmail.com'
                }
            }
        }
    }
}

Sunday, December 8, 2013

Global properties in gradle

When we have multiple projects and we may want to refer global properties such as dependencies by all sub-projects.

For example we have a project structure like this
+ProjectX
\build.gradleX
\----SubProjectA
\--------build.gradleA
\----SubProjectB
\--------build.gradleB

So we have a root project called ProjectX and two sub projects called ProjectA and ProjectB. Each of them has their own build.gradle file for gradle build. By default, they all should be named as build.gradle. To distinguish them, we denote them as

  • build.gradleX
  • build.gradleA
  • build.gradleB

Say we want to use the same version of JUnit in all projects, so we need to use the same JUnit dependency and reference the same dependency across all projects.

In build.gradleX, we define a property
ext {
    lib = [
        junit: 'junit:junit:4.11'
    ]
}

In build.gradleA and build.gradleB we refer the dependency by
dependencies {
     compile lib.junit  //This references the property defined in build.gradleX
}

Improvement:
The mechanism of the above code is, the build.gradleX defines a property in root project(ProjectX) so in its subjects, lib.junit will be read inherited from the root project. But if we define ext.lib in PojectA as well like below then the lib.junit will use its local value. This is good if we want to override the value but if we don't know what happens behind the scene, mistake occurs.
in build.gradleA
ext {
    lib = [
        junit: 'junit:junit:3.0.0'
    ]
}

So, to refer the lib.ext we use
rootProject.lib.junit    //referencing the absolute root project 
or
parent.lib.junit         //referencing relative parent project which is the root here