Java 如何在 Android Studio 中添加链接的源文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18947314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a linked source folder in Android Studio?
提问by Erik Z
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 中构建?
采纳答案by fabrizotus
in your build.gradle add the following to the end of the android node
在您的 build.gradle 中,将以下内容添加到 android 节点的末尾
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
回答by tom
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
您可以将源文件夹添加到构建脚本,然后进行同步。在此处的文档中查找 sourceSets: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 文件中。当然,这意味着每次同步构建脚本时它都会消失。
回答by lyfshadyboss
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:
此外,如果您的外部源目录不在 下src/main
,您可以使用这样的相对路径:
sourceSets {
main.java.srcDirs += 'src/main/../../../<YOUR DIRECTORY>'
}
回答by Brian White
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 Exclude Class from build?
Until Android Studio gets updated to support include/exclude directives for Android sources, Symlinkswork quite well. If you're using Windows, native tools such as junction
or mklink
can accomplish the equivalent of Un*x symlinks. CygWincan also create these with a little coersion. See: Git Symlinks in Windowsand How to make symbolic link with cygwin in Windows 7
在 Android Studio 更新以支持 Android 源的包含/排除指令之前,符号链接工作得很好。如果您使用的是 Windows,诸如junction
或 之类的本机工具mklink
可以完成等效于 Un*x 符号链接的工作。 CygWin也可以通过一些强制来创建这些。请参阅:Windows 中的 Git符号链接以及如何在 Windows 7 中使用 cygwin 进行符号链接
回答by Hector
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 模块 gradle 文件,它可以在 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"
回答by Jeremy Nikolai
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):
如果您不使用 gradle(例如,从 APK 创建项目),则可以通过 Android Studio UI 完成(从 3.3.2 版开始):
- Right-click the project root directory, pick
Open Module Settings
- Hit the
+ Add Content Root
button (center right) - Add your path and hit
OK
- 右键单击项目根目录,选择
Open Module Settings
- 点击
+ Add Content Root
按钮(右中) - 添加您的路径并点击
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.
根据我的经验(使用本机代码),只要您的.so
's 是使用调试符号构建的并且来自相同的绝对路径,源文件中添加的断点将被自动识别。