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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:05:21  来源:igfitidea点击:

Android Studio: create Java project with no Android dependencies

javaandroidandroid-studio

提问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 的项目的步骤:

  1. Remove include ':app' form settings.gradle
  2. Remove app directory
  3. Replace build.gradle with example from end of this post (IntelliJ creates similar)
  4. Create folder hierarchy for your Java code (src/main/java) enter image description here
  5. Select Edit Configuration from drop down menu where normally you start project

  6. Click Add new Configuration and select Application enter image description here

  7. In Main class point your Main class.
  1. 删除包含 ':app' 表单 settings.gradle
  2. 删除应用程序目录
  3. 用本文末尾的示例替换 build.gradle(IntelliJ 创建了类似的)
  4. 为您的 Java 代码创建文件夹层次结构 (src/main/java) 在此处输入图片说明
  5. 从通常开始项目的下拉菜单中选择“编辑配置”

  6. 单击添加新配置并选择应用程序 在此处输入图片说明

  7. 在 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 javaproject with Android Studiobecause AS's building system won't let you build your projectonce you are done writing your application. Android Studio's gradlebuilding system only builds .apkfile.

不,您无法创建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 initplugin 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

Easiest Way From Scratch

从头开始的最简单方法

  1. Create a empty folder where you want to put the project
  2. At the 'Welcome to Android Studio' screen, choose "Open existing..." enter image description here
  3. Navigate to your folder and hit OK
  4. Your project is created, now add your java files enter image description here
  5. Choose the SDK and you're done! Happy coding
  1. 创建一个要放置项目的空文件夹
  2. 在“欢迎使用 Android Studio”屏幕上,选择“打开现有...” 在此处输入图片说明
  3. 导航到您的文件夹并点击确定
  4. 您的项目已创建,现在添加您的 java 文件 在此处输入图片说明
  5. 选择 SDK 就大功告成了!快乐编码

回答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:

从我的角度来看的另一种方式:

  1. Create a new android project
  2. Add java module to your project
  3. Close android studio project and go to project's directory, just take the java module folder and paste it to new directory
  4. In Android Studio open exthis this module, ide will generate gradle file for you.
  1. 创建一个新的安卓项目
  2. 将 java 模块添加到您的项目中
  3. 关闭android studio项目并进入项目目录,只需将java模块文件夹粘贴到新目录即可
  4. 在Android Studio中打开exthis这个模块,ide会为你生成gradle文件。