While I develop an Android App, I have a library which I have created as separate Android Studio project and can use it by inserting it into new projects. I insert the library by choosing 'File|New|Import Module...' option.
The thing is that after the import, Gradle creates a hard copy of my library. If I change the library code in main external project, the code inside the project which is using the library won't get updated.
How can I have a library and share it among many project? I need to change the library in one single place and then all other projects which are using it get the update.
I found this post also which has no answer:
How to update imported modules with code modification from the their external library project in Gradle/Android Studio
OK I found the answer by myself:
You must not add the external library as an existing module. It will make a copy of it under your project folder.
What you have to do is:
Delete the library folder in your current project. (You can also delete the ./idea/modules/[module_name] folder.)
Open the setting.gradle file and add these:
include ':your_external_library_module_name'
project (':your_external_library_module_name').projectDir = new File('../path/to/your/external/library')
include ':perhaps_second_external_library'
project (':perhaps_second_external_library').projectDir = new File('../path/to/your/second/external/library')
In your build.gradle (:app) file add dependency as:
dependencies {
implementation project(':your_external_library_module_name')
implementation project(':perhaps_second_external_library')
}
Sync the project and you are done.
Related
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
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.
I am trying to import an android project created in eclipse into android studio. This main project also refers to another library project in the same eclipse workspace. The folder structure is like this:
workspace\app1
workspace\lib1
When I import the main project into android studio, the library project is automatically imported as a sub-project (or shall we call it a module) within the main project. That's not what I want. The library project is shared by multiple apps I am maintaining. It should sit in parallel with other app projects in terms of folder structure. I wouldn't want it to go understand any particular app project.
Please help explain how I can make the library project stand its own and refer to the library project from other app projects in android project. I am fairly new to the gradle build system. So detailed instructions would be highly appreciated. Thanks a lot!
Since you are importing in android studio, I assume you are moving to gradle build system. In your case , I think, it would be best to have a separate AS project for library. Use that project to generate a jar file and then import that jar using gradle build script in each project you want to use it.
If you are sharing the same library across multiple, you can configure them as separate AS project and have "aar" file as output. (In your gradle you put>> apply plugin: 'android-library'). You then define them as dependency in your gradle file. (either through maven repo or putting it in your libs directory and defining it in your dependencies section). You can find more info at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-projects
You can use version control tool such as git to synchronize the library project in every main project. But you need to use a version control tool to manage all your projects first.
Read here on "AS Modules"
review 57.1.2 on "common behavior"
Take a look at a project built on AS pattern of modules as libs used in eclipse (gradle.settings) pictured
all the includes are modules
maybe you can rethink the idea of how to get AS to work for you in reusing code....
I am trying to add external library to android dependancies using eclipse adt. I have a couple of libraries like pull to refresh etc in there. But everytime I add a new library jar file it shows outside of the android dependancies library folder. How do I make it such that it is coupled with the other libraries as a part of android dependancies library. Here's what I did so far:
Project> Properties> java build path > libraries> add jars ( also tried adding external jars), but it always showed my new jar file independantly, i want it to be a part of android dependancies). any clue?
Here's a screen shot, I would like to add it as a part of android dependencies:
I still do not get why that is important to you.
However, when you reference a library project: rightclick project->properties->Android->Add then the jar goes into Android Dependencies.
If you add the jar directly to your project under libs or using build path, then it goes under Android Private Libraries.
Both methods works but referencing a library project can have benefits if you want to alter the code of the library during development.
In my case I had the following in my project.properties file
target=android-18
proguard.config=proguard.cfg
android.library=false
android.library.reference.1=..\\com_facebook_android
android.library.reference.2=../actionbarsherlock
After I changed the last line to
android.library.reference.2=..\\actionbarsherlock
actionbarsherlock.jar appeared in the Android Dependancies and I could run my app without the java.lang.noclassdeffounderror exception
I want to use library from openCV named "opencv library -2.4.3" , I install all what it needs , and now openCV tutorials work. So I need add the same openCV library to my project for use.
QUESTION
How to add opencv "library -2.4.3" from another project to mine's folder named Android Dependencies
Thanks in advance
Create a libs folder (/libs) in your project's root folder.
Copy the wanted jar to it.
It will be added to Android dependencies.
You have to click on on the project properties (Right Click -> Properties) and add it as dependency there.
I just did this with a project, using eclipse 4.2.1. As with the other answerer, all I had to do was create a folder called "libs/" in my project, and it auto-updated the Android Dependencies list. Are you copying and pasting inside of eclipse?
I moved the file to that folder using the file system, outside of eclipse.
This link describes how the libs folder gets added to Android Dependencies:
http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
Right click on project
Select Properties
Click on Android on left menu
Click on Add
Please select a Library Project
The jar will go into Android Dependencies.
If you add the jar directly to your project under libs or using build path, then it goes under Android Private Libraries.
Both methods works but referencing a library project can have benefits if you want to alter the code of the library during development.