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

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

问题描述

我最近下载了 Android Studio 来开发 Android 应用程序(我来自 Eclipse),但我在使用外部库和/或外部 JAR 文件以及我自己的项目时遇到了问题.注意:以下测试是针对在 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.

示例 1:JAR 导入.

Example 1: JAR Import.

  1. 从 Google 下载 Admobs SDK 的全新副本.

  1. Download a fresh copy of the Admobs SDK from Google.

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

Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.

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

In the project explorer, right click on the newly added library.jar and click on 'Add as Library'.

从技术上讲,此时一切正常,导入工作正常,布局编辑器显示 AdView 小部件的预览以及所有这些.唯一的问题是它没有成功编译.

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

从控制台登录:

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

我尝试运行 gradlew compileDebug --stacktrace,问题似乎是尽管能够在代码和代码中成功导入类设计编辑器,在编译时,它无法解析导入.这是日志的相关部分:(此处为完整堆栈跟踪)

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

还值得注意的是,在不尝试添加库/JAR 的情况下,项目编译得很好.

It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine.

然后我尝试编辑 build.gradle 文件以包含这样的新库:

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')
}

这一次,它确实编译成功,但应用程序现在强制关闭,很明显,它无法从应用程序包的 lib 中找到特定的类.

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.

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

有什么想法吗?

推荐答案

所以,

使用 Android Studio 0.1.1 成功导入 JAR 到您的项目的步骤:

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

  • 下载 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 中重新打开您的应用程序项目.
  • 测试运行您的应用程序,它应该可以成功运行.

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

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