Android package and import aar file

We can import 3rd library in Android project with .jar or .aar file.

But .jar file only contains .class files, can’t use resource files.
For this reason, we use .aar file that can use resource file.

Package .aar file

Choose non-activity module.

If this module show cann’t run or build.

In Edit Configurations, change Launch Options is Nothing.

Step 1: Setup Output Mode

The gradle definition of apply plugin is

1
2
apply plugin: 'com.android.application'  //output mode : apk
apply plugin: 'com.android.library' //output mode : library

In build.gradle, setup output mode.

build.gradle
1
apply plugin: 'com.android.library' 

Step 2: delete applicationId**

build.gradle
1
2
3
4
defaultConfig {
//applicationId "com.nickthomas55gmail.testaar"
...
}

Step 3: generate .aar file

Open Gradle Projects (View -> Tool Windows -> Gradle) or click gradle button.

In projects root folder, click assemble.

After build, We can get the .aar file in build -> outputs -> aar folder.

module_name-debug.aar
module_name-release.aar

Import .aar file

In Project Structure, choose Import Jar/Aar Package to import .aar file.

At dependencies in build.gradle, add the line : compile project *** :module_name***

build.gradle
1
2
3
4
5
dependencies {
compile project(':module_name-release')
...
}

After that, we can use all classes and resources.

Author

Nick Lin

Posted on

2018-08-03

Updated on

2023-01-18

Licensed under


Comments