简体   繁体   English

Android Studio - 导入外部库/ Jar

[英]Android Studio - Importing external Library/Jar

I recently downloaded Android Studio to develop Android applications (I'm coming from Eclipse), and I am having issues using external libraries and/or external JAR files alongside my own project. 我最近下载了Android Studio以开发Android应用程序(我来自Eclipse),我在使用外部库和/或外部JAR文件以及我自己的项目时遇到了问题。 NOTE : the following tests were conducted on a new application project created from scratch in Android Studio. 注意 :以下测试是在Android Studio中从头开始创建的应用程序项目上进行的。

Example 1: JAR Import. 示例1:JAR导入。

  1. Download a fresh copy of the Admobs SDK from Google. 从Google下载Admobs SDK的最新副本。

  2. Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder. 将库jar GoogleAdMobAdsSdk-6.4.1.jar到项目的/ libs /文件夹中。

  3. In the project explorer, right click on the newly added library.jar and click on 'Add as Library'. 在项目资源管理器中,右键单击新添加的library.jar,然后单击“Add as Library”。

Technically at this point everything works , imports work just fine, the layout editor shows a preview of the AdView widget and all that. 从技术上讲,此时一切正常 ,导入工作正常,布局编辑器显示AdView小部件的预览以及所有这些。 The only problem is that it doesn't compile successfully. 唯一的问题是它无法成功编译。

Log from console: 从控制台登录:

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':APITests:compilePaidDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.

I tried running gradlew compileDebug --stacktrace , and the problem seems to be that the despite being able to import the classes successfully in both the code & design editor, at compile time, it cannot resolve the imports. 我尝试运行gradlew compileDebug --stacktrace ,问题似乎是尽管能够在代码和设计编辑器中成功导入类,但在编译时,它无法解析导入。 Here's the relevant part of the log: ( full stacktrace here ) 这是日志的相关部分:( 完整的堆栈跟踪

java:6: error: package com.google.ads does not exist
import com.google.ads.AdRequest;    
java:7: error: package com.google.ads does not exist
import com.google.ads.AdView;
java:11: error: cannot find symbol
AdView mAdView;
symbol:   class AdView
location: class MainActivity
java:22: error: cannot find symbol
mAdView = (AdView)this.findViewById(R.id.adView);
symbol:   class AdView
location: class MainActivity
java:23: error: cannot find symbol
mAdView.loadAd(new AdRequest());
symbol:   class AdRequest
location: class MainActivity
5 errors
:Test:compileDebug FAILED

But again, the imports work well in the editor, and the dependency is there: 但同样,导入在编辑器中运行良好,依赖性存在:

在此输入图像描述

It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine. 值得注意的是,如果不尝试添加库/ JAR,项目编译就可以了。

I then tried editing the build.gradle file to include the new lib like this: 然后我尝试编辑build.gradle文件以包含新的lib,如下所示:

dependencies {
compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

This time, it did compile successfully, but the app now forces closes, as apparently, it cannot find a specific class from the lib in the application package. 这一次,它确实成功编译,但现在应用程序强制关闭,显然,它无法从应用程序包中的lib找到特定的类。

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.foo.test-1.apk

Any ideas? 有任何想法吗?

So, 所以,

Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1: 要使用Android Studio 0.1.1将JAR导入项目,请遵循以下步骤:

  • Download the library.jar file and copy it to your /libs/ folder inside your application project. 下载library.jar文件并将其复制到应用程序项目中的/ libs /文件夹中。
  • Open the build.gradle file and edit your dependencies to include the new .jar file: 打开build.gradle文件并编辑依赖项以包含新的.jar文件:

compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')

  • File -> Close Project 文件 - >关闭项目
  • Open a command prompt on your project's root location, ie 'C:\\Users\\Username\\AndroidStudioProjects\\MyApplicationProject\\' 在项目的根位置打开命令提示符,即'C:\\Users\\Username\\AndroidStudioProjects\\MyApplicationProject\\'
  • On the command prompt, type gradlew clean , wait till it's done. 在命令提示符下,键入gradlew clean ,等待它完成。
  • Reopen your application project in Android Studio. 在Android Studio中重新打开您的应用程序项目。
  • Test run your application and it should work succesfully. 测试运行您的应用程序,它应该成功运行。

You don't need to close the project and go to command line to invoke grade:clean. 您不需要关闭项目并转到命令行来调用grade:clean。 Go to Build-> Rebuild Project 转到Build-> Rebuild Project

Easy way and it works for me. 简单的方法,它适合我。 Using Android Studio 0.8.2. 使用Android Studio 0.8.2。

  1. Drag jar file under libs. 在libs下拖动jar文件。
  2. Press "Sync Project with Gradle Files" button. 按“与Gradle文件同步项目”按钮。

在此输入图像描述

"simple solution is here" “简单的解决方案在这里”

1 .Create a folder named libs under the app directory for that matter any directory within the project.. 1.在app目录下创建一个名为libs的文件夹,用于项目中的任何目录。

2 . 2。 Copy Paste your Library to libs folder 复制将库粘贴libs文件夹

3 .You simply copy the JAR to your libs/ directory and then from inside Android Studio, right click the Jar that shows up under libs/ > Add As Library.. 3. .You simply copy the JAR to your libs/ directory and then from inside Android Studio, right click the Jar that shows up under libs/ > Add As Library..

Peace! 和平!

Here is how I got it going specifically for the admob sdk jar file: 这是我如何专门为admob sdk jar文件:

  1. Drag your jar file into the libs folder. jar文件拖到libs文件夹中。
  2. Right click on the jar file and select Add Library now the jar file is a library lets add it to the compile path 右键单击jar文件并选择Add Library,现在jar文件是一个库,我们可以将它添加到编译路径中
  3. Open the build.gradle file (note there are two build.gradle files at least, don't use the root one use the one in your project scope). 打开build.gradle文件(注意至少有两个build.gradle文件,不要使用root用户使用项目范围中的那个)。
  4. Find the dependencies section (for me i was trying to the admob -GoogleAdMobAdsSdk jar file) eg 找到依赖项部分(对我来说,我正在尝试使用admob -GoogleAdMobAdsSdk jar文件),例如

     dependencies { compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.3.1.jar') } 
  5. Last go into settings.gradle and ensure it looks something like this: 最后进入settings.gradle并确保它看起来像这样:

     include ':yourproject', ':yourproject:libs:GoogleAdMobAdsSdk-6.3.1' 
  6. Finally, Go to Build -> Rebuild Project 最后,转到Build - > Rebuild Project

你从Eclipse导出项目,然后从Android Studio导入项目,这应该解决你的问题,打开一个eclipse项目,而不从Android Studio导入它你可能会导致问题,看看:(借口我的语言,我说西班牙语。) http ://developer.android.com/intl/es/sdk/installing/migrate.html

I had the problem not able to load jar file in libs folder in Android Studio. 我遇到的问题是无法在Android Studio的libs文件夹中加载jar文件。 If you have added JAR file in libs folder, then just open build.gradle file and save it without editing anything else. 如果您已在libs文件夹中添加了JAR文件,则只需打开build.gradle文件并保存即可,无需编辑任何其他内容。 If you have added this line 如果您已添加此行

compile fileTree(dir: 'libs', include: ['*.jar'])

save it and clean the project .. In next build time Android Studio will load the JAR file. 保存并清理项目..在下一个构建时,Android Studio将加载JAR文件。

Hope this helps. 希望这可以帮助。

I'm using Android Studio 0.5.2. 我正在使用Android Studio 0.5.2。 So if your version is lower than mine my answer may not work for you. 因此,如果您的版本低于我的版本,我的答案可能不适合您。

3 ways to add a new Jar to your project: 向项目中添加新Jar的3种方法:

  1. Menu under Files-->Project Structure 文件 - >项目结构下的菜单
  2. Just press ' F4 ' 只需按' F4 '
  3. under Project navigation, right clink on any java library and a context menu will show then click on ' Open Library Settings ' 在项目导航下,右侧任何Java库的clink和上下文菜单将显示,然后单击“ 打开库设置

A Project Structure window will popup. 将弹出“项目结构”窗口。

On the left column click on 'Libraries' then look at the right pane where there is a plus sign '+' and click on it then enter the path to your new library. 在左侧列中单击“Libraries”,然后查看右侧窗格,其中有一个加号“+”并单击它,然后输入新库的路径。

Make sure the new library is under the 'project\\libs\\' folder otherwise you may get a broken link when you save your project source code. 确保新库位于“project \\ libs \\”文件夹下,否则在保存项目源代码时可能会出现断开的链接。

I am currently using Android Studio 1.4. 我目前正在使用Android Studio 1.4。

For importing and adding libraries, I used the following flow -> 为了导入和添加库,我使用了以下流程 - >

1. Press **Alt+Ctr+Shift+S** or Go to **File --> Project** Structure to open up the Project Structure Dialog Box.

2. Click on **Modules** to which you want to link the JAR to and Go to the Dependency Tab.

3. Click on "**+**" Button to pop up Choose Library Dependency.

4. Search/Select the dependency and rebuild the project.

I used the above approach to import support v4 and v13 libraries. 我使用上面的方法导入支持v4和v13库。

I hope this is helpful and clears up the flow. 我希望这是有用的,并清除流程。

我使用android studio 0.8.6并在项目中导入外部库,将该库粘贴到libs文件夹中,并在内部build.gradle写入路径内的依赖关系,如此编译文件('libs / google-play-services.jar')

In Android Studio (mine is 2.3.1) go to File - Project Structure: 在Android Studio(我的是2.3.1)中,转到文件 - 项目结构:

在此输入图像描述

Check the libs version requirements. 检查libs版本要求。 The lib might be trying to call a method from from a higher Android version. lib可能试图从更高的Android版本调用方法。 Try adding the apps compatability libs. 尝试添加应用程序兼容性库。

When all else fails try switching from a Gradle build to an Eclipse build. 当所有其他方法都失败时,尝试从Gradle构建切换到Eclipse构建。

Delete and restructure your folders to the eclipse style and delete everything but your code. 删除文件夹并将其重组为eclipse样式,并删除除代码之外的所有内容。 Then reload the project with your libs. 然后使用libs重新加载项目。

Once it runs go back to Gradle, if you wish. 一旦运行,如果您愿意,请返回Gradle。

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

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