[JAVA] Make an executable jar using Android Studio

Introduction

I will explain how to make an executable jar using Android Studio.

Version The OS version is as follows.

Microsoft Windows 10 Home
10.0.18363 N/A build 18363

The versions of Android Studio are as follows.

Android Studio 4.0
Build #AI-193.6911.18.40.6514223, built on May 21, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

The java version is as follows.

java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

Create a project

Follow the steps below to create a project.

Start Android Studio. Select Start a new Android Studio project. Create a No Activity project. Name it "My Application" (default). Let's say the Language is "Kotlin".

Create a jar module

Follow the steps below to add a jar module in your project.

Select File --New --New Module ... Select Java or Kotlin Library. Set the Library name to "lib" (default). Let the Class name be "MyClass".

fun main Open MyClass.kt. Replace the content with the following:

MyClass.kt


package com.example.lib

fun main(args: Array<String>) {}

The screen of the state executed so far is as follows.

1.jpg

build.gradle

Add the following to your lib build.gradle:

build.gradle


jar {
    manifest {
        attributes("Main-Class": "com.example.lib.MyClassKt")
    }
    from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

1.jpg

Add jar task

Follow the steps below to add a jar task.

Select View --Tool Windows --Gradle. Window is displayed. Double-click My Application --Tasks --build --jar. The build will start.

The screen of the state executed so far is as follows.

1.jpg

You will have a jar in the following path:

MyApplication\lib\build\libs\lib.jar

Run

Open a command prompt. Navigate to the path where lib.jar is located. Run the following command: If you can execute it without error, you can see that you have a jar that can be executed as expected.

java -jar .\lib.jar

Recommended Posts

Make an executable jar using Android Studio
How to make an app using Tensorflow with Android Studio
Make an android app. (Day 5)
[2020 version] How to send an email using Android Studio Javamail
Make an android app. (First day)
Make multi-project executable JAR in Gradle
Make Log of Android Studio / Xcode convenient
Beginners try using android studio Part 2 (event processing)
Beginners try using android studio Part 1 (Hello World)
How to build an executable jar in Maven
How to make an crazy Android music player
How to make an oleore generator using swagger codegen
App development beginners tried to make an Android calculator app
Notes in Android studio
Defeating Android Studio Part 3-6
Defeating Android Studio Part 1 & Part 2
You are currently using Java 6. Solution in Android Studio Gradle
[Mac] Android Studio quits for an unexpected reason during installation
[Android] [Library] I tried using an animation library called "Before After animation".