同时,我想将此项目用作Android Studio项目的一部分。
我假设这个项目产生一个库;否则,我不确定你会在Android项目中使用它做什么。然后问题就变成了:这个库是否被其他项目使用,与您计划的Android Studio项目分开?
如果答案是“是的,这个库有很多客户端”,那么你应该真的考虑将该库作为工件发布到存储库。这可能是您自己开发计算机上的存储库,或者是您的团队的本地服务器上的存储库,或者是公共存储库上的存储库(例如,如果这是一个开源项目)。您的每个客户可能都需要此库的不同版本,因此将库的不同版本作为版本化工件发布似乎很有用。
如果答案是“这个Java代码的JAR可以单独使用,比如从命令行,但作为一个库,唯一的客户端是Android Studio项目”,那么你可以设置一个混合Android Studio/Eclipse项目,将Eclipse项目移到Android Studio项目中,以便它可以用作库模块。
然后,关键是将build.gradle
文件添加到Eclipse项目/ AS模块,该模块教Gradle/AS如何构建Eclipse项目。例如,如果您有排序文件结构是预AS使用的Android项目,你可能有一个android
封这样的:
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
您还需要在此模块中的项目添加到您的settings.gradle
文件根。此时,其他模块可以使用dependencies
中的compile project(':whatever-you-call-it')
来拉入您的库。
这就是说,正如sschuberth所说,Android Studio支持纯Java模块,至少从Android Studio 2.2开始。我会走这条路,因为两个IDE将是非常系统密集型的,因为Eclipse和Android Studio都不具备“苗条”的资格。
请注意,现在继续使用Eclipse ADT进行Android开发可能不是一个好主意,因为[Google已正式结束其支持](http://android-developers.blogspot.de/2016/11/support -ended换蚀-android.html)。 – sschuberth
选项1:让Eclipse项目发布AS项目用作依赖项的JAR。例如,您可以使用本地Maven回购。选项2:将Eclipse项目作为模块移动到Android Studio项目中。在这个模块中添加一个'build.gradle',配置源代码集和教Gradle(以及Android插件)如何在Eclipse项目结构中查找文件。将该模块添加到项目根目录下的settings.gradle中。然后,让项目中的其他模块通过'compile project()'引用该模块。 – CommonsWare
@sschuberth谢谢你的注意事项。我使用Eclipse for Java(only)项目和AS for Android。这就是为什么我需要共享选项。 – c0der