Include non-static library project in Android Studio? - android

What's the best way to include a non-static library project in Android Studio?
I've developed a library that is used by multiple projects and it is currently under active development. In Eclipse I can continue developing and include it in multiple projects just by being in the same workspace, is there an equivalent way to do it in AS?
When I make changes to the library source I don't want to have to keep copying the source manually into modules of all the dependent projects, and putting it on Maven is not an option. The library and all dependent projects are hosted in SVN.
Any ideas or suggestions? Thanks!

I'm not sure if this is the "right" or best way, but it works for me. I don't use SVN, so I have no idea how that would affect this methodology.
Where you specify sourcesets, you can specify additional directories. Suppose my library project's root directory is in the same directory as my current working project. I would change this line as shown under the sourcesets section:
java.srcDirs = ['src', '../myLibraryProject/src']
And you can do the same for res.srcDirs and so on as applicable.
This results in a funny looking directory tree, where it shows the additional source directory as a sibling to the project's root directory, and the project's own src directory is repeated as a sibling. I think this might be a bug, but it does allow me to go into the library project's source and edit it as if it is part of the same project.

I was pointed in the direction of this question, which solved my issue perfectly - Add local Library Project as a dependency to multiple Projects in Android Studio

Related

Why so many metadata files in Android studio?

I created a new project in Android studio and got many files generated, where as my actual code is found in just one folder - src.
Why the so complicated structure? Please explain the motivation of putting meta-files at the root of the project instead of some inner folder named gradle.
Android build system consists of an Android plugin for Gradle. Gradle
is an advanced build toolkit that manages dependencies and allows you
to define custom build logic. Android Studio uses a Gradle wrapper to
fully integrate the Android plugin for Gradle.
Android Studio projects contain a top-level build file and a build file for each module. The build files are called build.gradle, and they are plain text files that use Groovy syntax to configure the build with the elements provided by the Android plugin for Gradle.
Gradle is an automated build toolkit that allows the way in which projects are built to be configured and managed through a set of build configuration files. This includes defining how a project is to be built, what dependencies need to be fulfilled for the project to build successfully and what the end result (or results) of the build process should be. The strength of Gradle lies in the flexibility that it provides to the developer.
For more info you may visit
Gradle Tutorial
Android Application Modules
First of all if you don't want to see those metadata... you can change it(see Image)..
gradle is required to compile your project. for example: In gradle file we specify minsdk version,maxsdk version and dependencies etc
To Know more about gradle go to http://gradle.org/the-new-gradle-android-build-system/
Why the so complicated structure?
IMHO the structure you are referring to is pretty straightforward but your assumption that all those meta-files are related with gradle is wrong.
Meta-files related with your android application are located inside the "app" sub-folder. You have some gradle files there because those are for the purpose of building that specific module.
As pointed out before in a previous answer the best resource to understand the file tree structure for this part is here.
You also have some metadata generated by the IDE (.idea sub-folder):
IntelliJ IDEA stores the configuration data for projects and their
components in plain text XML files making it easy to manage and share
project configuration data with others.
And .iml files:
A module is a discrete unit of functionality which you can compile,
run, test and debug independently.
Modules contain everything that is required for their specific tasks:
source code, build scripts, unit tests, deployment descriptors, and
documentation. However, modules exist and are functional only in the
context of a project.
Configuration information for a module is stored in a .iml module
file. By default, such a file is located in the module's content root
folder.
More info about can be found here.
Please explain the motivation of putting meta-files at the root of the
project instead of some inner folder named gradle.
As mentioned before in some previous answers some metadata is related with the configuration of your project itself and some is module-specific. One example is the build.gradle files. The global file has this comment:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
About the motivation I only assume it was for simplicity and to keep the semantics of the project structure. Other possibility is that it was just by convention.

Android Studio and Library Projects [duplicate]

I'm creating projects with dependencies in Android Studio. I know how to link projects by adding modules.
But I realized that 'importing modules' create a copy of the libProject inside the project.
Is there a way to prevent that ? Like an 'external module' ?
Since i'm in charge of both project, I want to be able to push changes to the libProject Repo, without having to copy paste files between folders.
Thanks
Yes, you can do it. The module needs to have a Gradle build file set up for it. If it's got that, then in the project you're linking to it, add this to the settings.gradle file at the project root:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
where the path you specify in the second line is the path to the directory containing the library's build.gradle file. The path can be relative or absolute.
The solution:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
was not working for me. After couple of wasted hours I figured out the issue. There are two build.gradle files, one for project and one for library name. If the library is in the folder '\MyLib' then there will be a build.gradle in '\MyLib' and another at '\MyLib\app'. You have to point to the '\MyLib\app' and not '\Mylib'.
Hopefully this saves some time for others.
If you have, like myself, have multiple modules (I only realised today that copies were included, I thought that the project included links to the source.)
You can have multiple modules/projects along the lines of :-
include ':app', ':sqlwords', ':dbindex', ':dbcolumn', ':dbtable', ':dbdatabase', ':displayhelp', ':pickdate'
project(':sqlwords').projectDir= new File('d:/Android_Applications/Modules/sqlwords')
project(':dbcolumn').projectDir= new File('d:/Android_Applications/Modules/dbcolumn')
project(':dbtable').projectDir= new File('d:/Android_Applications/Modules/dbtable')
project(':dbindex').projectDir= new File('d:/Android_Applications/Modules/dbindex')
project(':dbdatabase').projectDir= new File('d:/Android_Applications/Modules/dbdatabase')
project(':displayhelp').projectDir= new File('d:/Android_Applications/Modules/displayhelp')
project(':pickdate').projectDir= new File('d:/Android_Applications/PickDateShowCase/pickdate')
You can also use android { sourceSets{ main.java.srcDirs += '../../../library/src' }} in your app build.gradle . Not sure about supporting all android resources, for pure java library works well.

How to force Android Studio to use relative paths?

Every project created in Android Studio 1.0.2 contains several files that reference the project's own absolute path. I can't even move my own project directory if I want to, let alone share the project with other developers.
Excluding files in app/build, these are the files containing absolute paths:
.idea/workspace.xml
.idea/modules.xml
.idea/libraries/support_v4_21_0_3.xml
.idea/libraries/appcompat_v7_21_0_3.xml
.idea/gradle.xml
.gradle/2.2.1/taskArtifacts/taskArtifacts.bin
.gradle/2.2.1/taskArtifacts/fileSnapshots.bin
app/app.iml
How do I force Android Studio to use relative paths for everything?
Edit: By experimenting with vanilla IDEA, I've narrowed down the origin of these absolute paths to a specific type of module, which Android Studio apparently always uses without offering you a choice. But I'm still no closer to understanding how to eradicate them.
In IDEA 14.0.2, if you create an empty project or an Android project with an "Application Module", the project does not contain any absolute paths. If you create a project with a "Gradle: Android Module", then it contains absolute paths in the same files as an Android Studio project.
Edit #2: Created IDEA-134587
In general, don't consider any of the .iml files or the contents of the .idea folder to be part of the project, and don't share any of those files, don't check them into source control, and don't move them with the project. Think of them as cache files.
The Gradle files are the source of truth, so if you're having troubles with absolute paths, close the project, delete the non-shareable files, and re-import it from the Gradle build scripts.
I ran into the exact same problem, but the solution suggested above contradicts JetBrains' advice as well as this answer. Also, my co-worker working from the same source code (with unexpanded paths) and Android Studio version wasn't having the problem, so I kept banging my head against the wall.
We eventually solved the problem when we realized that many of the paths I used included symlinks. In my case, I had a symlink set up for ~/work so that it pointed to /some/drive/with/space. Within Android Studio all of my source was referred to from ~/work/source rather than /some/drive/with/space/source. When I changed everything so that Android Studio referred to things with their actual paths, the $PROJECT_DIR$ and $MODULE_DIR$ variables magically started working and my .iml files were no longer getting corrupted. YMMV.
TL;DR: Don't use symlinks in your project paths!
Also, be sure to not keep the files within .gradle as part of your shared project.
Also, one set of files that you might want to share though are your files under .idea/copyright though as that allows you to have shared copyright settings.
So a possible .gitignore file might be:
.gradle
.idea
!.idea/copyright/[YourCopyrightFile].xml
!.idea/copyright/profile_settings.xml
*.iml
build
local.properties
Hmm. I just don't see the same absolute paths in those files, I only see references to MODULE_DIR and PROJECT_DIR, such as:
./app/app.iml: <excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
or:
./.idea/workspace.xml: <entry file="file://$PROJECT_DIR$/.idea/libraries/appcompat_v7_21_0_2.xml">
I wonder if this is Linux issue only, or something in your settings?

