我是Android Studio的新手。 我如何在< JDK >文件夹下面的外部库中添加几个jar文件?


当前回答

如果你没有看到“添加为库”选项,确保你解压(解压缩)你的文件,使你有mail.jar而不是mail.zip。

然后右键单击你的文件,你可以看到“添加为库”选项。

其他回答

在你的android项目中添加一个罐子的“官方方式” 外部库,是在jar中添加依赖项{}节 build.gradle。

如果你做了以上所有的事情,但没有一个奏效,那么还有另外两种可能:

If Android Studio or some other IDE doesn't give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR. Your jar may not have a proper Manifest included therefore the compiler doesn't know what the jar can provide/don't know it's package name Maybe it's folder structure is messed up. The folder structure doesn't match with its package name. package a.b.c; should be matching to folder a > folder b > folder c. The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)


然而,如果你打算用cordova,这里有一些添加外部罐子的技巧。

“build-extras。Gradle”是更好的管理Gradle文件的方法。

下面是在基于cordova的android项目中管理额外设置的步骤:

添加jar到build-extras.gradle: //其他设置在这里,例如:buildscript{…} ext.postBuildExtras = { //你可能有一些其他的设置,例如:android{…} 依赖关系{ 编译文件(“libs / abc.jar”) } }

(更详细的步骤在这里:扩展cordova gradle文件以包括谷歌服务)

在终端,做:

Cordova构建android

请在build.gradle中提供jar文件位置

implementation fileTree(dir: '<DirName>', include: ['*.jar'])

例子:

implementation fileTree(dir: 'C:\Downloads', include: ['*.jar'])

添加单个jar文件

implementation files('libs/foo.jar')

注意: Compile在最新的gradle中已弃用,因此改用implementation。

步骤1:为项目下载任何JAR文件。

第二步:复制。jar文件和过去的libs文件夹。

步骤3:单击文件>项目结构>选择应用程序>依赖

步骤4:

步骤5:

第六步:点击Ok按钮后,我们可以看到依赖项以这样的方式添加:

如果你没有看到“添加为库”选项,确保你解压(解压缩)你的文件,使你有mail.jar而不是mail.zip。

然后右键单击你的文件,你可以看到“添加为库”选项。

虽然我想对这个问题做一个深入的回答,但回答得晚了。此方法适用于Android Studio 1.0.0及以上版本。


步骤


首先把你的文件夹结构从Android切换到Project。

现在在app文件夹中搜索libs文件夹。

一旦您将.jar文件粘贴到libs文件夹中。右键单击jar文件,最后单击Add as library。这将负责在构建中添加编译文件('libs/library_name.jar')。gradle[你不需要在你的构建文件中手动输入这个]。

现在可以开始在项目中使用这个库了。