5

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

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • I'm on a bus right now, but are you sure these absolue paths really prevent you from moving a project? I'm almost certain if you copy that project directory to a new directory and either open the project or re-import it and do a clean build it will update all of those files with new absolute paths that reflect where the copy of the original project is. I'm almost certain I've copied and opened my projects with no issues but like I said I'm on a bus. Although, maybe I should take a look, maybe those copied projects have references to the original because of this. – Mark Jan 07 '15 at 12:43
  • @Mark Yes. If I change the absolute path of the project, then when AS tries to open it, it says, `Cannot load module file '/old/path/to/module.iml'; file '/old/path/to/module.iml' does not exist. Would you like to remove module 'module' from the project?` – Kevin Krumwiede Jan 07 '15 at 18:36
  • @Mark And if I say no, then the module still isn't shown. There's nothing to clean or build. – Kevin Krumwiede Jan 07 '15 at 18:42

4 Answers4

12

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.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • You don't explain why its necessary to copy these files around. Why not rebuild the project from its Gradle files and not worry about what it puts in these files? – Scott Barta Dec 19 '14 at 19:58
  • 1
    Moving files around is a very common thing to do. Common sense and years of experience with other build tools dictate that projects should never contain absolute paths. – Kevin Krumwiede Dec 19 '14 at 20:29
  • 2
    I'm not defending Android Studio's current behavior, merely explaining it. Android Studio doesn't consider the .iml files or the .idea folder to be part of its "project", and users are discouraged from sharing them or checking them into source control or such. I know it's confusing because by and large IntelliJ *does* consider these to be part of the project, and because it is smarter about the paths, but Android Studio considers Gradle to be the source of truth. The fact that it puts absolute paths in there is likely a bug, but low-priority for this reason. There's no other workaround for it. – Scott Barta Dec 19 '14 at 20:38
  • Hrm. I'm never happy with workarounds, especially when the existence of a workaround means the devs will de-prioritize an annoying issue. I'll accept your answer once I've tried and failed to dive the code and fix it myself. If I succeed, I'll share what I did. – Kevin Krumwiede Dec 20 '14 at 03:00
  • This sucks if you sync the files with Dropbox (or other file sharing) over multiple computers. – Alex Burdusel Jun 30 '15 at 17:02
6

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!

Community
  • 1
  • 1
mes5k
  • 855
  • 10
  • 15
0

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

Mark
  • 1,124
  • 1
  • 14
  • 21
0

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?

Mark
  • 1,124
  • 1
  • 14
  • 21
  • I do not see absolute paths on Linux either, only those like I saw last night on Mac that reference $MODULE_DIR$ and $PROJECT_DIR$ What do your lines look like Kevin? – Mark Jan 08 '15 at 17:25
  • In Studio as well as IDEA, my ./app/app.iml always begins with a `module` tag that has a property like `external.root.project.path="$USER_HOME$/Projects/IDEA/Derp"`. And my ./.idea/workspace.xml always contains tags like ``. (Note that `~/Projects` is a symlink to `~/BACKUP/Projects`. Some files contain the resolved path, which is another bug.) – Kevin Krumwiede Jan 08 '15 at 19:54