@wuseal
2018-04-10T06:06:16.000000Z
字数 3138
阅读 754
组件化
APP
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
maven{
url "http://dpapi.irs01.com:8081/nexus/content/repositories/internal/"
credentials {
username = 'username'
password = 'password'
}
}
maven {
url 'http://dpapi.irs01.com:8081/nexus/content/groups/public/'
}
}
}
把账号密码替换为自己的账号密码
在代码工程的build.gradle文件中的dependencies内添加如下依赖
implementation 'cn.com.iresearch.android:componentizationsupporter:0.1'
就以实现toast自己组件状态Ok提示为例,如下同步实现。
class MyComponent :IComponent {
override fun execute(commandLine: String, vararg commandLineArgs: Any): Any {
if (commandLine == "toast") {
val context =commandLineArgs[0] as Context
Toast.makeText(context,"I am Ok",Toast.LENGTH_LONG).show()
return true
}
return false
}
override fun executeAsync(commandLine: String, vararg commandLineArgs: Any, callback: (response: Any) -> Unit) {
}
override fun getComponentName(): String {
return "Demo.MyComponent"
}
}
在组件module的AndroidManifest文件中的Application标签中添加组件注册信息
<application>
<meta-data
android:name="IRComponentMyComponent"
android:value="android.iresearch.com.cn.mycomponent.MyComponent" />
</application>
注意:
name
对应的声明值必须以IRComponent开头,且尽量保证唯一性,避免与其它组件声明Key的冲突。
value
对应组件的全路径类名信息,用来反射生成实例组件对象用的。
plugins {
id "com.github.dcendents.android-maven" version "2.0"
}
group = "cn.com.iresearch.android.component"
version = "1.0"
android {...}
settings.gradle
文件中添加如下代码完成修改。
project(':module的名字').name = '新的库名字'
比如:
project(':library').name = 'mycomponent'
build.gradle
的最后一行添加如下内容
uploadArchives {
repositories.mavenDeployer {
repository(url: "http://dpapi.irs01.com:8081/nexus/content/repositories/internal/") {
authentication(userName: "username", password: "password")
}
}
}
在完成组件开发要发版本的时候进行如下操作完成发版本
1.在开发工具右边的gradle面板中找到当前module->Tasks->other->install,并双击install。完成上传前的打包操作
2.继续在开发工具右边的gradle面板中找到当前module->Tasks->upload->uploadArchives,双击uploadArchives
完成组件的上传。
至此,自己的组件化工程就可以被其它人使用了。
就以刚才上传的组件的使用为例
在工程总的构建脚本中添加如下代码
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
maven{
url "http://dpapi.irs01.com:8081/nexus/content/repositories/internal/"
credentials {
username = 'username'
password = 'password'
}
}
maven {
url 'http://dpapi.irs01.com:8081/nexus/content/groups/public/'
}
}
}
在app工程中的build.gralde脚本中的dependencies添加如下依赖
implementation 'cn.com.iresearch.android:componentizationsupporter:0.1'
implementation 'cn.com.iresearch.android.demo.component:mycomponenttest:1.0'
然后同步下构建脚本
在应用工程的Application类中执行组件框架的初始化
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
ComponentRouter.init(this)
}
}
按照组件提供方提供的使用说明文档方法执行相应的组件功能调用,就以刚才上传的示例组件为例,执行如下操作即可调用组件功能
val component = ComponentRouter.getComponent("Demo.MyComponent")
component.execute("toast",this)
如一切正常,则可以弹出I am Ok
提示信息
将App module中buid.gradle文件中的依赖方式设置成直接工程依赖组件库,并按上文执行调用,则可以打包运行到设备上进行单独的组件调试及运行。
即把之前的依赖:
implementation 'cn.com.iresearch.android.demo.component:mycomponenttest:1.0'
更换成
implementation project(':mycomponenttest')
这种形式就可以实现边修改边运行调试了
若需要再依赖其它的组件或模块,则可以直接在app module build.gradle中添加即可。
附该Demo工程地址 Demo