Kevin Krumwiede
Kevin Krumwiede

Reputation: 10298

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

Upvotes: 5

Views: 10062

Answers (4)

mes5k
mes5k

Reputation: 875

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!

Upvotes: 6

Mark
Mark

Reputation: 1144

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?

Upvotes: 0

Mark
Mark

Reputation: 1144

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

Upvotes: 0

Scott Barta
Scott Barta

Reputation: 80020

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.

Upvotes: 12

Related Questions