Referencing external library projects in Android Studio (v0.8.9) - android

I have a custom library that needs to be referenced in about 10 projects. Everything works fine in Eclipse but I want to migrate them to Android Studio in order to take advantage of the build system. I've already check
Android studio add external project to build.gradle
How to share a single library source across multiple projects
and some other links and docs in Gradleware but I don't get to make my projects compile.
My file structure is like:
+projects
|+libraryProject
|+---workspace
|+------projectSrc
|
|+project0
|+---workspace
|+------projectSrc0
|
|+project1
|+---workspace
|+------projectSrc1
.
.
|+projectN
|+---workspace
|+------projectSrcN
Each project contains docs, design files, etc... and therefore is no convenient to have the src in the same root folder.
In my "project1", settings.gradle file I've used:
include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc')
In my build.gradle file, dependencies section I add:
dependencies {
compile project(':..:..:..:libraryProject:workspace:projectSrc')
....
}
But I get an error saying that the project wasn't found.
I've also used the following approach:
def customLib = file('../../../libraryProject/workspace/projectSrc')
settings.createProjectDescriptor(rootProjectDescriptor, 'module-custom-lib', customLib)
include ':module-custom-lib'
Then in the build.gradle
dependencies {
compile project(':module-custom-lib')
....
}
Please help. Thanks for your time.
EDIT: I've managed to get it working by adding the module app as a suffix in the project dir.:
include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc/app')
Now the library members resolve perfectly, but the project's 'R' resource class can not be resolved. So if I do:
setContentView(R.layout.main);
it fails to find that resource. Cleaning and rebuilding the project doesn't fixes the issue.
EDIT 2: Apparently, there was a problem with the manifest merging 'cause the problem was fixed once I adjusted the minSdk and targetSdk of both projects. The previous solution work perfectly.

Your first attempt has a simpler syntax, and should work. Your settings.gradle file looks okay:
include ':module-custom-lib'
project(':module-custom-lib').projectDir = new File(settingsDir, '../../../libraryProject/workspace/projectSrc')
though you should be cautious about including a relative path there -- Android Studio may not use the same working directory as a build you do from the command line, so referencing things relative to rootDir may help alleviate problems.
In the dependencies block of a module's build file, you need to reference a module the same way you do in settings.gradle, so you'd use something like this:
dependencies {
compile project(':module-custom-lib')
}
In dependencies blocks, you're linking to projects using Gradle's project namespace, and since you've already explicitly set up the root directory of the library module, you don't need to try to locate it in the filesystem here. That :..:..:.. syntax wouldn't work anyway to reference a module that's outside of your project's root directory, as this one is.

You should consider bundling "releases" of your library to a Maven repository (possibly your local Maven repo). This way you can reference your library from any other project that needs to use it just like you do with 3rd party libraries.

Related

Linking an Android library that is on local machine

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

Libraries with custom attributes conflicting

I am trying to use Shimmer library and a Slider View library together.
Unfortunately, there's a custom attributes in both libraries that is conflicting: I am getting a duplicate attribute found error.
I have read that this is a bug and there are no solutions as of now and we cant create custom attributes within the context of each library. But is there any other way around it apart from me implementing the slider view myself?
Android resource compilation failed
app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4619: error: duplicate value for resource 'attr/shape' with config ''.
\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:4619: error: resource previously defined here.
\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml: error: file failed to compile.````
I think there's an workaround for that issue. However, I'm not sure if you can do that due to licensing.
I didn't test it. Not sure if it will work. But I think you can try. If that does not work, let me know and I delete the answer.
To avoid that error, you can resort to the fact that you have access to the source code of the AndroidImageSlider. This way, instead of adding that library as an AAR file, you can import the source code as a module.
This way, you can rename the attr/shape and it will be re-compiled as part of your project.
1) Remove the AndroidImageSlider from your project (from build.gradle etc)
2) Then, create a new module called "library" (File -> Project Structure -> Modules -> + -> Android Library)
3) This will create a new folder to your project <ProjectPath>/library
4) Download the Library Source (using git or as a zip file) and replace the content of the library folder with content from library folder that you downloaded (you can ignore all other folders... You can replace the files of same name).
5) Remove file library/gradle-mvn-push.gradle and remove following line from: library/build.gradle:
apply from: './gradle-mvn-push.gradle'
6) Add following lines to your app/build.gradle
dependencies {
...
implementation project(":library")
...
}
7) Sync and Rebuild your project.
8) Open the file ./library/src/main/res/values/attrs.xml and rename the atribute shape (customShape for example).
9) Try to build again. Some errors may happen since you renamed that attribute. Fix them.
10) You can add the view to your layout as follows:
This will allow you to compile your project. The downside is that you are responsible to fix/update the library by yourself. On the other hand, you have control over the libraries build.gradle and then, you can update the build tools version, support library version etc.
Anyway, this may help you

