To cut to the chase, in modular development, most students will use interfaces as communication protocols for data communication between modules. The problems to be faced are as follows:

  • Who maintains the interface?

    This problem is simple and maintained by the module that provides the service.

  • How is the interface exposed?

    Jar and publish to Maven.

  • Where is the interface maintained?

    Now can refer to the scheme has three kinds: one. The interfaces of all related modules are maintained in one module. 2. The interface of each module is maintained in a new module built by itself and corresponding to each other through naming rules; Third, like wechat’s. API scheme, it uses special rules mixed in their respective modules.

    If you follow up with the first problem, scenario 1 seems a little difficult to determine the source module for the interface. In scheme 2, interface modules will be multiplied, and it is easy to see that a module contains only one interface class. Scheme 3 needs to customize related plug-ins, which is inconvenient and not flexible enough to create interfaces.


Enter MIS!! Next, we will introduce the simple use of MIS and the principle behind it.

MIS

Module Interface Service

MIS evolved from the. API solution of wechat, mainly to solve the problem of how to maintain its exposed interface (including packaging and publishing) in a module, rather than separating the interface and interface implementation into two different modules.

Usage

Reference mis plug-in

Add the CLASspath of the MIS plug-in to the build.gradle of the root project:

buildscript {
    dependencies {
        ...
        classpath 'com. Eastwood. Tools. Plugins: mis: 1.3.5'}}Copy the code

Add the MIS plug-in to the build.gradle module:

. apply plugin:'mis'
Copy the code

Creating the MIS directory

After Gradle Sync, create mis folder in Java sibling directory

Define interfaces and implement interface services

Create the corresponding package name, interface class, and data Model directly under the MIS folder. And implement interface service under Java folder.

Configure the publication corresponding to the MIS

mis {
    publications {
        main {
            groupId 'com.eastwood.demo'
            artifactId 'library-sdk'
            // version '1.0.0 - the SNAPSHOT'

            dependencies {
                compileOnly 'com. Google. Code. Gson: gson: 2.8.1'}}}... }Copy the code
  • “Main” is “main” in SRC /main/ Java. “Main” can be “Build Types” and “Product flavors”, which is “mis” in the corresponding directory. For example, SRC /debug/mis corresponds to SRC /debug/ Java.

  • GroupId, artifactId, version correspond to Maven’s GAV. Do not set version for the initial configuration. Set version for publishing to Maven.

  • In Dependencies, you can declare third-party libraries that you need to compile and run the MIS. Only compileOnly and implementation are supported.

Release to Maven

mis {
    publications {
        main {
            groupId 'com.eastwood.demo'
            artifactId 'library-sdk'
            version '1.0.0 - the SNAPSHOT'. } } repositories { maven { url"http://***"
            credentials {
                username '* * *'
                password '* * *'}}}... }Copy the code
  • You need to set version when publishing.

  • Maven-publish is a repository for the # maven publish Plugin

After Gradle Sync, open Gradle Tasks View and select publishMis[…] PublicationToMavenRepository release missions.

The publishMis […]. PublicationToMavenLocal is published to local Maven. If you are using maven locally, add mavenLocal() to your root project’s build.gradle, for example:

allprojects {
    repositories {
        google()
        jcenter()
        mavenLocal()
    }
}
Copy the code

Q&A

1. Will classes in the MIS directory participate in compilation?

Don’t. Although classes in the MIS directory can be referenced directly by classes in the Java directory, they do not participate in compilation. What is involved in compilation is the JAR packages generated by the MIS directory, which are located under the current project. Gradle/MIS. During the current Sync&Build project, the MIS plug-in compiles and packages the MIS directories configured with the publication to generate and rely on the JAR package.

Classes in the MIS directory can be referenced directly by classes in the Java directory because the MIS directory is set to the sourceSets aidl SRC directory, which Android Studio has special support for.

2. Without Maven server, all modules are under the same project. How can other modules reference interfaces?

No version is set for the publication. Declare dependencies with misPublication, for example:

dependencies {
    ...
    implementation misPublication('com.eastwood.demo:library-sdk')}Copy the code

MisPublication works by automatically looking for jar packages provided by mis under the current project. Gradle/MIS. If so, use the corresponding JAR package provided by MIS; If version is not available and specified, the JAR package on Maven is used.

3. After publishing the interface to Maven, other modules declare dependencies through misPublication, and that JAR contains.gradle/misUnder or on Maven?

When an interface is published to Maven, the jar packages under.gradle/mis are removed, and the module where the interface resides uses the JAR packages on Maven according to the GAV set in publication. If other modules declare dependencies on it through misPublication, for example:

dependencies {
    ...
    implementation misPublication('com.eastwood.demo:library-sdk'// implementation misPublication('com. Eastwood. Demo: library - SDK: 1.0.0 - the SNAPSHOT')}Copy the code

Regardless of whether a version is set in misPublication, the JAR package on Maven will be used with the same version as the GAV in the module publication where the interface resides.

When mis directory class essentially modified (generate different jar package), in the current engineering Sync&Build, will be in. Gradle/mis to regenerate the jar package, interface module whether publication of set version, Use jar packages under gradle/mis. If other modules declare dependencies on them through misPublication, jar packages under. Gradle /mis will be used regardless of the version set in misPublication.

4. Why can’t you find it in Gradle Tasks ViewpublishingRelated publishing Task?

For your first release, check if the corresponding publication already has a version, and if any related repositories are added.


The last

MIS has been uploaded to Github, welcome star to communicate. QQ 1056453754

Github.com/EastWoodYan…