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 a external folder to build in gradle?
This question is related to
java
gradle
android-studio
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>'
}
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):
Open Module Settings
+ Add Content Root
button (center right)OK
In my experience (with native code), as long as your .so
's are built with debug symbols and from the same absolute paths, breakpoints added in source files will be automatically recognized.
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.
Just in case anyone is interested, heres 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"
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 here: Android Studio Exclude Class from build?
Until Android Studio gets updated to support include/exclude directives for Android sources, Symlinks work quite well. If you're using Windows, native tools such as junction
or mklink
can accomplish the equivalent of Un*x symlinks. CygWin can also create these with a little coersion. See: Git Symlinks in Windows and How to make symbolic link with cygwin in Windows 7
Source: Stackoverflow.com