ServiceProviderBuilder Annotations

Service provider builder for android module Interface

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

com.github.tablebird
ArtifactId

ArtifactId

service-provider-builder-annotations
Last Version

Last Version

0.2.4
Release Date

Release Date

Type

Type

jar
Description

Description

ServiceProviderBuilder Annotations
Service provider builder for android module Interface
Project URL

Project URL

https://github.com/tablebird/ServiceProviderBuilder/
Source Code Management

Source Code Management

https://github.com/tablebird/ServiceProviderBuilder/

Download service-provider-builder-annotations

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.tablebird/service-provider-builder-annotations/ -->
<dependency>
    <groupId>com.github.tablebird</groupId>
    <artifactId>service-provider-builder-annotations</artifactId>
    <version>0.2.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.tablebird/service-provider-builder-annotations/
implementation 'com.github.tablebird:service-provider-builder-annotations:0.2.4'
// https://jarcasting.com/artifacts/com.github.tablebird/service-provider-builder-annotations/
implementation ("com.github.tablebird:service-provider-builder-annotations:0.2.4")
'com.github.tablebird:service-provider-builder-annotations:jar:0.2.4'
<dependency org="com.github.tablebird" name="service-provider-builder-annotations" rev="0.2.4">
  <artifact name="service-provider-builder-annotations" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.tablebird', module='service-provider-builder-annotations', version='0.2.4')
)
libraryDependencies += "com.github.tablebird" % "service-provider-builder-annotations" % "0.2.4"
[com.github.tablebird/service-provider-builder-annotations "0.2.4"]

Dependencies

compile (1)

Group / Artifact Type Version
androidx.annotation » annotation jar 1.0.0

Project Modules

There are no modules declared in this project.

Service Provider Builder

English README

基于SPI(Service Provider Interface)思想获取服务接口的实现,使用注解生成构建器代码,便于获取服务接口的实现

  • 在接口上使用@ServiceProvider声明服务接口
  • 在服务接口的实现上使用@ServiceImplementation指定需要构建的服务
  • 在服务的静态方法上使用@BuildService指定构建服务的方式
  • 消除了需要手动添加META-INF.services指定SPI接口与实现

开始使用

安装

在模块的build.gradle添加:

dependencies {
    implementation 'com.github.tablebird:service-provider-builder:0.2.4'
    annotationProcessor 'com.github.tablebird:service-provider-builder-compiler:0.2.4'
}

如果使用Kotlin则添加:

apply plugin: 'kotlin-kapt'
dependencies {
	...
	implementation 'com.github.tablebird:service-provider-builder:0.2.4'
	kapt 'com.github.tablebird:service-provider-builder-compiler:0.2.4'
}

application模块build.gradle前添加如下代码可以在编译期提前注册ServiceBuilder加快运行速度:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        google()
    }

    dependencies {
        classpath "com.github.tablebird:service-provider-builder-gradle-plugin:0.2.4"
    }
}
apply plugin: 'com.github.tablebird.serviceproviderbuilder'

示例代码

服务接口定义(公共模块)

@ServiceProvider(ServiceProviderPolicy.SINGLE)
public interface IService {
    String getName();
}

ServiceProviderPolicy.SINGLE代表服务的实现是唯一的,不允许存在多个,默认使用ServiceProviderPolicy.MULTIPLE允许多个实例

服务具体实现(具体业务模块)

Java

@ServiceImplementation
class Service implements IService {
    private String name = "Build service";
    private static final Service INSTANCE = new Service();
    @BuildService
    public static Service getInstance() {
        return INSTANCE;
    }
    @Override
    public String getName() {
        return name;
    }

Kotlin

@ServiceImplementation
internal Service private constructor() : IService {
    companion object {
        @BuildService
        @JvmStatic
        fun getInstance() = Holder.INSTANCE
    }

    private object Holder {
        val INSTANCE = Service()
    }
}

服务的构建会优先寻找@BuildService指定的静态方法,如果未指定则会使用构造函数 服务的实现建议使非公开的,避免直接被外部引用,构建器会负责找到服务的实现

服务接口的调用

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IService iService = ServiceProviderBuilder.buildSingleService(IService.class);
        TextView textView = findViewById(R.id.text);
        if (iService != null) {
            textView.setText(iService.getName());
        }
    }
}

混淆问题

需要添加如下内容(使用apply plugin: 'com.github.tablebird.serviceproviderbuilder' 后代码可以进行代码混淆,无需添加如下忽略):

-keep class com.tablebird.serviceproviderbuilder.*
-keep public class * extends com.tablebird.serviceproviderbuilder.ServiceBuilder{
}
-keep @com.tablebird.serviceproviderbuilder.ServiceProvider class * {}

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0