# Dart使用说明
# 废弃
[](https://maven-badges.herokuapp.com/maven-central/com.f2prateek.dart/dart) [](http://android-arsenal.com/details/1/1444)[](https://travis-ci.org/f2prateek/dart)
============
Extra "injection" library for Android which uses annotation processing to
generate code that does direct field assignment of your extras.
Dart is inspired by [ButterKnife][1].
```java
class ExampleActivity extends Activity {
@InjectExtra String extra1;
@InjectExtra int extra2;
@InjectExtra User extra3; // User implements Parcelable
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
Dart.inject(this);
// TODO Use "injected" extras...
}
}
```
Simply call one of the `inject()` methods, which will delegate to generated code.
You can inject from an Activity (which uses its intent extras), Fragment (which uses its arguments)
or directly from a Bundle.
The key used for the extra will be the field name by default. However, it can be set manually as a parameter in the annotation: `@InjectExtra("key")`
Optional Injection
------------------
By default all `@InjectExtra` fields are required. An exception will be thrown if the target extra cannot be found.
To suppress this behavior and create an optional injection, add the `@Nullable` annotation to the field or method.
Any annotation with the class name `Nullable` is respected, including ones from the support library annotations and ButterKnife.
```java
@Nullable @InjectExtra String title;
```
Default Values
--------------
You can assign any values to your fields to be used as default values, just as you would in regular "injection"-free code.
```java
@InjectExtra String title = "Default Title";
```
This value will be overridden after you call `inject()`. Remember to use the `@Nullable` annotation, if this injection is optional.
Bonus
-----
Also included is a `get()` method that simplifies code to retrieve extras from a Bundle.
It uses generics to infer return type and automatically perform the cast.
```java
Bundle bundle = getIntent().getExtras(); // getArguments() for a Fragment
User user = Dart.get(bundle, "key"); // User implements Parcelable
```
Henson
------
In Dart 2.0, we added an annotation processor that helps you to navigate between activities.
The new module is called Henson (after [Matthew Henson](https://en.wikipedia.org/wiki/Matthew_Henson), the African-American Arctic explorer that first reached the North Pole) :
For the sample activity mentioned above, Henson will offer a DSL to navigate to it easily :
```java
Intent intent = Henson.with(this)
.gotoExampleActivity()
.extra1("defaultKeyExtra")
.extra2(2)
.extra3(new User())
.build();
startActivity(intent);
```
Of course, you can add any additional extra to the intent before using it.
The DSL will be generated for all classes which contain `@InjectExtra` fields. If you want to extend it to other classes, use the `@HensonNavigable` annotation.
```java
@HensonNavigable
class AnotherActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
}
```
The Henson annotation processor will generate the `Henson` navigator class (used above) in a package that is :
* either the package specified by the `dart.henson.package` annotation processor option
* or if no such option is used, in the common package of all annotated activities. See the Javadoc of `HensonExtraProcessor` for more details.
Bonus
-----
As you can see from the examples above, using both Dart & Henson not only provides a very structured generated navigation layer and convenient DSLs; it also allows to wrap/unwrap parcelables automatically.
Parceler
-------------------------
Dart 2.0 offers a built-in support for [Parceler](https://github.com/johncarl81/parceler). Using Parceler with Dart 2 is optional.
If you use Parceler, Dart will automatically detect @Parcel annotated beans (pojos), or collections of them, and wrap them using the Henson DSL and unwrap them when they are injected via Dart.
```java
@Parcel
public class ParcelExample {
...
}
```
```java
class OneMoreActivityActivity extends Activity {
@InjectExtra ParcelExample extra;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
Dart.inject(this);
// TODO Use "injected" extras...
}
}
```
```java
Intent intent = Henson.with(this)
.gotoOneMoreActivityActivity()
.extra(new ParcelExample())
.build();
startActivity(intent);
```
Parceler usage is optional and will take place only when Parceler is present in the classpath.
When available, Parceler will be used to parcelize collections instead of serializing them, in order to gain speed.
ProGuard
--------
If ProGuard is enabled be sure to add these rules to your configuration:
```
-dontwarn com.f2prateek.dart.internal.**
-keep class **$$ExtraInjector { *; }
-keepclasseswithmembernames class * {
@com.f2prateek.dart.* <fields>;
}
#for dart 2.0 only
-keep class **Henson { *; }
-keep class **$$IntentBuilder { *; }
#if you use it
#see Parceler's github page
#for specific proguard instructions
```
Download
--------
For Dart 2.x :
Gradle:
```groovy
compile 'com.f2prateek.dart:dart:(insert latest version)'
provided 'com.f2prateek.dart:dart-processor:(insert latest version)'
```
or maven
```xml
<dependency>
<groupId>com.f2prateek.dart</groupId>
<artifactId>dart</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.f2prateek.dart</groupId>
<artifactId>dart-processor</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
```
And for using Henson :
Gradle:
```groovy
compile 'com.f2prateek.dart:henson:(insert latest version)'
provided 'com.f2prateek.dart:henson-processor:(insert latest version)'
```
When using Henson, as Android Studio doesn't call live annotation processors when editing a file, you might prefer using the [apt Android Studio plugin](https://bitbucket.org/hvisser/android-apt). It will allow you to use the Henson generated DSL right away when you edit your code.
The Henson annotation processor dependency would then have to be declared within the apt scope instead of provided.
or maven
```xml
<dependency>
<groupId>com.f2prateek.dart</groupId>
<artifactId>henson</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.f2prateek.dart</groupId>
<artifactId>henson-processor</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
```
For Dart 1.x :
Gradle:
```groovy
compile 'com.f2prateek.dart:dart:2.0.1'
```
Maven:
```xml
<dependency>
<groupId>com.f2prateek.dart</groupId>
<artifactId>dart</artifactId>
<version>2.0.1</version>
</dependency>
```
License
-------
Copyright 2013 Jake Wharton
Copyright 2014 Prateek Srivastava (@f2prateek)
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.
[1]: http://jakewharton.github.io/butterknife/
[2]: http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.f2prateek.dart&a=dart&v=LATEST
- 发布aar到maven仓库
- svn或gitlab代码上传规范
- maven仓库管理
- 自动构建
- Android自动构建
- Android-jenkins发布
- 规范
- Android
- Android组件模块文档
- 基础业务模块
- 智能导诊
- 科室医生
- 医院导航
- 健康资讯
- 健康百科
- 个人中心
- 外链模块
- 微信资讯
- 动态首页
- 互联网医院
- 外链模块基础版本
- 底层功能模块
- UI样式
- Http请求
- 动态功能
- 版本更新
- 支付(微信、支付宝)
- 二维码扫描
- 安全键盘
- 开发工具类模块
- icepick
- dart
- butterknife
- superRecycler
- jsonBuilder
- android-state
- iOS文档
- iOS组件模块文档
- iOS底层功能模块
- iOS弹出窗
- iOS加载框
- iOS-标准样式库
- iOS网络请求
- iOS二维码扫描
- iOS功能模块组件
- iOS健康资讯
- iOS健康百科
- iOS智能导诊
- iOS科室医生
- iOS医院导航
- iOS外链
- iOS模板(健康资讯类)
- iOS其它
- Cocoapods安装
- iOS-Cocoapods相关
- iOS-创建私有Cocoapods仓库
- 平台相关文档
- 全栈中心概述
- WEEX跨平台解决方法
- 玩转开发者平台
- android打包指南
- iOS自动化打包指南
- rubik-u web组件文档
- 开始
- 全局方法
- 列表组件
- 标题组件
- 按钮组件
- 切换组件
- 下拉框组件
- 开关/单/复选组件
- 功能列表组件
- 评星组件
- 搜索组件
- 图片上传组件
- 表单
- 表单例一
- 表单例二
- 表单例三
- 聊天组件
- 滑动刷新组件
- View窗口操作
- 面板组件
- 简单面板组件
- 信息展示面板
- 提示文字
- 底部悬浮组件
- 长文字展示组件
- 隐藏/显示面板
- 客户端组件
- 判断客户端环境
- 登录
- 等待框
- 提示框
- 时间选择器
- 扫码组件
- 拍照组件
- 访问相册组件
- 用户信息获取组件
- 返回上一页
- 返回首页
- 关闭webview
- 获取客户端类型
- 支付
- rubik-u web模板文档
- 基础组件模板
- 复选框使用案例