简体   繁体   English

如何包含Java src代码,以便我可以在Android Studio中使用它

[英]How to include java src code so i can use it inside Android studio

I have found some src code that I want to use in my projects in Android Studio. 我找到了一些要在Android Studio项目中使用的src代码。 It contains some java code that I need for my gradle project. 它包含我的gradle项目所需的一些Java代码。 I tried to import it but it doesn't work at all. 我尝试导入它,但是根本不起作用。 Here is that folder with src classes. 这是带有src类的文件夹。

在此处输入图片说明

在此处输入图片说明

Does anyone know a solution? 有人知道解决方案吗?

If it's your source code (or a library you've downloaded that has the source code) then can just copy and paste the files into a directory within your project and add them to source control. 如果是您的源代码(或您下载的具有源代码的库),则只需将文件复制并粘贴到项目内的目录中,然后将其添加到源代码管理中。 You'll need to open the files and make sure the package names are correct (matching the path that they are put into). 您需要打开文件,并确保软件包名称正确(匹配它们放入的路径)。 This assumes that the library only depends on core Java features and not other libraries (otherwise you'll need to manually copy and paste all their source or build them and bring them in as dependencies). 假设该库仅依赖于Java的核心功能,而不依赖于其他库(否则,您将需要手动复制和粘贴其所有源代码或构建它们并将它们作为依赖项引入)。 Honestly, seeing as you are using gradle you should completely ignore this method. 老实说,看到您正在使用gradle时,您应该完全忽略此方法。

Another way to do it would be to compile the source code to a .jar file (using Java compiler: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html ) and then import the produced .jar as a dependency to your project. 另一种方法是将源代码编译为.jar文件(使用Java编译器: https : //docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html ),然后将产生的.jar导入为项目的依赖项。

Or, even better, just open your modules gradle file and in the dependencies section, add this: compile "com.badlogicgames.box2dlights:box2dlights:1.4" then sync the project. 或者,甚至更好的是,只需打开模块gradle文件,并在“依赖项”部分中添加以下内容:编译"com.badlogicgames.box2dlights:box2dlights:1.4"然后同步项目。 Boom, now you have the library as a dependency so you can use all it's public classes etc. And you didn't even have to do any compiling, copy pasting etc. The power of gradle! 繁荣,现在您将库作为依赖项,因此可以使用其所有的公共类等。您甚至不必进行任何编译,复制粘贴等。gradle的强大功能!

Tip: Here is what I am guessing you did. 提示:这是我想您所做的。 You researched the library or heard about it somewhere and came across this page: github.com/libgdx/box2dlights . 您研究了该库或在某处听说过它,并遇到了以下页面: github.com/libgdx/box2dlights Then you didn't scroll down, instead, you clicked clone or download. 然后您没有向下滚动,而是单击了克隆或下载。 This is where your confusion started. 这是您开始困惑的地方。 Because it's open source, yes you can download and build it yourself (or download and modify it, which is why you usually clone a repo). 因为它是开源的,是的,您可以自己下载并构建它(或下载并修改它,这就是您通常克隆存储库的原因)。 Instead, in general, scroll down and you will see how to include libs through a simple 1 liner in gradle. 相反,通常向下滚动,您将看到如何通过gradle中的简单1衬里包含lib。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 代码中使用 Android.so 库? - How can I use an Android .so library in Java code? 我如何将Android代码导出到Android Studio中的Java库 - How i can export Android code to java library in Android Studio 如何从android studio中的java代码访问命令行? - How can I access commandline from java code in android studio? 在Java代码中使用Android .so - Use Android .so in Java code 如何在 Java class (Android Studio) 中使用 getApplicationContext? - How can i use getApplicationContext in Java class (Android Studio)? 如何在Java代码中使用javascript中声明的变量? - How can I use variables declared in javascript inside Java code? Android Studio:如何在Android模块中包含Java模块? - Android Studio: How do I include a Java Module in an Android Module? 如何将ZXing集成到Android Studio项目中,以便我可以在Core目录中编辑代码 - How to integrate ZXing in Android Studio project so that I can edit code in the Core directory java-maven2:如何在pom中包含一个jar作为依赖,这样我就可以访问src / test / java文件夹中的测试类或类 - java-maven2: How to include the a jar as depedency in pom so that I will be able to access test classes or classes in src/test/java folder 如何修改NDK中的Android位图,以便在Java端使用它? - How can I modify an Android bitmap in the NDK so that I can use it on the Java side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM