Android Studio:创建没有 Android 依赖项的 Java 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28957283/
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
Android Studio: create Java project with no Android dependencies
提问by Alexey Dmitriev
It's possible to add pure Java module to existing Android project.
可以将纯 Java 模块添加到现有的 Android 项目中。
But is it possible to create pure Java project with no Android dependencies?
但是是否可以创建没有 Android 依赖项的纯 Java 项目?
采纳答案by Aleksander Mielczarek
Yes, it is possible. You have to manually create all necessary files.
对的,这是可能的。您必须手动创建所有必需的文件。
Here are steps for Gradle based project:
以下是基于 Gradle 的项目的步骤:
- Remove include ':app' form settings.gradle
- Remove app directory
- Replace build.gradle with example from end of this post (IntelliJ creates similar)
- Create folder hierarchy for your Java code (src/main/java)
Select Edit Configuration from drop down menu where normally you start project
Click Add new Configuration and select Application
- In Main class point your Main class.
- 删除包含 ':app' 表单 settings.gradle
- 删除应用程序目录
- 用本文末尾的示例替换 build.gradle(IntelliJ 创建了类似的)
- 为您的 Java 代码创建文件夹层次结构 (src/main/java)
从通常开始项目的下拉菜单中选择“编辑配置”
单击添加新配置并选择应用程序
- 在 Main 类中指向您的 Main 类。
Android studio is more or less like IntelliJ Community Edition.
Android Studio 或多或少类似于 IntelliJ 社区版。
apply plugin: 'java'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
回答by crazy_coder
I think this is possible to create a new module from following path (Using Androdi Stdio 1.1.0):
我认为可以从以下路径创建一个新模块(使用 Androdi Stdio 1.1.0):
File> New Module> Choose from more module> java library
文件>新建模块>从更多模块中选择>java库
Hope it will work's for you.
希望它对你有用。
回答by Apurva
No you cannot create a java
project with Android Studio
because AS's building system won't let you build your projectonce you are done writing your application. Android Studio's gradle
building system only builds .apk
file.
不,您无法创建java
项目,Android Studio
因为一旦您完成编写应用程序,AS 的构建系统将不允许您构建项目。Android Studio 的gradle
构建系统只构建.apk
文件。
Android Studio won't support most things requiring an external database or application server.
Android Studio 不支持大多数需要外部数据库或应用程序服务器的东西。
回答by user3452758
Not via the 'New project' wizard.
不是通过“新建项目”向导。
One alternative is to create a Java maven/gradle project outside Android Studio, then 'import' it into AS via File
->Open
.
一种替代方法是在 Android Studio 之外创建一个 Java maven/gradle 项目,然后通过File
->将其“导入”到 AS 中Open
。
Gradle has the init
plugin to set up a java project scaffold:
Gradle 有init
设置 java 项目脚手架的插件:
gradle init --type java-library
gradle init --type java-library
https://docs.gradle.org/current/userguide/build_init_plugin.html
https://docs.gradle.org/current/userguide/build_init_plugin.html
回答by Scott Grodberg
回答by JT.
Easiest method is to temporarily disable the Android Support Plugin. Un-check Configuration > Plugin > Android Support Plugin After restarting close any current project and you will get the new project wizard with all of your non-android options. Once your new project is created you can re-enable the Android plugin.
最简单的方法是暂时禁用 Android Support Plugin。取消选中 Configuration > Plugin > Android Support Plugin 重新启动后关闭任何当前项目,您将获得包含所有非 android 选项的新项目向导。创建新项目后,您可以重新启用 Android 插件。
回答by keystoneclimber
One thing that you might want to add here to help REALLY new folks out (it kept tripping me up) is that after creating the folder hierarchy for your Java code (src/main/java) you must right click the java folder and select Mark Directory As > Sources Root. Otherwise, you won't have the option of creating a new java class within the foo directory.
您可能想在此处添加一件事以帮助真正的新手(它一直让我感到困惑)是,在为您的 Java 代码(src/main/java)创建文件夹层次结构后,您必须右键单击 java 文件夹并选择 Mark目录为 > 源根。否则,您将无法选择在 foo 目录中创建新的 java 类。
回答by ssmm
Another way from my perspective:
从我的角度来看的另一种方式:
- Create a new android project
- Add java module to your project
- Close android studio project and go to project's directory, just take the java module folder and paste it to new directory
- In Android Studio open exthis this module, ide will generate gradle file for you.
- 创建一个新的安卓项目
- 将 java 模块添加到您的项目中
- 关闭android studio项目并进入项目目录,只需将java模块文件夹粘贴到新目录即可
- 在Android Studio中打开exthis这个模块,ide会为你生成gradle文件。