gpt4 book ai didi

java - Android Studio Android Projekt 独立运行 Java

转载 作者:行者123 更新时间:2023-11-29 18:38:14 26 4
gpt4 key购买 nike

您好,我编写了一个 Android 应用程序,但该应用程序的某些部分包含应该在桌面上运行的 native 桌面 Java 代码,现在我可以在 Android Studio 中运行它并且它可以工作,提供了 Android 库作为“ stub ”,但我想导出它并使其在没有android studio的情况下可以运行。但我完全不知道从哪里开始。我不想创建 Java 模块,因为这多次破坏了我的应用程序。任何想法?我知道将 Desktop-Java 与 Android 混合使用是糟糕的设计,但无论如何我都需要 Android-App 的几乎所有 Desktop-Java 代码。

只需将运行行复制到 windows-cmd 中,即使没有 Android studio 也能正常工作,但我认为这不会在我的电脑以外的任何其他运行时环境中运行。

"C:\Program Files\Android\Android Studio\jre\bin\java" -Didea.launcher.port=55051 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\tw\AppData\Local\Android\sdk\platforms\android-26\android.jar;C:\Users\tw\AppData\Local\Android\sdk\platforms\android-26\data\res;F:\zwei18\blubberme\app\build\intermediates\classes\debug;F:\zwei18\blubberme\app\build\generated\res\rs\debug;F:\zwei18\blubberme\app\build\generated\res\resValues\debug;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-26.0.0.aar\b4b915afcc06edc72907953989a6a029\jars\classes.jar;C:\Users\tw\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\26.0.0\db09a97f4f0db8892c1a0111a4b966f97920d082\support-annotations-26.0.0.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\transition-26.0.0.aar\2216ab7388ea55822f0426346701ad90\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\transition-26.0.0.aar\2216ab7388ea55822f0426346701ad90\res;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.0.aar\b621d0ffa5f7edf4f78d6d69c706bedb\res;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.0.aar\b621d0ffa5f7edf4f78d6d69c706bedb\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\multidex-1.0.1.aar\73331bbce451c5905e18bb668d168a8a\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-fragment-26.0.0.aar\6cc4503602a68fc2387dc2bb33451a65\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-media-compat-26.0.0.aar\a2a3417028259290b6250bb82e1eef66\res;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-media-compat-26.0.0.aar\a2a3417028259290b6250bb82e1eef66\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\a8e8a3da9d5281847b733c2283a691f2\res;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\a8e8a3da9d5281847b733c2283a691f2\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-compat-26.0.0.aar\9a143a52b1b5faf3d74042078d3bb91b\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-compat-26.0.0.aar\9a143a52b1b5faf3d74042078d3bb91b\res;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-v4-26.0.0.aar\13c2be756832554a53d9af310221de3e\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-core-ui-26.0.0.aar\6fc1ad2c887486268d973e3de820567f\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-core-utils-26.0.0.aar\e4079133536e59e184d0ec91ca10bdb0\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-26.0.0.aar\f003cfe0156b60b8e5a69079c81066d9\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\design-26.0.0.aar\805dec2c6983e4269fe7d92ec69e5bc8\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\design-26.0.0.aar\805dec2c6983e4269fe7d92ec69e5bc8\res;C:\Users\tw\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.3\bde0667d7414c16ed62d3cfe993cff7f9d732373\constraint-layout-solver-1.1.3.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\recyclerview-v7-26.0.0.aar\cb0f3040d8654b9aee1823550cea3508\jars\classes.jar;C:\Users\tw\.gradle\caches\transforms-1\files-1.1\recyclerview-v7-26.0.0.aar\cb0f3040d8654b9aee1823550cea3508\res;C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 s.blubberme.engine.physics.Networks.ServerListenLoop

最佳答案

我是这样做的:

将所有核心代码放入带有 gradle 构建文件的桌面项目中。将桌面内容放入另一个桌面项目中。将 android 特定的东西放入 android studio 中。在每个其他项目中使用核心项目构建的 jar。

一个额外的好处是它使核心代码的单元测试非常容易。

请参阅下面的示例。 Android版本必须设置回调。

interface Factory {
Audio create();
class FactoryImpl implements Factory {
private FactoryImpl() {}
@Override public Audio create() {
return isAndroid()?new AndroidAudio():new WindowsAudio();
}
public static class AndroidAudio implements Audio {
AndroidAudio() {}
@Override public void play(Sound sound) {
if(Audio.Instance.sound) if(consumer!=null) consumer.accept(sound);
else l.severe("callback is not set: "+sound);
}
public void setCallback(Consumer<Sound> consumer) {
this.consumer=consumer;
}
public Consumer<Sound> consumer;
}
private static class WindowsAudio implements Audio {
WindowsAudio() {}
@Override public void play(final Sound sound) {
if(Audio.Instance.sound) try {
String filename=sound.name()+".wav";
l.info("play: "+filename);
Clip clip=AudioSystem.getClip();
AudioInputStream inputStream=AudioSystem.getAudioInputStream(new BufferedInputStream(Audio.class.getResourceAsStream(filename)));
if(inputStream!=null) {
clip.open(inputStream);
FloatControl gainControl=(FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-25.0f); // ?
clip.start();
// maybe do not wait?
while(clip.getMicrosecondLength()!=clip.getMicrosecondPosition())
Thread.sleep(1); // wait
// or at least don't wait here?
//Thread.sleep(500);
clip.close();
} else l.warning("input stream is null!");
} catch(Exception e) {
e.printStackTrace();
l.severe("caught: "+e);
l.severe("failed to play: "+sound);
}
}
}
} }

关于java - Android Studio Android Projekt 独立运行 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com