Android Studio:多模块项目中外部 JAR 的链接 [英] Android Studio: Link of external JAR in a multi modules project

查看:31
本文介绍了Android Studio:多模块项目中外部 JAR 的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Android 应用程序(在 Android Studio 上运行).它由2个模块组成:- 有一个低级纯 java 模块(我们称之为模块 A).- 最重要的是模块 B,它是 Android 应用程序.它依赖模块A进行一些处理.

I have an Android application (running on Android Studio). It is made of 2 modules: - There is a low level pure java module (let’s call it module A). - On top of it, there is the module B which is the Android application. It relies on moduleA for some processings.

这工作正常.

我现在需要模块 A 来调用外部库.我已经下载了 JAR 文件并将其放在 moduleA/libs 文件夹中.这个libs文件夹是在moduleA的gradle依赖中引用的,所以编译没问题.但是,运行时有一个异常:

I now need the module A to call an external Library. I have downloaded the JAR file and put it in moduleA/libs folder. This libs folder is referenced in gradle dependency of moduleA so the compilation is OK. However, there is an exception at runtime:

致命异常引起:java.lang.ClassNotFoundException:在路径上找不到类XXXX":DexPathList

FATAL EXCEPTION Caused by: java.lang.ClassNotFoundException: Didn't find class "XXXX" on path: DexPathList

我看到APK没有包含JAR文件,所以出现这个异常是正常的.

I have seen that the APK doesn’t contain the JAR file so it is normal that this exception occurs.

如果我在 moduleB/libs 中复制相同的 JAR 文件,那么它可以正常工作,但我在项目中有两次 JAR 文件!处理这个问题的干净解决方案是什么?我想它可以用 gradle 解决,但我不知道如何.

If I copy the same JAR file in moduleB/libs, then it works fine but I have the JAR file two times in the project! What is the clean solution to handle this? I guess that it can be solved with gradle but I don't know how.

非常感谢奥利维尔

推荐答案

我已经能够解决这个问题.这篇关于 Gradle 的阅读对我帮助很大:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project

I have been able to fix this issue. This reading about Gradle helped me a lot: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project

这是我所做的:我没有将 JAR 文件放在 moduleA/libs 文件夹中,而是通过单击项目然后右键单击 -> 新建 -> 模块将 JAR 文件导入到 Android Studio 中.然后我点击Import .JAR/.AAR package".这创建了一个包含 JAR 文件和 gradle 脚本的模块.

Here is what I have done: Instead of putting the JAR file in moduleA/libs folder, I have imported the JAR file in Android Studio by clicking on the project then right click -> new -> module. I then clicked on "Import .JAR/.AAR package". This created a module containing the JAR file + a gradle script.

然后,在 moduleA 的 gradle 脚本中,我在依赖项中添加了这个:编译项目(路径:':name_of_the_jar_file')

Then, in moduleA’s gradle script, I have added this in the dependencies: compile project(path: ':name_of_the_jar_file')

我重建了所有,并且可以正常工作.JAR 文件现在存在于 APK 中,并且在运行时不再发生崩溃.

I rebuilt all and it works. The JAR file is now present in the APK and there is no more crash at runtime.

这篇关于Android Studio:多模块项目中外部 JAR 的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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