1

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.

NLam
  • 537
  • 1
  • 11
  • 25
  • Are you interested in using the aar file directly like this question? https://stackoverflow.com/q/16682847/8040697 – Emad Razavi May 14 '20 at 20:53

2 Answers2

0

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:

  1. In Android Studio, in your new app project, go to: File > Project Structure > Dependencies.
  2. 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').
  3. 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.
ptgr
  • 1
  • 1
0

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

Earl Allen
  • 29
  • 5