android库kotlin创建访问部署问题解决了您想要的所有问题

Every Android developer knows how time taking is to create a new project from the scratch so rectify this android development have special architecture called modules architecture. We can reduce time, reuse the code by configuring into your project here is an elaborated guide to a modular architecture.

每个Android开发人员都知道从头开始创建新项目需要花费多少时间,因此纠正此android开发具有特殊的体系结构,称为模块体系结构。 通过配置到您的项目中,我们可以减少时间,重用代码,这里是有关模块化体系结构的详细指南。

Creating a module is pretty simple thanks to the android studio by following steps its do work for you.

借助android studio,按照以下步骤为您工作很容易,因此创建模块非常简单。

To add a new module to your project for a new device, proceed as follows:

要将新模块添加到项目中以使用新设备,请按照以下步骤操作:

  1. Click File > New > New Module.

    单击文件 > 新建 > 新建模块

  2. In the Create New Module window that appears, Android Studio offers the following device modules:

    在出现的“ 创建新模块”窗口中,Android Studio提供以下设备模块:

  • Phone & Tablet Module

    手机和平板电脑模块
  • Wear OS Module

    穿戴式OS模块
  • Android TV Module

    Android TV模块
  • Glass Module

    玻璃模组
  1. Select the module for the device you want, and then click Next.

    选择所需设备的模块,然后单击下一步

  2. In the Configure your new module form, enter the following details:

    在“ 配置新模块”表单中,输入以下详细信息:

  • Application Name: This name is used as the title of your app launcher icon for the new module.

    应用程序名称 :此名称用作新模块的应用程序启动器图标的标题。

  • Module Name: This text is used as the name of the folder where your source code and resources files are visible.

    模块名称 :此文本用作可见源代码和资源文件的文件夹的名称。

  • Package Name: This is the Java namespace for the code in your module. It is added as the package attribute in the module's Android manifest file.

    包名称 :这是模块中代码的Java名称空间。 它被添加为模块的Android清单文件中package属性。

  • Minimum SDK: This setting indicates the lowest version of the Android platform that the app module supports. This value sets the minSdkVersion attribute in the build.gradle file, which you can edit later.

    最低SDK :此设置指示该应用程序模块支持的最低Android平台版本。 此值在build.gradle文件中设置minSdkVersion属性,您可以稍后对其进行编辑。

  1. Then click Next.

    然后单击“ 下一步”

  2. In the Create New Module window that appears, Android Studio offers the following device modules:

    在出现的“ 创建新模块”窗口中,Android Studio提供以下设备模块:

Select the Phone & Tablet Module and then click Next.

选择电话和平板电脑模块,然后单击下一步

  1. Click an activity template with which you want to start, and then click Next. If you don’t need an activity, click Add No Activity, click Finish, and then you’re done.

    单击您要开始使用的活动模板,然后单击下一步 。 如果您不需要任何活动,请单击“不添加活动” ,再单击“ 完成” ,然后完成。

  2. If you chose an activity template, enter the settings for your activity on the Customize the Activity page. Most templates ask for an Activity Name, Layout Name, Title, and Source Language, but each template has activity-specific settings. Click Finish. When you create an app module with an activity template, you can immediately run and test the module on your device.

    如果选择了活动模板,请在“ 定制活动”页面上输入活动的设置。 大多数模板要求输入“ 活动名称” ,“ 布局名称” ,“ 标题 ”和“ 源语言” ,但是每个模板都有特定于活动的设置。 点击完成 。 使用活动模板创建应用程序模块时,可以立即在设备上运行并测试该模块。

Android Studio creates all the necessary files for the new module and syncs the project with the new module gradle files. Adding a module for a new device also adds any required dependencies for the target device to the module’s build file.

Android Studio为新模块创建所有必需的文件,并将项目与新模块gradle文件同步。 为新设备添加模块还会将目标设备所需的所有依赖项添加到模块的构建文件中。

REF : https://developer.android.com/studio/projects/add-app-module

