I have a set of utils and custom widgets that I want to pull out of my project as an Android library so that I can use them in other projects (and possibly share in the future). I created a new Android Studio project and changed the build.gradle file so that 'com.android.application' was 'com.android.library' and deleted the applicationId. It all compiles fine and I have a .aar file created.
I now want to use this new library as a module in my original project. When I do an Import Project, all the files from the library project are copied into my original project. But I don't want that because if I change the imported library code, it isn't reflected in the library project or vice versa.
I also tried adding this to settings.gradle:
include ':myutils'
project(':myutils').projectDir = new File(settingsDir, '../../../../Development/MyUtils/')
and in the original project app build.gradle:
dependencies {
implementation project(':myutils')
...
But I get this error:
ERROR: Unable to resolve dependency for ':app#debugUnitTest/compileClasspath': Could not resolve project :myutils.
Show Details
Affected Modules: app
How can I link the library to my project without importing it? I would prefer not to go through an external maven repo yet. I'm happy to (and would expect to) recompile my library whenever there is a change there and then rebuild my original project.
Thank you in advance.
I think I just had the same problem - I wanted to put the library .aar file somewhere on my local drive and then use it in a new app as a dependency. I didn't want to have to go through a repo or to include it in the libs folder in the new app. Hopefully that is what the OP asked and the following might be of help to others.
Searching on SO (Aug 2021), majority of answers seemed much more involved than what Android Studio offers (tested on version 4.2). That is, an .aar file that lives outside the app project can now be added as an implementation file in the gradle dependencies. So, it doesn't have to go through a repo, and it doesn't have to be included in the libs folder in the project.
The current documentation (Aug 2021) gives a fairly straightforward answer how to do it:
https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency
In short, if you have put your .aar file somewhere on your local drive, this is how to add it as an implementation file in another app project:
In Android Studio, in your new app project, go to: File > Project Structure > Dependencies.
In: Modules > app > Declared Dependencies, click '+' and select 'Jar Dependency'. (Even though you are trying to use an .aar file, you still select 'Jar Dependency').
In the 'Add Jar/Aar Dependency' popup dialog:
in step 1 enter the path to your .aar file on your local drive, and
in step 2 select 'implementation'.
If everything worked out, your build.gradle(Module) should have a line in the dependencies that looks like:
dependencies {
implementation files('../../../MyFolder/MyLibraryFile.aar')
A few notes:
You can actually just add the dependency manually, by typing it into the build.gradle(Module) dependencies, so you don't actually have to go through the Android Studio dialog outlined above.
you can either use a relative path (as the example above), or an absolute path.
the Android Studio dialog is somewhat limited in that you cannot just browse to your file (in point 3, step 1), but you have to actually enter the path manually.
Probably the most important: Whenever you make a change in the library and assemble a new .aar file, then remember to do the following in your app project that uses the .aar file as a dependency: Clean Project, then Sync Project with Gradle Files, and only then run the app, so that the changes in the library could take effect in your app.
I have been using Phgr's above technique for four years. I have three comments -
First, I don't clean the app project each time I change the library - I just do a Sync Project before building and testing the app.
Second, I changed the File-Settings-Keymap-Main Menu-File-Sync to Alt-S for easy of syncing - I hate wasting time using the mouse for selecting the Sync icon.
Third, I have an implementation line in the app's build module file for each app variant such as the following -
debugImplementation files('c:/Android Studio Projects/PciLibrary/app/build/outputs/aar/PciLibrary-debug.aar')
releaseImplementation files('c:/Android Studio Projects/PciLibrary/app/build/outputs/aar/PciLibrary-release.aar')
All of this is working fine with Android Studio 4.2.2 and sdk version 30.
Hope this helps others with this problem
I receive the "error :the package com.badlogic.gdx does not exist" when you try to start the project.
In my project there is a libGDX. Classes see it and objects from this libGDX are used. But when trying to compile an application, this error occurs.
At the same time, there are no errors in the classes on which this error is made.
"Sync Project with Gradle Files" and "Invalidate caches/restart" did not help.
Someone came across similar? How to fix this?
enter image description here
Go to libs folder, right-click on libraries you want then select: Add as library
The reason is, you should add the external libraries on Build.Gradle if the online source from maven-jcenter or etc is not available and you have to add them manually.
After doing this, try rebuild the project then you're good to go.
Or in code: (in not added cases by IDE)
implementation fileTree(dir: 'libs', include: ['*.jar'])
Replace the name instead of * in above code.
How to add a jar in External Libraries in android studio
And: How to add a jar in External Libraries in android studio
Creating a standalone library application is a common task in Eclipse + ADT.
I thought that this should be a simple task in Android Studio (1.2 or above), but after struggling with this problem for two days, I figured out that google did nothing about this and implementing a standalone library module is not as simple as I thought. So I decided to share my experiences with you.
To create a standalone and reusable library module in Android Studio:
1- Create a new project with no Activity.
2- New project's default module is named app. Right click on module and refactor/rename it to something like 'library'. Close Android Studio.
3- Open file explorer and rename module's folder from app to library.
4- Open .idea folder. There are some XML files there that have references to app folder. Replace app into library in these files.
5- Open module's gradle file (library/build.gradle) and change plugin to com.android.library. Then remove applicationId.
6- Open Android Studio. Rebuild module. If there is no error, we are done here.
7- Open application which is dependent on that module. Open settings.gradle and include library module as below:
include ':library'
project(':library').projectDir = new File('/Path/To/LibraryProject/library')
8- Open application's app module build.gradle file and add this line into dependencies section:
compile project(':library')
9- Rebuild your project. If everything is right, you will see library module in your project. You can edit library module from there and/or its project and more important: Now you have a standalone library module that you can reuse in multiple projects!
I hope google will make this process a lot easier in future releases of Android Studio!
EDIT:
I checked Android Studio version 1.4 and hopefully in this version we can omit steps 3 and 4.
My question might be very simple but I cant find the answer:
For my android project I try to implement https://github.com/iPaulPro/aFileChooser
In the installation instructions we find:
Add aFileChooser to your project as an Android Library Project.
with a link to http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject When i follow the link point 1 is :
1. Make sure that both the project library and the application project
that depends on it are in your workspace.
If one of the projects is missing, import it into your workspace.
How should I do this points ? zip aFileChooser into the main folder, or do i need to right click on app and create new Package or something ?
Note : im using android studio and followed the tutorial in the comments, but android studio dosnt recognize the project as a library. I got the project from github by downloading the zip.
1 - In your project's "main" root directory (where the res, java and AndroidManifest.xml file located) create a new folder:
/libs
2- Paste your library in newly created
/libs
folder. Now just download ZIP from Github, rename library directory to "aFileChooser" and copy it.
3 - In app/build.gradle add your library project as an dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(":aFileChooser")
}
To resolve this problem:
Download and unzip the library
In your project go to file-> new -> import module -> click on the small button at the right and choose the library that you are downloaded ->ok
NOTE: INTO 'aFileChooser-master' THERE ARE 2 FOLDERS: aFileChooser AND aFileChooserExample. YOU SHOULD SELECT aFileChooser
Now go to file -> project structure -> select the tab 'app' at the left ->select the tab 'dependencies' -> click on the plus button -> choose module dependencies -> select the library ->ok
Finish. Now you can use the classes of the library in your project.
I recently downloaded Android Studio to develop Android applications (I'm coming from Eclipse), and I am having issues using external libraries and/or external JAR files alongside my own project. NOTE: the following tests were conducted on a new application project created from scratch in Android Studio.
Example 1: JAR Import.
Download a fresh copy of the Admobs SDK from Google.
Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.
In the project explorer, right click on the newly added library.jar and click on 'Add as Library'.
Technically at this point everything works, imports work just fine, the layout editor shows a preview of the AdView widget and all that. The only problem is that it doesn't compile successfully.
Log from console:
Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':APITests:compilePaidDebug'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
I tried running gradlew compileDebug --stacktrace, and the problem seems to be that the despite being able to import the classes successfully in both the code & design editor, at compile time, it cannot resolve the imports. Here's the relevant part of the log: (full stacktrace here)
java:6: error: package com.google.ads does not exist
import com.google.ads.AdRequest;
java:7: error: package com.google.ads does not exist
import com.google.ads.AdView;
java:11: error: cannot find symbol
AdView mAdView;
symbol: class AdView
location: class MainActivity
java:22: error: cannot find symbol
mAdView = (AdView)this.findViewById(R.id.adView);
symbol: class AdView
location: class MainActivity
java:23: error: cannot find symbol
mAdView.loadAd(new AdRequest());
symbol: class AdRequest
location: class MainActivity
5 errors
:Test:compileDebug FAILED
But again, the imports work well in the editor, and the dependency is there:
It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine.
I then tried editing the build.gradle file to include the new lib like this:
dependencies {
compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
}
This time, it did compile successfully, but the app now forces closes, as apparently, it cannot find a specific class from the lib in the application package.
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.foo.test-1.apk
Any ideas?
So,
Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1:
Download the library.jar file and copy it to your /libs/ folder inside your application project.
Open the build.gradle file and edit your dependencies to include the new .jar file:
compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
File -> Close Project
Open a command prompt on your project's root location, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
On the command prompt, type gradlew clean, wait till it's done.
Reopen your application project in Android Studio.
Test run your application and it should work succesfully.
You don't need to close the project and go to command line to invoke grade:clean.
Go to Build-> Rebuild Project
Easy way and it works for me. Using Android Studio 0.8.2.
Drag jar file under libs.
Press "Sync Project with Gradle Files" button.
"simple solution is here"
1 .Create a folder named libs under the app directory for that matter any directory within the project..
2 .Copy Paste your Library to libs folder
3.You simply copy the JAR to your libs/ directory and then from inside Android Studio, right click the Jar that shows up under libs/ > Add As Library..
Peace!
Here is how I got it going specifically for the admob sdk jar file:
Drag your jar file into the libs folder.
Right click on the jar file and select Add Library
now the jar file is a library lets add it to the compile path
Open the build.gradle file (note there are two build.gradle files at least, don't use the root one use the one in your project scope).
Find the dependencies section (for me i was trying to the admob -GoogleAdMobAdsSdk jar file)
e.g.
dependencies {
compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.3.1.jar')
}
Last go into settings.gradle and ensure it looks something like this:
include ':yourproject', ':yourproject:libs:GoogleAdMobAdsSdk-6.3.1'
Finally, Go to Build -> Rebuild Project
you export the project from Eclipse and then import the project from Android Studio, this should solve your problem, open a eclipse project without importing it from Android Studio you can cause problems, look at:
(Excuse my language, I speak Spanish.)
http://developer.android.com/intl/es/sdk/installing/migrate.html
I had the problem not able to load jar file in libs folder in Android Studio.
If you have added JAR file in libs folder, then just open build.gradle file and save it without editing anything else. If you have added this line
compile fileTree(dir: 'libs', include: ['*.jar'])
save it and clean the project .. In next build time Android Studio will load the JAR file.
Hope this helps.
I'm using Android Studio 0.5.2. So if your version is lower than mine my answer may not work for you.
3 ways to add a new Jar to your project:
Menu under Files-->Project Structure
Just press 'F4'
under Project navigation, right clink on any java library and a context menu will show then click on 'Open Library Settings'
A Project Structure window will popup.
On the left column click on 'Libraries' then look at the right pane where there is a plus sign '+' and click on it then enter the path to your new library.
Make sure the new library is under the 'project\libs\' folder otherwise you may get a broken link when you save your project source code.
I am currently using Android Studio 1.4.
For importing and adding libraries, I used the following flow ->
1. Press **Alt+Ctr+Shift+S** or Go to **File --> Project** Structure to open up the Project Structure Dialog Box.
2. Click on **Modules** to which you want to link the JAR to and Go to the Dependency Tab.
3. Click on "**+**" Button to pop up Choose Library Dependency.
4. Search/Select the dependency and rebuild the project.
I used the above approach to import support v4 and v13 libraries.
I hope this is helpful and clears up the flow.
I use android studio 0.8.6 and for importing external library in project , paste that library in libs folder and inside build.gradle write path of that library inside dependencies like this
compile files('libs/google-play-services.jar')
In Android Studio (mine is 2.3.1) go to File - Project Structure:
Check the libs version requirements. The lib might be trying to call a method from from a higher Android version. Try adding the apps compatability libs.
When all else fails try switching from a Gradle build to an Eclipse build.
Delete and restructure your folders to the eclipse style and delete everything but your code. Then reload the project with your libs.
Once it runs go back to Gradle, if you wish.