Importing android gradle project into eclipse

I need to import this library project into my eclipse. While i am trying to use this project it have some error. Can you help me on this. https://github.com/daimajia/AndroidImageSlider
That project is created for use with Gradle for Android. Eclipse does not support that yet. It is also distributed as an AAR, which Eclipse does not support. There are recipes for converting AARs into Eclipse-friendly Android library projects that you can try. Otherwise, you will need to reorganize the project code yourself to support the classic Eclipse-style project structure. Mostly, that will involve moving the contents of library/src/main/ into a regular Eclipse Android library project:
The res/ directory and AndroidManifest.xml file would go in the library project root directory
The java/ directory would be renamed src/ and also go in the library project directory
However, you will have to repeat this process each and every time the library's author updates the library, at least for those updates that you are interested in.
You may wish to see if there is an alternative library that meets your needs but is better packaged for use with Eclipse.
Basically there are some changes to be done to eclipse project before importing it to eclipse like src folder.
The project you posted may contains many error since it have two more android project dependency.
I tired to convert above project with its dependency :
Checkout Complete Source Code
There is one more project lib u need to add LIB

How to include external libs in Android source controlled project?

How should external libraries be included into Android projects?
I see this documentation from Google:
http://developer.android.com/tools/support-library/setup.html#libs-with-res
...which says they should be kept outside the source tree for the project, and referenced as dependencies.
The guide for Facebook libraries says the same thing:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/
What about when the project is going into source control, and will be worked on by multiple developers? Is it possible to be sure other developers will have the correct versions of libraries if they're not included in source control?
It seems as though it might be better to check in the whole tree of these external libraries under say an "external" folder in the project and then reference them as libraries from there? The above links don't say this is wrong, but is there any reason not to do that?
I could not find anything against this approach, but maybe my search skills are off.
Thanks!
You have basically tree options (referring to git):
Putting the source or binaries into your git repository.
You can create/clone extra repositories and link these as submodule into your main repository.
Use gradle/android-studio to maintain remote binary dependencies.
In my opinion, option 3. is the best. It speeds up build time and reduces the date saved in your internal repository. Referencing most open source projects, googles libraries and even the Facebook API is just a one liner in your build.gradle file.
For internal libraries or anything not uploaded to some maven repository, you can create a local maven repository and link that.
And in the end, you have the option 2. to create a library submodule within git and gradle to handle it efficiently.
If you want to stick to eclipse + ant, try 2. first.
At least ant will work out of the box for building all things.
Setting up eclipse is a bit more difficult but can be done.
Option 1. is easy to implement, but It might get messy at some point.
Copy jar file in android project libs forlder and right click on jar file and click on bulid path-> add to build path.
If you want to add jar file then copy your jar file and put in to libs folder, and if you want to add external library then import your library project go to project properties and select android tab and add external library with add button.

Categories

Resources