可以用来运行标准的

可以用来运行标准的

本文介绍了Android Studio 可以用来运行标准的 Java 项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些想要隔离 Java 并对其进行快速测试的时候..

For those times when you want to isolate the Java and give it a quick test..

您能否像在 Eclipse 中一样在 Android Studio 中运行非 Android Java 项目?

Can you run non-Android Java projects in Android studio as in Eclipse?

推荐答案

在 Android Studio 0.8.6 - 3.5 上测试

使用这种方法,您可以在同一个项目中拥有 Java 模块和 Android 模块,还可以将 Java 模块作为独立的 Java 项目编译和运行.

Using this method you can have Java modules and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.

  1. 在 Android Studio 中打开您的 Android 项目.如果您没有,请创建一个.
  2. 点击文件>新建模块.选择Java Library,然后点击下一步.
  3. 填写包名等,点击完成.您现在应该会在您的 Android 项目中看到一个 Java 模块.
  4. 将您的代码添加到您刚刚创建的 Java 模块中.
  5. 单击运行按钮左侧的下拉菜单.点击编辑配置...
  6. 在新窗口中,点击窗口左上角的加号并选择Application
  7. 应该会出现一个新的应用程序配置,输入详细信息,例如您的模块的主类和类路径.
  8. 点击确定.
  1. Open your Android project in Android Studio. If you do not have one, create one.
  2. Click File > New Module. Select Java Library and click Next.
  3. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
  4. Add your code to the Java module you've just created.
  5. Click on the drop down to the left of the run button. Click Edit Configurations...
  6. In the new window, click on the plus sign at the top left of the window and select Application
  7. A new application configuration should appear, enter in the details such as your main class and classpath of your module.
  8. Click OK.

现在,如果您单击运行,这应该会编译并运行您的 Java 模块.

Now if you click run, this should compile and run your Java module.

如果您收到错误 Error: Could not find or load main class...,请再次输入您的主类(如您在步骤 7 中所做的那样),即使该字段已经填写完毕.点击Apply,然后点击Ok.

If you get the error Error: Could not find or load main class..., just enter your main class (as you've done in step 7) again even if the field is already filled in. Click Apply and then click Ok.

我的用例:我的 Android 应用程序依赖于一些预先计算的文件来运行.这些预先计算的文件是由一些 Java 代码生成的.由于这两件事是相辅相成的,因此将这两个模块放在同一个项目中是最有意义的.

My usage case:My Android app relies on some precomputed files to function. These precomputed files are generated by some Java code. Since these two things go hand in hand, it makes the most sense to have both of these modules in the same project.

新功能 - 如何在您的独立项目中启用 Kotlin

如果您想在独立项目中启用 Kotlin,请执行以下操作.

If you want to enable Kotlin inside your standalone project, do the following.

  1. 从上面的最后一步继续,将以下代码添加到您的项目级别 build.gradle(要添加的行由 >>> 表示):

  1. Continuing from the last step above, add the following code to your project level build.gradle (lines to add are denoted by >>>):

buildscript {
    >>> ext.kotlin_version = '1.2.51'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        >>> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
...

  • 将以下代码添加到您的模块级别build.gradle(要添加的行由>>>表示):

  • Add the following code to your module level build.gradle (lines to add are denoted by >>>):

    apply plugin: 'java-library'
    >>> apply plugin: 'kotlin'
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        >>> implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        >>> runtimeClasspath files(compileKotlin.destinationDir)
    }
    ...
    

  • 额外步骤:将您的 main 函数转换为 Kotlin!只需将您的主类更改为:

  • Bonus step: Convert your main function to Kotlin! Simply change your main class to:

    object Main {
        ...
        @JvmStatic
        fun main(args: Array<String>) {
            // do something
        }
        ...
    }
    

  • 这篇关于Android Studio 可以用来运行标准的 Java 项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-05 11:13