Java 如何在 Android Studio 项目中添加类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26521836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add classpath in an Android Studio project
提问by Stefano Piovesan
I have an Android project with an Android application that depends on a pure Java library (and the Java library uses some others compiled jars libraries).
I added the dependencies, I can build the project, but at run time I have a ClassNotFoundException
error.
I had to add to theCLASSPATH
environment variable the path to the jars.
Is there a way to set the classpath locally for the project only, like using the command line option
我有一个带有 Android 应用程序的 Android 项目,该应用程序依赖于纯 Java 库(Java 库使用其他一些编译的 jars 库)。
我添加了依赖项,我可以构建项目,但是在运行时出现ClassNotFoundException
错误。
我必须将CLASSPATH
jars 的路径添加到环境变量中。
有没有办法只在本地为项目设置类路径,比如使用命令行选项
java –classpath <path to the jars>
in the Android studio Run/Debug Configurations?
在 Android Studio 运行/调试配置中?
回答by Arnold Layne
First off all, be sure there's a "libs" subfolder in the "app" folder of your project. If there's not, create one. Next, in the app/build.gradle file, add this line of code:
首先,确保项目的“app”文件夹中有一个“libs”子文件夹。如果没有,请创建一个。接下来,在 app/build.gradle 文件中,添加这行代码:
dependencies {
...
compile fileTree(dir:'libs', include: ['*.jar'])
...
}
Place all of your .jar files in the "libs" folder, and voila, you're done
将所有 .jar 文件放在“libs”文件夹中,瞧,大功告成
回答by User
Thanks to Arnold Layne, I created a folder named libs.
感谢Arnold Layne,我创建了一个名为 libs 的文件夹。
Then, I went to
然后,我去了
Files -> Project Structure (new menu opens) -> Modules (on the left) -> app -> Dependencies
文件 -> 项目结构(打开新菜单)-> 模块(在左侧)-> 应用程序 -> 依赖项
There, I could add the library with the +
Button.
在那里,我可以使用+
Button添加库。
This created this entry:
这创建了这个条目:
dependencies {
implementation files('libs/commons-lang3-3.7.jar')
}
回答by Jo?o Scheleder Neto
Add this lines to build.gradle
of app
module.
将此行添加到build.gradle
ofapp
模块。
dependencies {
...
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/mysql-connector-java-8.0.16')
...
}