99

I am a Delphi programmer and have written, over the years, hundreds of classes and routines which I can use in every Delphi program I write.

This library is called dlib and can be used in every Delphi program by putting this folder in my library path and using one of the units in the uses section of a Delphi unit.

Being completely new to Java and Android development, I am wondering how to do this in similar way.

So my question, how can I write own classes, put them in some global folder, and use these classes and routines in every Android program I write ?

I know this is a basic question, which I can probably find out by searching Google and trying it out in Eclipse, but if someone can put me on the right track, I know I will save much time.

Thanks.

1
  • 3
    I would strongly caution against making these kind of general libraries. Java tends to have most of the functionality you need already, and more often than not, smarter people have done whatever you need doing already. What kind of things do your general libraries do?
    – Andrew
    Nov 3, 2010 at 13:37

6 Answers 6

94

You have to create Android Library Project. Create android project in Eclipse, enter Project Properties -> Android and check isLibrary property. Now you can add this library to your Android Application project by adding it to list on the same property page.

More detailed instructions here in Working with Library Projects section

3
  • If two different apps both use the same android library project, are two copies of the same library installed when both apps are installed on the same phone? Jul 5, 2014 at 7:36
  • @enigmaticPhysicist The library is actually pulled in at compile time. So yes, both apps have their own copies of the library bundled with them Jul 5, 2014 at 16:30
  • There has got to be a better way. Perhaps in a future version of Android. Jul 12, 2014 at 3:47
11

Instructions for creating a library in Android Studio:

Create a library module

To create a new library module in your project, proceed as follows:

  1. Click File > New > New Module.

  2. In the Create New Module window that appears, click Android Library, then click Next.

    There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects—especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.

  3. Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

Once the Gradle project sync completes, the library module appears in the Project panel on the left. If you don't see the new module folder, make sure it's displaying the Android view.

Convert an app module to a library module

If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:

  1. Open the module-level build.gradle file.

  2. Delete the line for the applicationId. Only an Android app module can define this.

  3. At the top of the file, you should see the following:

    apply plugin: 'com.android.application'

    Change it to the following:

    apply plugin: 'com.android.library'

    Save the file and click Tools > Android > Sync Project with Gradle Files.

1
  • Thanks for adding this answer, this is so much better with Android Studio. Aug 7, 2015 at 22:24
7

If your library is in .java files composed of java code. There's a really detailed tutorial of how to use the library at mobile.tutsplus.com. Link below:

http://mobile.tutsplus.com/tutorials/android/android-essentials-creating-android-compliant-libraries/

For Example I wanted to use the Pull To Refresh library by Chrisbanes at Github.com here https://github.com/chrisbanes/Android-PullToRefresh/tree/master/library. The library's structure is in the form of an Android app. It has the form like below:

res/
src/
AndroidManifest.xml
pom.xml
project.properties

How to use on Eclipse:

  1. Create new project in Eclipse. Give a name to your project. Select "Create project from existing source". Select the location of the root folder containing the above mentioned files in "Location". Select your target and click finish.
  2. Select properties of the newly project you created. Select "Android" option. Select the "Is Library" checkbox if it's not already selected. close properties.
  3. Add a reference to the library from the project that is going to use this library. Select your project that uses this library. Open Properties. Select "Android" option. At the bottom on the "Is Library". Don't select checkbox of "Is Library". Click "Add" button on the right. Your project that you created on step 1 and 2 should be listed ready for selection. select it and click apply. close properties.
  4. You're ready to reference the classes from your project.
1
  • thanks it help me... regards from our fgroupindonesia team :D
    – gumuruh
    Apr 3, 2014 at 2:50
4

With java, you create a Java Archive (jar) that contains all your classes (*.class files) of that library and the jar file is your library.

To use it, simply add it to the classpath.

(For "jar" and "classpath": basic Java concepts, please use google to find tutorials, you'll have to understand those concepts anyway, the sooner, the better ;) )

2
  • 2
    Hi, can I have drawables or layouts in my jar libraray?
    – Mahendran
    Mar 15, 2012 at 5:30
  • afaik, the answer is: no. Use the android library concept as described in the accepted answer for drawables and layouts.
    – Minsky
    Feb 18, 2013 at 7:39
2

Convert all of your class in Java and make a jar file. Use this jar in your android project by copying in libs/ folder and then adding in to build path. Make a clean of project and then run it.

0

If you're using new android studio version and gradle 7.0.3

Android Studio Arctic Fox | 2020.3.1 Patch 3 Build #AI-203.7717.56.2031.7784292, built on October 1, 2021 Runtime version: 11.0.10+0-b96-7249189 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 2 Registry: external.system.auto.import.disabled=true

Creating module

  1. Create new project with empty activity
  2. Click file -> new -> new module -> and choose android library.
  3. After new module created you can add java class or something for your library

Export library to AAR file

You can check this for more information

Export library to jitpack.io

Set this file like this

build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (module:app)

plugins {
    id 'com.android.application'
}

android {
    lintOptions {
        abortOnError false
    }
}

android {
    compileSdk 31

    defaultConfig {
        minSdk 16
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

build.gradle (module: YourLibraryName)

plugins {
    id 'com.android.library'
    id 'maven-publish'
}

task androidSourcesJar(type: Jar) {
    classifier 'sources'
    from android.sourceSets.main.java.srcDirs
}

project.afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId 'com.github.YourGithubUsername'
                from components.release
                artifact androidSourcesJar // optional sources
            }
        }
    }
}

android {
    compileSdk 31

    defaultConfig {
        minSdk 16
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Create file jitpack.yml in root project (YourProjectName -> Gradle -> right click -> new -> file -> name it "jitpack.yml" and put this code to jitpack.yml file

jdk:
  - openjdk11
before_install:
  - chmod +x gradlew
install:
#  - ./gradlew build :lib:publishToMavenLocal
  - ./gradlew build publishToMavenLocal

Share your project to Github

Visit jitpack website

Follow this step for upload your library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.