简体   繁体   中英

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. It contains some java code that I need for my gradle project. I tried to import it but it doesn't work at all. Here is that folder with src classes.

在此处输入图片说明

在此处输入图片说明

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). Honestly, seeing as you are using gradle you should completely ignore this method.

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.

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

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM