How to Add a Library Project to Android Studio

From WikiHTP

When working on an Android project, you may need methods or features that you have already developed in another project. It is possible with Android Studio to include another project as a dependency, which gives you access to its methods. You can do this using 2 ways: with the Visual Studio GUI, or with the Gradle production engine. So it is possible with Android Studio to include another project as a dependency, which gives you access to its methods. Here's we are going to discuss how to add a library project to the Android Studio.

Via the Visual Studio GUI[edit]

To perform the manipulation with the GUI, first, open the project in which you want to add the library. From the File menu, select the Project Structure option. In the project structure management window, select in the left column the Modules option, then Import Module. Then select the library corresponding to the project you want to import. Then choose the module representing the project you are developing and then add addency. Choose the previously created module. The methods of your project are imported.

The handling may vary depending on the version of Android Studio, but it is still close to this technique. For example, for Android Studio 0.8.2, the manipulation is faster. In the project structure management window, go to the Modules tab and click on the plus button. Choose the option Import Existing Project, and select your project.

Once the project is imported, synchronize your Gradle files. The following message may appear Error: The Build SDK revision (XX.XX) is too low. Minimum required is YY.YY If so, go to the project directory to import, and open the build.gradle file. Edit the buildToolsVersion section in the file to indicate the version of Android suggested by Android Studio

buildToolsVersion 'YY.Y.Y'

Via the Gradle production engine[edit]

The Gradle production engine can be used in Android Studio to import your project as a library. If you do not want to duplicate your code and the project you want to import can change, you can add the project as an external dependency. Just add a line of code in the app/build.gradle file of your project:

dependencies {
    compile 'com.yourProject.1.0.0'
}

If you prefer to duplicate the code so that the project remains independent, you must add the project as a library. In your folder, create a libs folder. This step is not obligatory, but it allows to arrange the project well. Add the project to import into the libs folder. At the root of your project, edit the settings.gradle file. You must add your project in the include section. If it is in a particular file, as in this case, it is also necessary to indicate the file with one more directive:

include ': app', ': ProjectAmporter'
project (': ProjectAmporter'). projectDir = new File ('libs / ProjectAImporter')

If you get the Default Configuration error in your project, then use the following syntax:

include ': app' 
include ': libs: ProjectAmporter'

In the build.gradle file located in the app folder, add your project as a dependency:

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

If you used the second notation to add the project to the settings.gradle file, then the notation changes:

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

If your project does not have a build.gradle file located at the root, then you need to create one. Here is an example of a file:

apply plugin: 'com.android.library'
dependencies {
    compile 'com.android.support:support-v4:21.0.3'
}
android {
    compileSdkVersion 21
    buildToolsVersion 21.1.2
    defaultConfig {
        minSdkVersion 14
    targetSdkVersion 21
}
sourceSets {
    hand {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        res.srcDirs = ['res']
   }
}

It is possible to use variables in the configuration of the build.gradle file. They must be added to the gradle.properties file:

ANDROID_BUILD_MIN_SDK_VERSION = 14
ANDROID_BUILD_TARGET_SDK_VERSION = 21
ANDROID_BUILD_TOOLS_VERSION = 21.1.3
ANDROID_BUILD_SDK_VERSION = 21

There is only to use them in the build.gradle file:

android {
    compileSdkVersion Integer.parseInt (project.ANDROID_BUILD_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    defaultConfig {
        minSdkVersion Integer.parseInt (project.ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdkVersion Integer.parseInt (project.ANDROID_BUILD_TARGET_SDK_VERSION)
    }
}

Your project is now ready to be imported. In Android Studio, click the Sync project with Gradle button. The project you want to import will then be available as a library.

About This Tutorial

This page was last edited on 28 January 2019, at 06:34.