GithubHelp home page GithubHelp logo

kotlinstudy's Introduction

KotlinStudy

Kotlin Study

Run main() function in Android Studio reference stackoverflow

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. 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.

Now if you click run, this should compile and run your Java module. 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. 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.

NEW - How to enable Kotlin in your standalone project

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

  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
   }
}
...

  1. 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)
 }
 ...

  1. Bonus step: Convert your main function to Kotlin! Simple change your main class to:
 object Main {
     ...
     @JvmStatic
     fun main(args: Array<String>) {
         // do something
     }
     ...
 }

kotlinstudy's People

Contributors

margi3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.