在多个Android Studio项目中重用相同的代码 [英] Reusing the same code across multiple Android Studio projects

查看:451
本文介绍了在多个Android Studio项目中重用相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码想在多个不同的项目中使用.假设这是一些电子商务代码,用于处理付款和购物车之类的事情.

I have some code I'd like to use across multiple different projects. Let's say it's some e-commerce code that handles things like payments and shopping carts.

在不同项目中复制粘贴所有内容似乎是低效率且危险的.而且,如果我在核心电子商务模块中添加了一项功能或修复了一个错误,我希望这一更改也能反映在其他使用此功能的项目中.

It seems inefficient and dangerous to copy-paste everything across different projects. And if I add one feature or fix one bug in the core e-commerce module, I'd like that change to be reflected in other projects using it too.

我还想重用一些活动,片段和适配器.

I would also like to re-use some of the Activities, Fragments, Adapters too.

对此有什么好的方法?

推荐答案

当我们有一个库项目需要与本地计算机上的每个项目共享时,我们可以使用Maven.

When we have a library project that needs to be shared to every project on a local computer, we can make use of Maven.

A.这是您将在图书馆中执行该项目的步骤:

A. Here the step in your library that we will you for the project:

  1. 从Android Studio创建一个库项目.

  1. Make a library project from Android Studio.

将Gradle Android Maven插件添加到 root build.gradle

Add Gradle Android Maven plugin to root build.gradle

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
  }
}

  • 在库build.gradle中为步骤1添加Apply插件. (不是root build.gradle):

  • Add apply plugin for step 1 in your library build.gradle. (NOT root build.gradle):

    apply plugin: 'com.android.library'
    apply plugin: 'com.github.dcendents.android-maven'
    

  • 在apply插件之后添加以下内容,此行可在添加到项目时确定您的库:

  • Add the following after the apply plugin, this line to determine your library when adding to project:

    group = 'com.yourpackage.yourlibrary'
    version = '1.0'
    

  • 在您的settings.gradle中添加以下代码:

  • Add the following code in your settings.gradle:

    rootProject.name = 'yourlibrary' 
    

  • 然后使用以下命令将其发布到本地专家:

  • Then publish it to your local maven with:

    ./gradlew install
    

    或者您可以在Android Studio中使用gradle选项.

    Or you can use gradle option in Android Studio.

    您的库将安装在$ HOME/.m2/repository中.请记住,要使用该库,您需要像这样添加:

    Your library will be installed in $HOME/.m2/repository. Remember that to use the library you need to add like this:

      Groupid:artifactid:versionid
    

    Artifactid将是您的库的软件包名称.

    Artifactid will be package name of your library.

    B.这是您的项目中使用库的步骤:

    1. 在您的root build.gradle中添加以下代码:

    1. Add the following code in your root build.gradle:

      mavenLocal() // for local maven.
    

  • 这是为了获取我们在步骤A中安装的本地库Maven

    This for getting the local library maven that we have installed in step A

    1. 然后在您的 app 项目中,渐变,为库添加编译:

    1. Then in your app project.gradle, add compile for the library:

    compile 'com.yourpackage.yourlibrary:yourlibrary:1.0'
    

    了解更多:

    • Gradle: How to publish a Android library to local repository
    • https://github.com/dcendents/android-maven-gradle-plugin
    • https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en

    这篇关于在多个Android Studio项目中重用相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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