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
Related
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.
For 4 days now I have been trying to get this TwoWayView Library to work in Eclipse. I am not sure if the issue is me or Eclipse. Since I updated Eclipse two weeks ago it has been nothing but a nightmare. Anyways, I downloaded the TwoWayView Project from GitHub, Imported into Eclipse as existing Code, Set project as Library. That should be it for the TwoWayView Lib.
In my project I Right Click> Properties> Android> Add> (Select TwoWayView)> Apply>.
I have Right Click> Properties> Project> Add TwoWayView>.
So far, nothing. I have read in 3 blogs about how simple it should be by just importing the project and using it. Even Lucas' instructions are 3 steps and for the life of me can't figure out how in the world it could sound so simple yet I can't get it to work at all. Has anyone got this to work and if so, HOW?
you could try using gradle, simply add this line to your dependencies in build.gradle: compile 'org.lucasr.twowayview:twowayview:0.1.1'
Just today I had the same problem as you, but I'm working with Android Studio, but I hope you serve.
It turns out that the content of the project is not the library, are just some of the folders that are within twoway-view-master so you have to go one by one and only necessary. Only you need to include the following folders:
core
layouts
Here are the steps to follow:
Download the library project TwoWayView of Lucasr
Create, in your Android project you're working in a new folder. In C:\Users\your-user\AndroidStudioProjects\your-name-of-application\ create a new folder called libraries.
Within the libraries folder created, paste the library project TwoWayView you just downloaded (twoway-view-master folder).
In the project tree of our IDE us that new folder (libraries) will appear. Access the settings.gradle of libraries and add the following lines of code:
include ': app'
include ': libraries: twoway-view-master: core'
include ': libraries: twoway-view-master: layouts'
Now go to the libraries/ twoway-view-master/core and access build.gradle folder. Change this line of code:
apply from: "${rootDir}/gradle/scripts/gradle-mvn-push.gradle"
By the following:
apply from: "${rootDir}/libraries/twoway-view-master/gradle/scripts/gradle-mvn-push.gradle"
Perform step 5 but this time with the build.gradle file layouts folder.
In the build.gradle file layouts folder, you also have to change the line:
compile project (':core')
By the following:
compile project (':libraries:twoway-view-master:core')
Finally you just have to access the build.gradle of your project and add the following lines in the section dependencies:
compile project (':libraries:twoway-view-master:core')
compile project (':libraries:twoway-view-master:layouts')
If you do not understand very well what to do, this question is the solution that gives the user prudhvi.
https://stackoverflow.com/a/32402832/6129931
EDIT: However, since Google launched the Android version L, exist the called RecyclerView, there that can do everything you do with TwoWayView, i.e. can create lists and grids in horizontally and vertically. I recommend you reports about it rather than use separate libraries because Google is starting to demand their use.
Here you can get information about RecyclerView use in horizontal and vertical:
https://stackoverflow.com/questions/36355506/learn-step-by-step-to-use-the-recyclerview-in-android - StackOverflow
Hope this can help you.
We currently use Eclipse for Android development , and our projects are synchronized to an SVN .
Current structure :
Android Application 1
Android Application 2
Android library 1
Android library 2
It is therefore independent projects on SVN.
Details :
Android Android app 1 need library 1
Android Android app 2 need library 1 and 2 , etc ...
Until then, it is quite normal.
The challenge is to move to the project structure in Android Studio / IntelliJ , while ensuring compatibility with eclipse . So changing NOTHING in the current structure of SVN .
And I just can't handle how to do this with the IntelliJ structure (projects, modules, ... ) .
In addition there appears to have a problem with Android Studio, it is impossible for me to IMPORT module , I can create one, but not import one...
And that's not all , in "Open Module settings" , only Android SDK Appears for EACH project. So I can not handle modules ...
I tried to import each project one by one with a checkout from Subversion, it works, but how to link projects to each other then ? I end up with a project structure as in eclipse, and I guess that is not good.
In Android Studio, a module is dependent on a project (right ?), but I do not want a library dependent of any project.
Please, do not hesitate to ask any information. I'm sure that I didn't say everything, because I try that from 2 days now, so I've try many many things... I just can't get it.
What's wrong ?
Thanks a lot
Best Regards
Firstly you should generate a gradle build file for each of your projects. You can do this in Eclipse. Go to File > Export > Android Gradle Files.
At the end you should have this architecture on you project :
root
build.gradle
settings.gradle
AndroidProj1
build.gralde
AndroidProj2
build.gradle
AndroidLib1
build.gralde
AndroidLib2
build.gradle
Make sure that all your projects are refereced in your settings.gradle. You should have these lines:
:AndroidProj1
:AndroidProj2
:AndroidLib1
:AndoridLib2
Then, in build.gradle files of your projects, you can make references of other projects. If AndroidProj1 depends on AndroidLib1, then you should have these lines in build.gradle of AndroidProj1:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':AndroidLib1')
}
When Eclipse generate gradle files, it keeps the old directory structure so you can keep using Eclipse.
In Android Studio, you should import only the root project. It will find the underlying projects.
Then you can try to build your projects.
For each project within Eclipse, you should go to File > Export > Android Gradle Files and then use these files to import into Android Studio.
However, I think once imported into Android Studio, it is then not compatible with Eclipse. I have had many issues trying to reference libraries for my apps in Android Studio and I eventually gave up. Android Studio is still in a very early release and therefore there are still lots of bugs and issues within Android Studio. Therefore, I wouldn't recommend moving to Android studio as yet and stick to Eclipse for the time being until a stable version is released.
I have a setup of android project on eclipse and I want to migrate to Android Studio. So, I have android-support-v4.jar that I use for my main project and my Facebook lib-project.
I guess I have to exclude lib-projects as a folder in my main module (lets call the main module Jack). Jack has dependancy on the facebook lib-project.
How should I define the android-support-v4.jar as a separate library and use it in both projects? Or should I just use directly the jar files and leave them in both Jack's libs folder and Facebook libs folder?
If the first option should be done, will ant clean release still work (with the build.xml android generated file)?
In Android Studio, builds are done with Gradle now. Gradle is different. With gradle, you tell your project which jar's you need, and it will connect to a server and download them if it doesn't already have them when you compile your apk.
A few things to note:
When you install Android Studio, it has it's own Android sdk directory. You have to download everything from the sdk downloader (from inside the Android Studio App) again. Don't bother trying to switch the sdk download path to your current one. You will only encounter bugs (Or at least I did).
So your question is worded very confusingly. It sounds like you have a main module, and then you have a library module, and the library module uses the support library.
You'll need to set it up so the main module has a dependency on your library module. From there, you'll need to go into your library module's gradle file and tell it that you want to include the android support library
dependencies {
compile 'com.android.support:appcompat-v7:19.0.0'
}
The support library is a little weird in gradle. Gradle normally would download the dependencies you need. However, android studio requires that you have the support library installed through their sdk downloader (top-right group of icons in android studio. The download icon).
After you get all your dependencies entered into your gradle file, you'll then need to go to Tools -> Android -> Sync gradle files with project. From there compile errors should go away, and you should be able to run the project.
Best of luck. By the way, Here is the documentation on Gradle on the android website. I find myself having to go to it A LOT, especially when I made the switch from Eclipse to Android Studio. This + Various tutorials I found as needed via google. http://tools.android.com/tech-docs/new-build-system/user-guide
EDIT: This link might also be helpful. Google has some steps for switching from eclipse to Android Studio: http://developer.android.com/sdk/installing/migrate.html That with some of the stuff above may prove helpful.
While I haven't tried this myself, I suggest you use the recommended migration steps provided by Google in this article.
Before you do that, though, make sure that you either:
Check that both support libraries on the main project and dependencies have the same version (Eclipse will complain about it during build time, and will likely cause problems during conversion to Gradle script.); OR,
Uncheck the "Android Private Libraries" entry on the Order and Export tab of the dependency project's build settings.
If the migration process described doesn't work smoothly for you, you can always call the Ant build script from within the Gradle script, as described here.
The Android Studio uses only Gradle, but you can export Android ant project from eclipse.
To do that go to File -> import project usually next, next, next... works.
If not go to project setting Shift + Ctrl + Alt + S and under modules -> PROJECT_NAME -> Dependencies you can add your support library.
To add a Facebook library you must add it as another module to your project.
If you still want to use Ant there is another option: Use Intellij IDEA which support Ant.
I have a sample project, with the following setup:
/root
+ Pure Java Lib
+ Android Test Lib
+ Android Test Project
Where the 'Test Project' is dependent on the 'Test Lib', and the last depends on the 'Pure Java Lib'
Compiling the project and launching this setup works just fine.
I'm now thinking about importing my previous Eclipse workspace and work with Android studio, the problem is the project setup is different, and I would like to keep it this way.
for example if to use the previous example:
/root
+ Android Test Lib
+ Android Test Project
/Some Other folder (another repository for example)
+ Pure Java Lib
I've tried many configurations, but I didn't find a way to reference a project outside the scope of the parent folder ('root' in the example case).
In many platforms/modules you can use the '..' to move up in the folders but this didn't work for me, perhaps I've used it wrong.
Does anyone know how this can be achieved with Gradle?
UPDATE
I'll try to be more generic:
/C:/
/Project A
+ Module 1 - Pure Java
+ Module 2 - Android Test Lib
+ Module 3 - Android Test Project
/Project B
+ Module 1 - Pure Java
+ Module 2 - Pure Java
+ Module 3 - Pure Java
I would like to use Module 1 of project B, in project A.
UPDATE: 09-03-19
I saw this now and I must update... after almost 6 years, today I am wiser, and I can definitely say the problem was me misunderstanding the concept of "Source of truth".
While having one ref to a library is a nice to have concept.. and may seem like the a "Source of truth", the REAL "Source of truth" would be the version of the code each project is using of that library, cause the library by itself has versions.. many versions an the "Source of truth" is relative to the project which is using the library.
The correct way would be to use what most developers do not like, and that is git submodules, and yes duplicate the sources in each project cause most chances each project uses a different version of the code.
You would need however to aim for all of your projects to use the latest and greatest version of all your libraries.. which is a challenge by itself
The reason this is the right way to develop a project with library sources is that this scales... you can have hundreds of project each with its own library configuration.
Assuming that Some Other Folder is a gradle project you could add something like the following to your settings.gradle file:
include ':module1'
project(':module1').projectDir = new File(settingsDir, '../Project B/Module 1')
You have to put in your file settings.gradle this lines:
include ':module2'
project(':module2').projectDir = new File(settingsDir, '../Project 2/Module2')
Then you have to add in your builde.gradle (Module: app) in the dependencies tree, this line:
implementation project(':module2')
or go into the Project Structure > app > Dependencies, click on Add, choose 3 Module Dependencies and select your module
With Gradle 1.10 (don't know what other versions this will be valid for) this is what I came up with based on a response given here http://forums.gradle.org/gradle/topics/reference_external_project_as_dependancy
I have an api library project, a common library project and the main app project. Each is a stand-alone development project and the two libraries are meant to be shared between multiple apps.
In settings.gradle for the common project:
def apiLibDir = file('../android-api/android-api-lib')
def rootProjectDescriptor = settings.rootProject
settings.createProjectDescriptor(rootProjectDescriptor, 'android-api-lib', apiLibDir)
include ':android-api-lib'
Then in the main app project settings.gradle:
def apiLibDir = file('../android-libs/android-api/android-api-lib')
def rootProjectDescriptor = settings.rootProject
settings.createProjectDescriptor(rootProjectDescriptor, 'android-api-lib', apiLibDir)
include ':android-api-lib'
def commonLibDir = file('../android-libs/android-common/android-common-lib')
settings.createProjectDescriptor(rootProjectDescriptor, 'android-common-lib', commonLibDir)
include ':android-common-lib'
In each of the respective build.gradle files you just reference them by the name you gave them in the settings.createProjectDescriptor like you would any other project dependancy:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':android-api-lib')
compile project(':android-common-lib')
}
This seems to work. It didn't even throw an error for multiple DEX files defining the api library, I suspect because it was all part of the same build process and Gradle was smart enough to figure it all out.
Right click on project - Select "Open Module Settings" - Select "Modules" in left pane - Click on "+" symbol on top - Choose "Import Module".
After importing Module. You need to add it as a dependency for your current project.
Keep "Modules" Selected in left pane and click on your project - Now Go in dependencies tab and click on "+" symbol that is located at bottom - Choose third option "Module Dependencies" and if you have imported your project correctly, it will show you the all available module that can be added as a dependency to your current project.
I re-ask this question in a way which entails the original posters' intentions at How do we reference custom Android and Java Libraries living outside the directory of our Main Android Project?
There I answer my own question. At core my answer uses #Ethan's (the author of the chosen answer in the current thread) gradle coding insight. But my answer also navigates some other gotchas and provides a detailed step by step example.
As Ethan said, if you add this to your settings.gradle, it will add an external project to Android Studio (in this example, it's in the parent folder):
project(':../ProjectB/:module1')
Then, to add it as a dependency of one of your projects, just add it in the build.gradle of that project as another dependency like this (you can also do it graphically as in here):
compile project(':../ProjectB/:module1')