Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use kotlin2plantuml.jar with an Android Studio project? #183

Open
LX222 opened this issue Feb 3, 2023 · 10 comments
Open

How to use kotlin2plantuml.jar with an Android Studio project? #183

LX222 opened this issue Feb 3, 2023 · 10 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@LX222
Copy link

LX222 commented Feb 3, 2023

Hi there,

I'm trying to get kotlin2plantuml.jar running with this project
https://github.com/android/uamp

What I tried was
java -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService
in the root of the Android Studio project.

But I only get

Exception in thread "main" java.lang.ClassNotFoundException: Unable to find com.example.android.uamp.media.MusicService at io.github.kelvindev15.kotlin2plantuml.utils.ReflectUtils$Companion.loadClassOrThrow(ReflectUtils.kt:34) at io.github.kelvindev15.kotlin2plantuml.utils.ReflectUtils$Companion.loadClassOrThrow$default(ReflectUtils.kt:31) at io.github.kelvindev15.kotlin2plantuml.MainKt.main(Main.kt:92)

From the README I could see , that I maybe have to pass the CLASSPATH over to the command.
So I tried

java -cp . -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService
java -cp "C:/uamp_test_uml" -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService

but unfortunatly this doesn't work and results in the same error

@kelvin-olaiya kelvin-olaiya self-assigned this Feb 4, 2023
@kelvin-olaiya kelvin-olaiya added documentation Improvements or additions to documentation question Further information is requested labels Feb 4, 2023
@kelvin-olaiya
Copy link
Contributor

kelvin-olaiya commented Feb 4, 2023

Hi!

You're right, the main issue here has to do with the classpath. Right now i can think of two "quick" solutions:

  1. Create a gradle task in the build file of the project, in order to launch io.github.kelvindev15.kotlin2plantuml.MainKt with the CLI arguments. This requires that you correctly set the classpath. In your specific case, an example could be:
task diagram(type: JavaExec) {
    var classpathList = new ArrayList<File>()
    classpathList.add("<path-to-build-dir>")
    classpathList.add(new File("kotlin2plantuml.jar"))
    classpath classpathList.toArray();

    mainClass = "io.github.kelvindev15.kotlin2plantuml.MainKt"
    args "com.example.android.uamp.media.MusicService"
    args "<any-cli-option>"
}

You can then launch the task with:

./gradlew diagram
  1. As explained here also add the kotlin2plantuml.jar file to the classpath and execute the io.github.kelvindev15.kotlin2plantuml.MainKt class. Example:
java -cp kotlin2plantuml.jar:<path-to-build-dir> io.github.kelvindev15.kotlin2plantuml.MainKt com.example.android.uamp.media.MusicService

I know, it may not the most beautiful way to deal with it...

@Oneotrix
Copy link

Oneotrix commented Feb 6, 2023

ok, it's good answer, but personaly i try to do this, but this gradle file doesn't see File class, please say, what i can do,
image

@kelvin-olaiya
Copy link
Contributor

I'm guessing you're missing an import. Try importing

import java.io.File;

At the top of the build file.

@Oneotrix
Copy link

Oneotrix commented Feb 6, 2023

well, i try to do this in build.gradle(Project) , but i cant make imopor , AS saying :
image
I have not changed the paths and names of the packages so far

@kelvin-olaiya
Copy link
Contributor

This issue seems to tackle your problem.

Also this may be useful.

@Oneotrix
Copy link

Oneotrix commented Feb 6, 2023

big thank you for answering on my questions)

@Oneotrix
Copy link

Oneotrix commented Feb 9, 2023

sorry, i solved my problem(just switched from Windows to Linux XDDD)
and i have last 2 questions :

  1. in which directory should we put(any place on the disk or in the folder with the project)
  2. path-to-build-dir - it's the path where the kotlin2plantuml.jar located, or path where located main class of app?
    thx)

@kelvin-olaiya
Copy link
Contributor

  1. If you mean where to put the kotlin2plantuml.jar, I would suggest on the root folder of your project. The gradle task i previously posted assumes the jar is there.
  2. With build-dir I meant the destination directory of the compiled class. Usually for gradle projects is the /build folder, but it actually depends on the build configuration of your project.

@kelvin-olaiya
Copy link
Contributor

Hi @LX222 & @Oneotrix
With the latest release there's now the possibility to set the classpath directly via CLI with the -cp option.

@CyxouD
Copy link

CyxouD commented Jan 22, 2024

In Android Kotlin project, when I try to run
java -jar kotlin2plantuml.jar com.milence.MainActivity -cp app/build/intermediates/javac/devDebug/classes:app/build/tmp/kotlin-classes/devDebug:app/build/intermediates/asm_instrumented_project_classes/devDebug: --recurse
in Android Gradle Project, I receive the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: androidx/appcompat/app/AppCompatActivity.

It means that it found my MainActivity class, but it can't find all other dependencies (in this case, AppCompatActivity from android core library). I think that the only solution is to include jar for each dependency manually, which makes it not practical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants