How can I add a linked source folder in Android Studio?

JavaGradleAndroid Studio

Java Problem Overview


In Eclipse I can add a source folder to my Android project as a "linked source folder". How do I achieve the same thing in Android Studio?

Or is it possible to add an external folder to build in Gradle?

Java Solutions


Solution 1 - Java

In your build.gradle file, add the following to the end of the Android node:

android {
    ....
    ....

    sourceSets {
        main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
    }

}

Solution 2 - Java

The right answer is:

android {
    ....
    ....

    sourceSets {
        main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
    }
}

Furthermore, if your external source directory is not under src/main, you could use a relative path like this:

sourceSets {
    main.java.srcDirs += 'src/main/../../../<YOUR DIRECTORY>'
}

Solution 3 - Java

You can add a source folder to the build script and then sync. Look for sourceSets in the documentation here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basic-Project

I haven't found a good way of adding test source folders. I have manually added the source to the .iml file. Of course this means it will go away everytime the build script is synched.

Solution 4 - Java

While sourceSets allows you to include entire directory structures, there's no way to exclude parts of it in Android Studio (as of version 1.2), as described in https://stackoverflow.com/questions/16701256/android-studio-exclude-class-from-build.

Until Android Studio gets updated to support include/exclude directives for Android sources, symbolic links work quite well. If you're using Windows, native tools such as junction or mklink can accomplish the equivalent of symbolic links on Unix-like systems. Cygwin can also create these with a little coercion. See: https://stackoverflow.com/questions/5917249/git-symlinks-in-windows/30457566#30457566 and https://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7.

Solution 5 - Java

Here’s a complete Java module Gradle file that correctly generates and references the built artefacts within an Android multi-module application:

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
    }
}

apply plugin: "net.ltgt.apt"
apply plugin: "java-library"
apply plugin: "idea"

idea {
    module {
        sourceDirs += file("$buildDir/generated/source/apt/main")
        testSourceDirs += file("$buildDir/generated/source/apt/test")
    }
}

dependencies {

    // Dagger 2 and Compiler
    compile "com.google.dagger:dagger:2.15"
    apt "com.google.dagger:dagger-compiler:2.15"
    compile "com.google.guava:guava:24.1-jre"

}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

Solution 6 - Java

If you're not using Gradle (creating a project from an APK, for instance), this can be done through the Android Studio UI (as of version 3.3.2):

  • Right-click the project root directory and pick Open Module Settings
  • Hit the + Add Content Root button (center right)
  • Add your path and hit OK

In my experience (with native code), as long as your .so files are built with debug symbols and from the same absolute paths, breakpoints added in source files will be automatically recognized.

Solution 7 - Java

This is for Kotlin DSL (build.gradle.kts):

android {
    sourceSets["main"].java.srcDirs("src/main/myDirectory/code/")
    sourceSets["main"].resources.srcDirs("src/main/myDirectory/resources/")

    // Another notation:
    // sourceSets {
    //     getByName("main") {
    //         java.srcDirs("src/main/myDirectory/code/")
    //         resources.srcDirs("src/main/myDirectory/resources/")
    //     }
    // }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErik ZView Question on Stackoverflow
Solution 1 - JavafabrizotusView Answer on Stackoverflow
Solution 2 - JavalyfshadybossView Answer on Stackoverflow
Solution 3 - JavatomView Answer on Stackoverflow
Solution 4 - JavaBrian WhiteView Answer on Stackoverflow
Solution 5 - JavaHectorView Answer on Stackoverflow
Solution 6 - JavaJeremy NikolaiView Answer on Stackoverflow
Solution 7 - JavaMahozadView Answer on Stackoverflow