参考: https : //developer.android.com/studio/projects/add-app-module

模块化建筑用法 (Modular Architechture Usage)

Let's talk about the usage of modular architecture. Follow below link to use modular architecture in order to WHERE/WHY you want to choose this approach.

让我们谈谈模块化架构的用法。 单击下面的链接以使用模块化体系结构,以便在WHERE / WHY中选择此方法。

I hope the above details explain about modular and let's see how to consume by the in-app module.

我希望以上详细说明能够说明模块化,并让我们看看如何通过应用内模块进行消费。

Image for post
Photo by Randy ORourke on Unsplash
Randy ORourkeUnsplash拍摄的照片

APP->模块化通讯 (APP -> Modular Communication)

The first point is how to access the modules from your app we can use builder class to configure and get access from the App module.

第一点是如何从您的应用程序访问模块,我们可以使用构建器类来配置和获取来自App模块的访问权限。

APP->模块化通讯 (APP -> Modular Communication)

In Modular define this class

在模块化中定义此类

class LibOptions(val title: String, val body: String /*..more fields */) {
class Builder {
private lateinit var title: String
private lateinit var body: String
//... more fields
fun setTitle(title: String): Builder {
this.title = title
return this;
}
fun setBody(body: String): Builder {
this.body = body;
return this;
}
fun create(): LibOptions {
return LibOptions(title, body)
}
}
}

The above class is a simple builder class that contains details that we want to sent to Modular.

上面的类是一个简单的构建器类,其中包含我们要发送给Modular的详细信息。

Next, Declare Singleton INSTANCE of Library to get access from the app.

接下来,声明Library的Singleton INSTANCE以从应用程序获取访问权限。

For singleton instance creation we can use by following pattern.

对于单例实例创建,我们可以使用以下模式。

open class SingletonHolder<out T: Any, in A>(creator: (A) -> T) {
private var creator: ((A) -> T)? = creator
@Volatile private var instance: T? = null
fun getInstance(arg: A): T {
val i = instance
if (i != null) {
return i
}
return synchronized(this) {val i2 = instance
if (i2 != null) {
i2
} else {
val created = creator!!(arg)
instance = created
creator = null
created
}}}
}

Want to know more about singleton instance follow below link.

想更多地了解单例实例,请点击以下链接。

Let's create Instance LibManger,

让我们创建实例LibManger,

import android.content.Context
import androidx.appcompat.app.AlertDialog
class LibManager constructor(val context: Context) {
init {
// do some stuff
}
fun show(options: LibOptions) {
val builder = AlertDialog.Builder(context)
builder.setTitle(options.body).setMessage(options.body)builder.setNegativeButton("Close") { dialog, which -> dialog?.dismiss() }options.body
}
companion object : SingletonHolder<LibManager, Context>(::LibManager)
}

Now we can call this method in the App Module using this singleton class.

现在,我们可以使用此单例类在App Module中调用此方法。

LibManager.getInstance(this).show(LibOptions.Builder().setBody("Alert Dialog BODY").setTitle("Alert").create())

When we create an android module in an android studio it automatically add the below section in App.Gradle

当我们在android studio中创建android模块时,它会自动在App.Gradle中添加以下部分

implementation project(":library")

We can easily reuse this module library in every project by using

通过使用以下命令,我们可以轻松地在每个项目中重用此模块库

File -> New -> Import Module

文件->新建->导入模块

But the problem is the whole module source code must be in the same system to use in mutiple project. For that we can Deploy library to maven, jCenter repository then we can consume it.

但是问题是整个模块的源代码必须在同一系统中才能在多个项目中使用。 为此,我们可以部署到Maven,jCenter存储库中,然后就可以使用它了。

For example, In App.Gradle

例如,在App.Gradle中

implementation 'com.domain.library:alert-library:1.0.0'

Deploy & Release

部署与发布

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.

