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?
在Eclipse中,我可以將一個源文件夾作為“鏈接源文件夾”添加到我的android項目中。如何在Android Studio中實現同樣的功能?
Or is it possible to add a external folder to build in gradle?
或者是否可以在gradle中添加一個外部文件夾?
5 个解决方案
#1
79
in your build.gradle add the following to the end of the android node
在您的構建。gradle在android節點的末尾添加以下內容
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/'
}
}
#2
14
The right answer is:
正確答案是:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/'
}
}
Furthermore, if your external source directory is not under src/main, you could use a relative path like this:
此外,如果您的外部源目錄不在src/main目錄下,您可以使用如下的相對路徑:
sourceSets {
main.java.srcDirs += 'src/main/../../../'
}
#3
2
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
您可以向構建腳本添加一個源文件夾,然后同步。在文檔中查找源代碼: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.
我還沒有找到添加測試源文件夾的好方法。我已經手動將源代碼添加到.iml文件中。當然,這意味着每次構建腳本被同步時,它都會消失。
#4
2
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?
雖然sourceSets允許您包含整個目錄結構,但是沒有辦法在Android Studio中排除部分目錄結構(從1.2版開始),就像這里描述的那樣:Android Studio在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
在Android Studio更新以支持包含/排除Android源代碼的指令之前,symlink的工作非常好。如果您正在使用Windows,本地工具(如連接或mklink)可以完成與Un*x符號鏈接相同的任務。CygWin也可以用一點強迫來創造這些。參見:Windows中的Git符號鏈接,以及如何在Windows 7中與cygwin進行符號鏈接
#5
0
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
這里有一個完整的Java模塊等級文件,可以正確地生成和引用Android多模塊應用程序中構建的人工制品
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"