Android Studio - 导入外部库/ Jar [英] Android Studio - Importing external Library/Jar

查看:160
本文介绍了Android Studio - 导入外部库/ Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了Android Studio来开发Android应用程序(我来自Eclipse),而且我在使用外部库和/或外部JAR文件的同时也遇到了问题。 注意:在Android Studio中从头开始创建的新的应用程序项目进行了以下测试。



示例1:JAR导入。


  1. 从Google下载Admobs SDK的新版本。

  2. 将库jar 将GoogleAdMobAdsSdk-6.4.1.jar 复制到项目的/ libs /文件夹中。

  3. 在项目资源管理器中,右键单击新添加的library.jar,然后单击添加为库。

技术上在这一点上一切都有效,导入工作很好,布局编辑器显示了 AdView 小部件,以及所有这些。 唯一问题是它不会成功编译。



从控制台登录:

  Gradle:
FAILURE :构建失败,异常。

*出了什么问题:
执行失败的任务':APITests:compilePaidDebug'。
>汇编失败;有关详细信息,请参阅编译器错误输出。

*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多的日志输出。

无法使用Gradle发行版http://services.gradle.org/distributions/gradle-1.6-bin.zip执行构建。

我尝试运行 gradlew compileDebug --stacktrace ,而且问题似乎是尽管能够在代码和代码中都强制导入这些类。设计编辑,在编译时,无法解析导入。以下是日志的相关部分:(此处的完整堆栈跟踪)

  java:6:错误:com.google.ads包不存在
import com.google.ads.AdRequest;
java:7:错误:com.google.ads包不存在
import com.google.ads.AdView;
java:11:错误:找不到符号
AdView mAdView;
符号:类AdView
位置:class MainActivity
java:22:错误:找不到符号
mAdView =(AdView)this.findViewById(R.id.adView);
符号:类AdView
位置:class MainActivity
java:23:错误:找不到符号
mAdView.loadAd(new AdRequest());
符号:class AdRequest
位置:class MainActivity
5个错误
:测试:compileDebug失败

但是,再次,导入在编辑器中工作良好,依赖关系如下:





然后,我尝试编辑build.gradle文件以包含新的lib,如下所示:

 依赖关系{
编译文件('libs / android-support-v4.jar','libs / GoogleAdMobAdsSdk-6.4.1.jar')
}

这一次,它确实编译成功,但是应用程序现在强制关闭,显然它不能从lib中找到一个特定的类应用程序包。

 导致:java.lang.ClassNotFoundException:没有在路径上找到类com.google.ads.AdView /data/app/com.foo.test-1.apk 

任何想法?谢谢!

解决方案

所以,



使用Android Studio 0.1.1将JAR强制导入您的项目:




  • 下载library.jar文件并将其复制到/ libs /您的应用程序项目中的文件夹。

  • 打开build.gradle文件并编辑您的依赖项以包含新的.jar文件:



编译文件('libs / android-support-v4.jar','libs / GoogleAdMobAdsSdk-6.4.1.jar')




  • 文件 - >关闭项目

  • 在项目的根位置打开命令提示符,即'C: \Users\Username\AndroidStudioProjects\MyApplicationProject\'

  • 在命令提示符下,键入 gradlew clean ,等待完成。

  • 在Android Studio中重新打开您的应用程序项目。

  • 测试运行您的应用程序,它应该成功运行。


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. NOTE: the following tests were conducted on a new application project created from scratch in Android Studio.

Example 1: JAR Import.

  1. Download a fresh copy of the Admobs SDK from Google.
  2. Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.
  3. In the project explorer, right click on the newly added library.jar and click on '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. The only problem is that it doesn't compile succesfully.

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 sucesfully in both the code & design editor, at compile time, it cannot resolve the imports. 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.

I then tried editing the build.gradle file to include the new lib like this:

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

This time, it did compile succesfully, but the app now force closes, as apparently it cannot find a specific class from the lib in the application package.

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

Any ideas? Thanks!

解决方案

So,

Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1:

  • Download the library.jar file and copy it to your /libs/ folder inside your application project.
  • Open the build.gradle file and edit your dependencies to include the new .jar file:

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, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
  • On the command prompt, type gradlew clean, wait till it's done.
  • Reopen your application project in Android Studio.
  • Test run your application and it should work succesfully.

这篇关于Android Studio - 导入外部库/ Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