Libraries in Android Studio - how not to produce a full copy

I am in the process of migrating from Eclipse to Studio.
I have one Eclipse project which is a personal library of various bits and pieces that I use in other projects. I have successfully imported that library project into Studio.
When I import a project that uses that library, the importer seems to copy the whole library project into the app project, source and all.
This, obviously, is not what is required. I've tried many things and wasted a LOT of time trying to overcome this. So...
Q: Within Studio / Gradle, how do I replicate the Eclipse functionality Project / Properties / Android / Add... to just reference the library project and not produce a stand-alone copy?
You can specify in your settings.gradle a relative path using the project().projectDir property, something like this:
include ':lib'
project(':lib').projectDir = new File('xxxxxx') // Relative file path from your settings.gradle
In this way you can use the lib module inside a project without cloning the code.
Example:
Project0
|--library
|----build.gradle
|build.gradle
|settings.gradle
Project1
|--app
|----build.gradle
|build.gradle
|settings.gradle
in Project1/settings.gradle
include ':library'
project(':library').projectDir = new File(settingsDir, '../Project0/library')
in Project1/app/build.gradle
dependencies {
compile project(':library')
}

How to Import Module without creating copy in Android Studio

I am using Android Studio 1.3.1 and trying to add library module to an existing android application. The library module is available in a git repository. I am able to import the module, but it creates a copy inside the existing app. Hence I am not able to pull the updates in the module.
I am sure there is a way to import external libraries from an existing Android project in studio.
I found the below stackoverflow posts related to my doubt -
How to import a Module on Android Studio 0.5.1?
Android Studio 0.8.1 Creating Modules without copying files?
Both seem not to work for me. I also found couple of comments from other users saying it is also not working for them in the latest version of studio.
Here are the things that I tried
// in settings.gradle
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
// in build.gradle
compile project(':libraryName')
Also I tried using this this url
Any help is appreciated. Thanks
You were on the right track. Just make sure your library is inside one folder then you can direct the library path like this..
Inside settings.gradle
include ':libraryName'
project (":libraryName").projectDir = new File("../FolderName/libraryName")
if your library is inside 2 folders then direct the path like this...
include ':libraryName'
project (":libraryName").projectDir = new File("../../FolderName/libraryName")
This allowed me to use the library without making a duplicate.
Is your path relative or absolute there?
Try this if you want to reference the other module relative to the current project:
include ':libraryName'
project(':libraryName').projectDir = new File(rootProject.projectDir, '../path/to/library')
Set the projectDir and by setting the rootProject.name it will also show up in your IDEs Project pane:
// in settings.gradle
include ':app'
rootProject.name = "Project name"
include ':libraryName'
project (":libraryName").projectDir = new File("../path/to/library")
// in build.gradle
compile project(':libraryName')
Have you tried creating a folder 'libs' under your project and copying the .jar file to that folder and try compile the libs folder? That seems to normally work for me. I think it was the first solution on this question
How to import a Module on Android Studio 0.5.1?

Android studio add external project to build.gradle

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')

Categories

Resources