How do I create a Library-Android using Android Studio?

4

I have some projects under development and I have several classes in common for these projects. I would like to know how to create and use a Class Lib so that I can reuse them whenever I need them without having to re-write these classes.

    
asked by anonymous 01.11.2016 / 20:07

2 answers

4

I was successful in creating my Library. The tutorial mentioned in Leonardo Dias's response gave me a greater understanding of how building and compiling works. However I could not quite reach my goal with him alone.

To do the export and create a .jar that can be implemented I did as follows:

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

Step 2: Change the app / build.gradle file in the first line to look like this:

    apply plugin: 'com.android.library'

Step 3: Change the structure of the Android "task" maintaining a body approximately 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 the following "task" at the end of the file that will be responsible for creating the .JAR file 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: Give a Sync in the Gradle file that has just been edited so that it builds the structure that will be used in the .JAR generation.

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

Step 7: Access via Explorer the project folder and navigate to the location where the file that was generated is located. It will probably be in the C:\Users\"Usuario"\AndroidStudioProjects\"Projeto"\app\release folder if the Android Studio installation was done by default.

At this point the created file can already be used on 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 view structure. To display it within Android Studio change the display of the project files to the "Project" type.

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

Step 9: Finally to freely use the classes just make a import in the project class that is receiving the lib. This will allow you to access all the resources created in the library.

Considerations To write this step by step I watched the following video as an add-on:

link

    
01.11.2016 / 23:26
2

You should create a normal project

The difference is in the app / build.gradle file, which you need to tell the gradle build that this project is a lib, so add the following lines at the beginning of the file:

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

Then you will need to add the app / build.gradl file and the settings about your library and the location where it should be installed, ie the location of your repository.

The repository can be an internet URL (if there is a server), or even a local computer folder, for 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 the task uploadArchives was created which defines the repository where the 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, the lib will also be uploaded to the repository.

Here's an example of what the app / build.gradle file would look like:

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'
}

See in this link the full explanation of where this tutorial was removed

Or you can follow the Android documentation that teaches you how to compile your lib in a .jar file.

    
01.11.2016 / 20:24