How to create a Library-Android using Android Studio?

Asked

Viewed 1,726 times

4

I have some projects in development process and have several common classes for these projects. I would like to know how to create and use a Lib of the classes so I can reuse them whenever I need without the need to keep rewriting these classes.

2 answers

4


I was successful in creating my Library. The tutorial mentioned in Leonardo Dias' reply provided me with a greater understanding of how construction and compilation works. However I could not fully achieve my goal with him alone.

To export and create a file . JAR that can be implemented I did as follows:

1st Step: Create the common Android project with the classes that will be reused.

Step 2: Change the file app/build.Radle in the first line to be as follows:

    apply plugin: 'com.android.library'

Step 3: Change the structure of the "task" Android keeping a body roughly equal to this:

    android {
        compileSdkVersion 24
        buildToolsVersion "24.0.2"

        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 24
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

Step 4: Implement at the end of the file the following "task" which will be responsible for creating the file. JAR within the project:

    //Task para deletar os JAR velhos
    task deleteOldJar(type: Delete){
        delete 'release/AndroidPlugin.jar'
    }

    //Task para exportar conteudos para JAR
    task exportJar(type: Copy){
        from('build/intermediates/bundles/release/')
        into('release/')
        include('classes.jar')

        rename('classes.jar', 'AndroidPlugin.jar')
    }

Step 5: Sync the Gradle file that has just been edited for it to build the structure that will be used in the . JAR generation.

Step 6: Access the "Gradle Projects" window that is located in the upper right corner of the screen and navigate to the file "Projeto">"Projeto">Tasks>other>exportJar and execute him.

Step 7: Access the project folder via Explorer and navigate to the location of the generated file. It will probably be in the folder C:\Users\"Usuario"\AndroidStudioProjects\"Projeto"\app\release if the installation of Android Studio has been done as standard.

At this time the file created can already be used in any other Android project using the common import processes. To do this just copy it and paste it into the "Libs" folder of the project that will use the shared classes. This folder is not visible if you are using the "Android" file display structure. To display it inside Android Studio change the view of the project files to type "Project".

8th Step: After copying the library to the Libs folder the file will be displayed within the IDE. To add to the project right click on it and select the option Add as Library and select which module to lib will be incorporated.

9th Step: Finally to freely use the classes just make a import in the project class that is receiving the lib. It will be possible to access all resources created in the library fluently.

Considerations To write this step by step I watched the following video as a complement:

https://www.youtube.com/watch?v=1i4I-Nph-Cw

2

You must create a normal project

The difference is in the file app/build.Radle, that you need to indicate to the Gradle build that this project is a lib, then add the following lines at the beginning of the file:

apply plugin: 'com.android.library'
apply plugin: 'maven'

Then you’ll need to add in the file app/build.gradland settings about your library and where it should be installed, i.e., where your repository is located.

The repository can be an Internet URL (if there is a server), or even a local folder of the computer, example:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file:///home/ricardo/gradle/rep")

            pom.groupId = GROUP
            pom.artifactId = POM_ARTIFACT_ID
            pom.version = VERSION_NAME
        }
    }
}
task install(dependsOn: uploadArchives)

In this case, a task uploadArchives was created that defines the repository where lib should be installed.

The last line of this setting indicates that the task install depends on the task uploadArchives. So if you run the task install you will also upload the lib to the repository.

Here’s an example of what the file would look like app/build.Radle complete:

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            //repository(url: "file:///C:/gradle/rep/")
            repository(url: "file:///home/ricardo/gradle/rep")

            pom.groupId = GROUP
            pom.artifactId = POM_ARTIFACT_ID
            pom.version = VERSION_NAME
        }
    }
}
task install(dependsOn: uploadArchives)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

Look at this link the full explanation of where this tutorial was taken from

Or you can follow the Documentation of Android which teaches how you compile your lib into a file .jar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.