How To Create Your Own Android Library




Create a Library Module

1. Click on File => New => New Module.



2. On the next window select Android Library as a template and enter your module name as shown in the image and click on Finish.


3. When you click on Finish the gradle build will start and add the newly created module to your project.

The below image shows the project structure before creating the library module and you can see there is only an app module.



The below image shows the structure after creating the library module and you can see that the new module is visible ie. networkmodule.


4. The Below image shows the structure of the library module and it is similar to a normal Android app module.

Now let’s see what happened under the hood when we created the library module.

For Old Version:


   build.gradle (Project: Level)
       
    
        
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.0'
        classpath 'com.google.gms:google-services:4.3.14'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        jcenter()
    }
}



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


         




For New Version:

   build.gradle (Project: Level)
 
        
        
        plugins {
    id("com.android.application") version "8.2.1" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("com.android.library") version "8.2.1" apply false
}
               




build.gradle (Module :app)
  
  
     //Old version
     plugins {
    id 'com.android.application'
    id("org.jetbrains.kotlin.android")
    id("maven-publish")
    }

  //New Version 
   plugins {
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("maven-publish")
    }
  
  
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

	    kotlinOptions {
        jvmTarget = '17'
    }
    
    
 implementation project(":customtoast") //your module name
        
        







build.gradle (Module :customtoast)
  
     
      //Old version
     plugins {
    id 'com.android.application'
    id("org.jetbrains.kotlin.android")
    id("maven-publish")
    }

  //New Version 
   plugins {
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("maven-publish")
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    
    
    
    
    afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release // or components.android if needed
                groupId = 'com.nbsoftdev' //package name
                artifactId = 'customtoast' //module name
                version = '1.0.6'
            }
        }
    }
}

        
        










Previous Post Next Post