How to import a .aar file into Android Studio 1.1.0 and use it in my code
After reading a lot of answers on Stackoverflow, I found the solution for my problem, I want you to know which were the steps I followed in order to reproduce it:
- Add a .aar file in my libs folder.
- Use "New Module" option under File menu.
- Import the .aar file.
- Build gradle and compile the project.
When I tried to use the new module in my app, It didn't recognize any class inside the new module.
The problem is related to the version of Gradle, I was using 1.1.0 and there is a bug in this version, so my suggestion is to change the version to 1.0.1, there is an Issue already open in order to fix this problem https://code.google.com/p/android/issues/detail?id=162634
You should change the version in the build.gradle file located in the root of your project.
buildscript {
repositories {
jcenter()
}
dependencies {
//classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.android.tools.build:gradle:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
You can find additional information about this problem here https://groups.google.com/forum/#!topic/adt-dev/1Ho_c8dALQQ
I guess in version 1.2.0 this problem will be solved.
Adding local .aar files to Gradle build using flatDirs is not working
Building upon Josiah's answer, here's how I got it to work.
Following his instructions (under edit) (File -> New-> New Module -> Import .JAR/.AAR) and import your .AAR.
Then in your project build.gradle (not the top level one, the one under 'app') add the following (in the dependencies section):
dependencies {
compile project(':Name-Of-Your-Project')
}
Note Name-Of-Your-Project should match the name of the folder that was added after you imported the AAR file (at the same level as app/.idea
under the top most level folder). Or to put it another way...
MyApplication
.idea
app
build.gradle (here's where to add compile project(':ProjectName') to dependency section)
ProjectName (added automatically after importing, matching the name of your aar file)
build
gradle
etc
This worked for me running Android Studio 0.8.0. Don't forget to synchronize gradle (using toolbar button or in File->Synchronize) after you do this.
(Thanks to Josiah for getting me going in the right direction)
(Note: prior to this I tried adding it to the libs folder, trying to manipulate the top level build.gradle
and the app level build.gradle
, but none of that worked for my aars files--jar's will work fine, but not the aar files)
Not able to access class from .aar file
Finally I found solution even on Gradle 1.1.0.
I found help from Here by @AliDK answer.
create a folder in app
by name aar
.
make a copy from myLibrary.aar
and put in app/aar
. then
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
flatDir {
dirs 'aar'
}
}
}
and write below code in MyProject/app > build.gradle
dependencies {
...
compile(name: 'myLibrary', ext: 'aar')
}
And it worked for me after spending 2 days.
Thanks to @AliDK..
I hope it would work for others too.
Can't load .so file stored in a library (aar) from an application project
I finally figure it out by myself.
The first problem comes from my cmakelists.txt
I create my-lib-1 from c sources files then I created my-lib-2 in order to link my-lib-1 with some cpp files for jniexport.
But the link to my-lib-1 was correctly set for the compilation time, but when I created my-lib-2 from my-lib-1 the reference to my-lib-1 was broken.
Using System.loadlibrary()
method to load my-lib-2 raised an error : java.lang.UnsatisfiedLinkError
saying that my-lib-1 (not my-lib-2) wasn't found. That's how I fix the first problem.
Solution :
# mylib1 - source files
file(GLOB SOURCES ${mylib1_source_DIR}/src/*.c)
# mylib1 - include header files
include_directories(${mylib1_source_DIR}/include/)
# compile and add our cpp files - Shared library
add_library(mylib2 SHARED ${SOURCES} mylib2.cpp)
# logger
find_library(log-lib log)
# include our header file
target_include_directories( mylib2 PRIVATE ${mylib1_source_DIR}/include )
# link our library (mylib2) with android and log libraries
target_link_libraries( mylib2 android ${log-lib})
The second problem comes from relinker
The library is compiled. I use Relinker to load my library, but Relinker is unable to find my-lib-2. Relinker is looking for the library in the wrong folder (lib folder instead of jni folder).
fyi : lib folder is the library folder for an android app
jni folder is the library folder for an aar library
The reason is probably because Relinker is called from an android application, which mean it's going to look at the application native library folder and not inside the aar folder (which contains my-lib-2)
Solution :
1. Remove relinker
2. load library from my aar classes :
System.loadLibrary(my_lib_2);
That's it
Thanks @bruno for your help
building an aar module
Did you add
repositories {
flatDir {
dirs 'libs'
}
}
in your project's build.gradle?
Did you add
compile(name: 'xxx', ext: 'aar')。
in your module's build.gradle?
How do I add a library project to Android Studio?
Update for Android Studio 1.0
Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.
My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).
Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle
file:
dependencies {
implementation 'com.jakewharton:butterknife:6.0.0'
}
Adding the library
Here is the full process of adding external Android library to our project:
- Create a new project via Android Studio creator. I named it HelloWorld.
- Here is the original project structure created by Android Studio:
HelloWorld/
app/
- build.gradle // local Gradle configuration (for app only)
...
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...
- In the root directory (
HelloWorld/
), create new folder:/libs
in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure). - Paste your library in the newly created
/libs
folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
HelloWorld/
app/
- build.gradle // Local Gradle configuration (for app only)
...
libs/
PagerSlidingTabStrip/
- build.gradle // Local Gradle configuration (for library only)
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...
Edit settings.gradle by adding your library to
include
. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:include ':app', ':PagerSlidingTabStrip'
project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
5.1 If you face "Default Configuration" error, then try this instead of step 5,
include ':app'
include ':libs:PagerSlidingTabStrip'
In
app/build.gradle
add our library project as an dependency:dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation project(":PagerSlidingTabStrip")
}
6.1. If you followed step 5.1, then follow this instead of 6,
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation project(":libs:PagerSlidingTabStrip")
}
If your library project doesn’t have
build.gradle
file you have to create it manually. Here is example of that file:apply plugin: 'com.android.library'
dependencies {
implementation 'com.android.support:support-v4:21.0.3'
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit
gradle.properties
file and add lines:ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.1.3
ANDROID_BUILD_SDK_VERSION=21Now you can use it in your
build.gradle
files (in app and libraries modules) like below://...
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)
}
}
//...That’s all. Just click‚ synchronise the project with the Gradle’ icon . Your library should be available in your project.
Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:
Android Studio is all about editing, and debugging and profiling.
It's not about building any more.
At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.
Related Topics
Android:Change Button Text and Background Color
Delete File from Internal Storage
Android Button Background Color
Using an Android Library Project Activity Within Another Project
Google Drive API Doesn't Play Well with Proguard (Npe)
Could Not Find Com.Android.Tools.Lint:Lint-Gradle Android Studio 3
Android:Record Sound in Mp3 Format
Deep Linking Not Working in Chrome
Google Play Services Missing in Emulator (Android 4.4.2)
How Does One Implement Drag and Drop for Android Marker
Android Studio- Program Type Already Present: Com.Google.Android.Gms.Internal.Measurement.Zzwp
Copy Database File to Sdcard in Android
How to Use Selector to Tint Imageview
Load Images from Disk Cache with Picasso If Offline
Access_Coarse_Location Permission Gives a Cell Tower Precision on Android