Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

android - Android Studio:将jar添加为库?(Android Studio: Add jar as library?)

I'm trying to use the new Android Studio but I can't seem to get it working correctly.

(我正在尝试使用新的Android Studio,但我似乎无法让它正常工作。)

I'm using the Gson library to serialize/deserialize JSON-objects.

(我正在使用Gson库来序列化/反序列化JSON对象。)

But the library somehow isn't included in the build.

(但是库不知何故不包含在构建中。)

I had created a new project with just a MainActivity .

(我创建了一个只有MainActivity的新项目。)

Copied gson-2.2.3.jar in the /libs folder and added it as a library dependancy(right click->Add as library).

(在/ libs文件夹中复制gson-2.2.3.jar并将其添加为库依赖项(右键单击 - >添加为库)。)

This includes the jar in android studio so it can be referenced from the source files.

(这包括android studio中的jar,因此可以从源文件中引用它。)

When I try to run the project it cannot compile so I added:

(当我尝试运行该项目时,它无法编译,所以我添加:)

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file.

(到de .gradle文件中的依赖项。)

After that it compiles correctly but when running the application I get a ClassDefNotFoundException .

(之后它正确编译,但在运行应用程序时,我得到一个ClassDefNotFoundException 。)

Does anyone know what I'm doing wrong?

(有谁知道我做错了什么?)

  ask by Ozzie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less.

(我已经在同样的事情上苦苦挣扎了好几个小时,试图让Gson jar继续工作。)

I finally cracked it – here are the steps I took:

(我终于破解了它 - 这是我采取的步骤:)

  1. Put the Gson jar (in my case, gson-2.2.4.jar ) into the libs folder

    (把Gson jar(在我的例子中, gson-2.2.4.jar )放到libs文件夹中)

  2. Right click it and hit 'Add as library'

    (右键单击它并点击“添加为库”)

  3. Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)

    (如果你使用的是很多jar文件,请确保compile files('libs/gson-2.2.4.jar')build.gradle文件中(或者compile fileTree(dir: 'libs', include: '*.jar') ))

    Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar') ) in Android Studio 3.0+

    (编辑:在Android Studio 3.0+中使用implementation files('libs/gson-2.2.4.jar') (或implementation fileTree(dir: 'libs', include: '*.jar') ))

  4. Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean . I'm on Mac OS X, the command might be different on your system

    (做一个干净的构建(你可以在Android Studio中做到这一点,但要确保我在终端导航到我的应用程序的根文件夹并键入gradlew clean 。我在Mac OS X上,命令可能不同你的系统)

After I did the above four, it started working fine.

(在我完成上述四个之后,它开始正常工作。)

I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.

(我认为'添加为库'步骤是我之前错过的那个步骤,直到我清理它之前它才行。)

[Edit - added the build.gradle step which is also necessary as others have pointed out]

([编辑 - 添加了build.gradle步骤,这也是必要的,正如其他人指出的那样])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...