如何在android studio中使用库项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20377591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:46:28  来源:igfitidea点击:

How to use a library project in android studio

androidandroid-gradle-pluginlibrary-project

提问by dmSherazi

I have been trying to add this library projectto my existing project in Android Studio. It's the first time I am going to use a library project and I am having tough time. I have looked around for many ways to do this from tutorials and posts around but couldn't get it done.

我一直在尝试将此库项目添加到我在 Android Studio 中的现有项目中。这是我第一次使用图书馆项目,我遇到了困难。我已经从教程和帖子中寻找了很多方法来做到这一点,但无法完成。

回答by owe

I'm not sure if it's already possible to add a library project via the IDE (-> without any problems). I do this by configuring the gradle files of my project like this:

我不确定是否已经可以通过 IDE 添加一个库项目(-> 没有任何问题)。我通过像这样配置我的项目的 gradle 文件来做到这一点:

  • create a folder in your root project directory named libs
  • copy the folder datetimepicker-libraryto libs
  • add this library in your settings.gradlewith the following command:

    include ':libs:datetimepicker-library'
    
  • go to your build.gradlefile of your AppProjectand add the following line to your dependencies:

    implementation project(':libs:datetimepicker-library')
    
  • at least you have to sync your gradle files: Tools -> Android -> Sync Project with Gradle Files

  • 在您的项目根目录中创建一个名为的文件夹 libs
  • 将文件夹复制datetimepicker-librarylibs
  • settings.gradle使用以下命令在您的库中添加此库:

    include ':libs:datetimepicker-library'
    
  • 转到您的build.gradle文件AppProject并将以下行添加到您的dependencies

    implementation project(':libs:datetimepicker-library')
    
  • 至少你必须同步你的 gradle 文件:工具 -> Android -> 将项目与 Gradle 文件同步

Please try this. If you get errors please post the log file.

请试试这个。如果出现错误,请发布日志文件。

回答by dmSherazi

I did it this way ,

我是这样做的

  1. go to project Structurefrom Filemenu
  2. Select modulesfrom the left pane
  3. press on `+'
  4. complete the new module wizard steps Make sure to make the module package name same as the module you want to add
  5. again open Project structureas in step 1
  6. select your project from the module list and on the right select dependencies
  7. Click on the +icon from right
  8. select module dependency and then select the newly added module.
  9. copy files of the library project to the new directory created as module
  10. Done
  1. project StructureFile菜单转到
  2. modules从左侧窗格中选择
  3. 按“+”
  4. 完成新模块向导步骤 Make sure to make the module package name same as the module you want to add
  5. 再次Project structure像步骤 1 一样打开
  6. 从模块列表中选择您的项目,然后在右侧选择依赖项
  7. 点击+右边的图标
  8. 选择模块依赖,然后选择新添加的模块。
  9. 将库项目的文件复制到作为模块创建的新目录中
  10. 完毕

Step 1:

第1步:

step 1

第1步

Step 2:

第2步:

Step 2!

第2步

Step 3:

第 3 步:

Step 3

第 3 步

Step 4:

第四步:

**Step 4:**

**第四步:**

....

....

Step 6 & 7:

第 6 步和第 7 步:

enter image description here

在此处输入图片说明

Step 8:enter image description here

第 8 步:在此处输入图片说明

回答by Suragch

The other answers make it seem more difficult than it usually is. Just add a single compileline to your dependencies section of the app's build.gradle file.

其他答案使它看起来比通常更困难。只需compile在应用的 build.gradle 文件的依赖项部分添加一行即可。

In this case it is

在这种情况下是

dependencies {
    // ...
    compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2'
}

Gradle prompted me to sync and after I did so the library was automatically downloaded into my project.

Gradle 提示我同步,在我同步之后,库会自动下载到我的项目中。

回答by Kenneth Argo

The problem with solutions listed above is that you will end up with a clone of the library project in the app using the library. This complicates making updates to the library because you need to remove and re-import the module when you make changes.

上面列出的解决方案的问题是,您最终将使用库在应用程序中克隆库项目。这使库更新变得复杂,因为您需要在进行更改时删除并重新导入模块。

The solution I found can use the library project directlyfrom outside folder and it does not clone the original library files.

我找到的解决方案可以直接从外部文件夹使用库项目,它不会克隆原始库文件。

The solution is easy...

解决方法很简单...

In settings.gradleadd the following lines:

settings.gradle 中添加以下几行:

include ':commonwidgets'
project (':commonwidgets').projectDir = new File(settingsDir, '../SharedWidgets/commonwidgets')

and in the build.gradledependencysection add:

并在build.gradledependency部分添加:

implementation project(path: ':commonwidgets')

Note: commonwidgetsis the name of my library, you should replace the name and path according to your library name and path.

注意:commonwidgets是我的库名,根据你的库名和路径替换名字和路径。

I hope this helps someone...

我希望这可以帮助别人...

I really would have liked to have the library import from GitHub bit I refuse to pay $9/month for jetpack for private libraries.

我真的很想从 GitHub 导入库,我拒绝为私人库的喷气背包支付 9 美元/月的费用。