简体   繁体   English

将库(Smack)添加到Android Studio项目

[英]Adding library (Smack) to Android Studio project

Since I need to make some changes to the Smack library, I want to add the Smack as a library to my Android Studio project. 由于我需要对Smack库进行一些更改,我想将Smack作为库添加到我的Android Studio项目中。 I've download the ZIP file from the Smack Github repository , and did the following steps -- as I found while searching for solutions 我从Smack Github存储库下载了ZIP文件,并执行了以下步骤 - 正如我在搜索解决方案时发现的那样

(1) Created a new folder libs in my project root folder and copied the extracted and renamed folder /Smack into libs . (1)在我的项目根文件夹中创建了一个新文件夹libs ,并将提取和重命名的文件夹/Smack复制到libs So it's now the following structure: 所以它现在是以下结构:

root/libs/Smack/

(2) I edited my settings.gradle in the root folder to (2)我在根文件夹中编辑了我的settings.gradle

include ':app'
include ':libs:Smack'

(3) Via File -> Project Structure -> Modules app -> Dependencies , I've added :libs:Smack as module dependency (3)通过File -> Project Structure -> Modules app -> Dependencies ,我添加了:libs:Smack作为模块依赖项

(4) I've added the following line to root/app/build.gradle (4)我在root/app/build.gradle添加了以下行

compile project(':libs:Smack')

According to most sources I found online, this should be it. 根据我在网上找到的大多数消息来源,这应该是它。 However, I got an error that Gradle project sync failed . 但是,我收到Gradle project sync failed的错误。 In more detail I get: 我得到更多细节:

/path-to-root/libs/Smack/build.gradle
Error:(42, 0) Project with path ':smack-integration-test' could not be found in project ':libs:Smack'.

When I look into the file and line it says project(':smack-integration-test') . 当我查看文件和行时,它说project(':smack-integration-test') Somehow this cannot be found. 不知何故,这是无法找到的。 However, there is a folder libs/Smack/smack-integration-test . 但是,有一个文件夹libs/Smack/smack-integration-test It looks pretty straightforward, but I seem to miss something crucial here. 它看起来很简单,但我似乎错过了一些关键的东西。

EDIT: Android Studio actually supports importing Gradle Projects: File -> New -> New Module opens a dialog window where I select Import Gradle Project where I have to select the source directory of, here, Smack. 编辑: Android Studio实际上支持导入Gradle项目: File -> New -> New Module打开一个对话窗口,我在其中选择Import Gradle Project ,我必须在这里选择Smack的源目录。 Doing this makes all the correct include statements in settings.gradle . 这样做会在settings.gradle所有正确的include语句。

However, no I get the error: 但是,我没有得到错误:

Error:(386, 0) Could not get unknown property 'clirr' for project ':smack-integration-test' of type org.gradle.api.Project.

Since I'm not familiar enough with Gradle, I have no idea what this might mean, and my Google search also didn't advance me. 由于我对Gradle不够熟悉,我不知道这可能意味着什么,我的Google搜索也没有推进我。

I'm not sure I'd recommend creating a multi-module build out of the two projects. 我不确定我是否建议在这两个项目中创建一个多模块构建。 This will likely not work as expected. 这可能无法按预期工作。 It's likely you'll need to add most/all of the modules in the smack settings.gradle to your own settings.gradle . 您可能需要将smack settings.gradle中的大部分/全部模块添加到您自己的settings.gradle If you really want to do this, I suggest you use prezi-pride which manages this from command line. 如果你真的想这样做,我建议你使用prezi-pride从命令行管理它。

I'm not an android user so not sure which versions(s) of Gradle the android plugin works with. 我不是Android用户所以不确定哪个版本的Gradle android插件可以使用。 If android supports Gradle 3.1+ then you can use the new composite build support . 如果android支持Gradle 3.1+,那么您可以使用新的复合构建支持 This is by far the cleanest solution and I recommend this approach 这是迄今为止最干净的解决方案,我推荐这种方法

Another solution is to build the projects separately and in your project do 另一个解决方案是单独构建项目并在项目中构建项目

compile fileTree(dir: '../smack/project-1/build/lib', include: '*.jar')
compile fileTree(dir: '../smack/project-2/build/lib', include: '*.jar')
// etc

Yet another solution is to publish the smack artifacts to maven local and reference them from there in your project. 另一种解决方案是将smack工件发布到maven local并从项目中引用它们。 This would likely be the second best solution if you can't use composite build 如果您不能使用复合构建,这可能是第二个最佳解决方案

I wouldn't add the project in this way, you can keep it a separate project out of your project folder structure, you would modify it and build it. 我不会以这种方式添加项目,您可以将它作为项目文件夹结构中的单独项目,您可以修改它并构建它。 The simplest way is to copy the build jar file(s) into your project and include it into gradle: 最简单的方法是将构建jar文件复制到项目中并将其包含在gradle中:

For jar, create the libs folder in your project and copy the file there, gradle including works like this: 对于jar,在项目中创建libs文件夹并将文件复制到那里,gradle包括如下所示的工作:

compile fileTree(dir: 'libs', include: ['*.jar']) // for all of the jars in the libs folder
compile files('libs/xyz.jar') // for a specific file

In particular for the Smack project, you can do it with a 特别是对于Smack项目,你可以用一个

gradle assemble

in it's root folder 在它的根文件夹中

The ouput jar(s) are in each specific folder, per example for android I see: ouput jar(s)在每个特定的文件夹中,每个例子对于android我看到:

smack-android\\build\\libs\\ 嫌-的Android \\建造\\库\\

smack-android-extensions\\build\\libs\\ 咂嘴,Android系统的扩展\\建立\\库\\

Not a solution for your problem but a possibly even nicer workaround could be the following: 不是您的问题的解决方案,但可能更好的解决方法可能是以下:

jitpack.io provides you with the ability to add GitHub Repositories to your project as Gradle Dependencies, thus allowing you to omit the process of manually adding the jar. jitpack.io为您提供了将GitHub存储库作为Gradle Dependencies添加到项目中的功能,从而允许您省略手动添加jar的过程。 How to achieve that is best described by JitPack itself. 如何实现这一点最好由JitPack本身描述。 In short: Add the JitPack Repository to your build.gradle: 简而言之:将JitPack存储库添加到build.gradle:

    repositories {
        ...
    maven { url 'https://jitpack.io' }
    }

You can now add the GitHub Repo as a Dependency: 您现在可以添加GitHub Repo作为依赖项:

    dependencies {
        compile 'com.github.igniterealtime:Smack:-SNAPSHOT'
    }

After refreshing your project you should be able to use the API as expected. 刷新项目后,您应该能够按预期使用API​​。

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

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