27

I'm working on creating a log-in screen to be used with multiple different android applications. What would be the best way to package it so that other people could use my log-in function on their apps. It would be preferred that it would auto-sync for them in-case we were to make changes. ***EDIT**** It seems packaging it into a library module is the best option. How does one go about uploading this module so that if we make an update to this module it will seamlessly update without having to pull from github for example.

Thanks!

Ryan Newsom
  • 696
  • 7
  • 12
  • possible duplicate of [How to create your own library for Android development to be used in every program you write?](http://stackoverflow.com/questions/4085313/how-to-create-your-own-library-for-android-development-to-be-used-in-every-progr) – BSMP Jun 09 '15 at 19:46
  • Publish your project on GitHub. (Edit: sorry, meant to post this as comment not answer.) – hungryghost Jun 09 '15 at 19:46
  • 2
    Check this tutorial https://github.com/danielemaddaluno/gradle-jcenter-publish – Pavel Synek Jun 09 '15 at 19:49
  • put it on maven-central – njzk2 Jul 03 '15 at 16:12
  • There's an example here http://stackoverflow.com/a/30798785/3975649 using JitPack – metrimer Jul 08 '15 at 08:27
  • I recently got some upvotes on this so I thought I would post an update. The best way I have found of doing this, is using a maven repo. Create a gradle task which uploads your .aar to it. Hook this up to whatever CI you want, and you're good to go. If you don't have access to a maven repo, Andrejs solution will work. – Ryan Newsom Oct 17 '16 at 18:44

5 Answers5

21

If you've pushed your code to GitHub then sharing the library (aar) is easy with JitPack.

Your users will just need to add the repository to their build.gradle:

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

and then your GitHub repository as dependency:

dependencies {
    // ...
    compile 'com.github.YourUsername:Repo:Release'
}

The nice thing is that you don't have to upload your library. Behind the scenes JitPack will check out the code from GitHub and compile it. As you publish a new release on GitHub it becomes available for others to use.

There is also a guide on how to prepare an Android project.

Andrejs
  • 26,885
  • 12
  • 107
  • 96
  • 1
    This is really cool because it doesn't require projects that already use GitHub to upload to another repo. Too bad JitPack doesn't yet support the NDK. – rmtheis Jul 10 '15 at 19:19
  • The [guide](https://github.com/jitpack/jitpack.io/blob/master/ANDROID.md#installing) says users of the repository will also need to add `jcenter()` to the `repositories` section of their build.gradle. – rmtheis Jul 10 '15 at 19:24
  • Thanks Andrejs! Decent solution.. :) – Martin Pfeffer Jan 02 '16 at 19:51
  • Is it necessary that my github repo should only contain library module and not app modules? – Talha Jun 27 '17 at 08:32
5

Make the relevant classes into a library module - you already seem to know how to do that - and then use the Gradle Bintray plugin to upload it to JCenter.

Let's say you set group in build.gradle to com.ryan-newsom, version to 1.0 and the project name is android-log-in-screen.

(part of) android-log-in-screen/build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6"
    }
}

apply plugin: 'com.jfrog.bintray'

group = 'com.ryan-newsom'
version = '1.0'

bintray {
    // Omitted for brevity, refer to the examples on GitHub.
}

You (or anyone else) can then use it in your project by adding the following:

(part of) other-project/build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile "com.ryan-newsom:android-log-in-screen:1.0"
}

The library will then be pulled from JCenter and added to the classpath.

Raniz
  • 10,882
  • 1
  • 32
  • 64
3

You can package the library into an AAR format. It will also contain the resources you used in your login module. After that you can push the AAR library format to bintray (which is free, and allows you to setup your own repository).

Your collaborators can then access the library using a dependency that looks like:

compile 'com.newsom:awesome-login-screen:0.5'

Check this starter tutorial if you are using AndroidStudio/Gradle and would like to push it to bintray. https://github.com/jimcoven/android-bintray-kit

Peanuts
  • 352
  • 3
  • 6
2

The best way to create a lib and make it available to other developers is creating a AAR so that developers can import it in their project using dependencies.

The process is quite long. These are the main steps you should follow to publish your lib:

I wrote a post about it and to have more details you can look here. This is a piece of gradle file called maven_push.gradle:

apply plugin: 'maven'
apply plugin: 'signing'

def sonatypeRepositoryUrl
if (isReleaseBuild()) {
    println 'RELEASE BUILD
    sonatypeRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
            : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
    println 'SNAPSHOT BUILD'
    sonatypeRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
            : "https://oss.sonatype.org/content/repositories/snapshots/"

}

def getRepositoryUsername() {
    return hasProperty('nexusUsername') ? nexusUsername : ""
}

def getRepositoryPassword() {
    return hasProperty('nexusPassword') ? nexusPassword : ""
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                pom.artifactId = POM_ARTIFACT_ID

                repository(url: sonatypeRepositoryUrl) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_DESCRIPTION
                    url POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            id POM_DEVELOPER_ID
                            name POM_DEVELOPER_NAME
                        }
                    }
                }
            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.allJava
        classpath += project.files(android.plugin.getRuntimeJarList().join(File.pathSeparator))
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        //basename = artifact_id
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        //basename = artifact_id
        from android.sourceSets.main.allSource
    }

    artifacts {
        //archives packageReleaseJar
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

while gradle.properties is:

VERSION_NAME= 
VERSION_CODE=1
GROUP=
POM_DESCRIPTION=
POM_URL=
POM_SCM_URL= POM_SCM_CONNECTION=
POM_SCM_DEV_CONNECTION=scm:git@github.com:
POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo 
POM_DEVELOPER_ID=
POM_DEVELOPER_NAME=

There is another way but i did not try it and it seems to be easier. Give a look at jitpack.

Hope it helps you.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
1

make the package or jar depending on your source, and post it on git hub the you can refer to the git from your ide to import or check for updates.

Technivorous
  • 1,682
  • 2
  • 16
  • 22