Android库在结构上与Android应用程序模块相同。 它可以包含构建应用程序所需的所有内容,包括源代码,资源文件和Android清单。 但是,Android库不是编译为在设备上运行的APK,而是编译为Android存档(AAR)文件,您可以将其用作Android应用程序模块的依赖项。 与JAR文件不同,AAR文件可以包含Android资源和清单文件,除了Java类和方法外,清单文件还允许您捆绑诸如布局和可绘制对象之类的共享资源。

Image for post
Photo by Vidar Nordli-Mathisen on Unsplash
Vidar Nordli-MathisenUnsplash拍摄的照片

There are plenty of platforms for hosting your Android library and create its Gradle or maven dependency. You can choose over JitPack or JCenter or Maven Central.

有很多平台可以托管您的Android库并创建其Gradle或maven依赖项。 您可以选择JitPackJCenterMaven Central

Ref: Follow this link to configure the publication.

参考:单击此链接可以配置发布。

JCenter

中心

It's quite a time to set up configurations mostly preferable for organization because source code kept in private.

现在是时候设置最适合组织使用的配置了,因为源代码是私有的。

JitPack

JitPack

One of the easiest way to publish no need to create an account and not a lot of configurations.

无需创建帐户且无需大量配置即可发布的最简单方法之一。

问题与修复 (Problems & Fixs)

The above example didn’t use any external library in the library module. For example if we add some library like stylable toast in library when publish happens its also need to ship with package.

上面的示例在库模块中未使用任何外部库。 例如,如果我们在发布发生时在库中添加了诸如样式化吐司之类的库,那么它也需要随包一起提供。

Simply,

只是,

LibraryA is consumed LibraryB when I use LibraryA in App its doesn't know about LibraryB so it will cause runtime exception(No Class Found Exception).

当我在App中使用LibraryA时,LibraryA被消耗了LibraryB,它不了解LibraryB,因此会导致运行时异常(未找到类异常)。

This is also known as Transitive Dependency

这也称为 传递依存关系

传递依存关系 (Transitive Dependency)

A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

组件的变体可以依赖于其他模块才能正常工作,即所谓的传递依赖。 托管在存储库上的模块的发行版可以提供元数据来声明这些可传递依赖项。 默认情况下,Gradle自动解决传递依赖。 声明依赖约束可以影响可传递依赖的版本选择。

To solve this we need to add the “pom” file which contains all dependency details.

为了解决这个问题,我们需要添加包含所有依赖项详细信息的“ pom”文件。

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.

项目对象模型或POMMaven中的基本工作单元。 这是一个XML 文件 ,其中包含有关项目的信息以及Maven用于构建项目的配置详细信息。 它包含大多数项目的默认值。

First, We need to import the external library in Gradle by using api or implementation.

首先,我们需要使用api实现将外部库导入Gradle

For example, In Module.Gradle

例如,在Module.Gradle中

dependencies {
//... api 'com.muddzdev:styleabletoast:2.2.3' // retrofit
api 'com.google.code.gson:gson:2.8.6'
api 'com.squareup.retrofit2:retrofit:2.6.1'
api 'com.squareup.retrofit2:converter-gson:2.6.1'
//...
}

Then add this line end of Module.Gradle

然后在Module.Gradle的该行末尾添加

publishing {publications {aar(MavenPublication) {groupId packageName
version = libraryVersion
artifactId project.getName()
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
pom.withXml {def dependenciesNode = asNode().appendNode('dependencies')
// Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.api.allDependencies.each {def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)}
}
}
}
}

That's it, there is no need to generate pom file on own above code automatically get “api’ and add it to pom file.

就是这样,不需要在上面的代码上生成pom文件,它会自动获取“ api ”并将其添加到pom文件中。

That's all for PART 1.

这就是第1部分的全部内容。

Let's see in PART 2.

让我们在第2部分中看到。

Reference:

参考:

翻译自: https://medium.com/swlh/android-library-kotlin-creation-access-deploy-problems-fixes-everything-you-want-to-c1a1701f0e8f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值