Jet annotation library gybin02. Making. IO/Jet /

Use annotations to achieve the removal of some duplicate template Code, make Code simpler; Includes Butterknife functionality; Automatically initialize the Field in the Activity.

Named Jetpack from WordPress;

  • Keep it simple and stupid by doing something simple with each annotation
  • Heart_eyes: :heart_eyes:
  • If you like, please give a Star smile

Has been completed

Note the name role note
@JFindView Equal to the findViewById Supports activities, fragments,ViewHolder, etc
@JFindViewOnClick FindViewById () and View.setonClickListener (this) The Activity needs to implement the View.onClickListener interface
@JOnClick The annotation on the method is view.setonClickListener Support single View and multiple views, method parameter with View View, you can return the View click
@JIntent Used to automatically resolve values in an Intent It can also be automatically evaluated from the Bundle
@JImplement For decoupling, the implementation is invoked based on the interface value
@JProvider Cooperate with JImplement
@JPermission 6.0 automatically apply for permission; Support multiple permissions
@JPermissionGrant Permission authorization succeeded Cooperate with @jPermission, optional
@JPermissionDeny Permission authorization failure Cooperate with @jPermission, optional


Runtime injection, which automatically initializes the findViewById of a View


Support automatic initialization of View findViewById and onClick

The Activity needs to implement the View. An OnClickListener. Class;


The Annotation Runtime implements the getIntent function to read the data in the Intent.

Automatic value from intents, such as intent.getStringExtra ([String])

The supported parameter types are as follows (including default values)

           return intent.getStringExtra(value)

           return intent.getCharExtra(value, '\ 0');

            return intent.getByteExtra(value, (byte) 0);

            return intent.getShortExtra(value, (short) 0);

            return intent.getIntExtra(value, 0);

            return intent.getLongExtra(value, 0);

            return intent.getFloatExtra(value, 0);

            return intent.getDoubleExtra(value, 0);

            return intent.getBooleanExtra(value, false);

            return intent.getSerializableExtra(value);

            return intent.getBundleExtra(value);

            return intent.getStringArrayExtra(value);

            return intent.getStringExtra(value);Copy the code

Using the example

    // The original code
   public class DemoActivity implement View.OnClickListener{
     / / Android code

      private String testString;
      private boolean testBoolean;
      private int testInt;
      private long testLong;

      private Button btnHello;
      private Button btn_test;

      / / value
      public void onCreate(Bundle savedInstanceState){
        / / Intent initialization
          Intent intent = getIntent();
          testString = intent.getIntExtra("testString".0);
          testInt = intent.getStringExtra("testInt");
          testBoolean = intent.getStirngExtra("testBoolean");
          testLong = intent.getBooleanExtra("testLong");

         / / the View initialization
        btnHello= findViewById(R.layout.btn_hello);
        btn_test= findViewById(R.layout.btn_test);

      public void onClick(View v){
          int id= v.getId;
          if(id== hello_world){
            //TODO Do Something}}//TODO:What would your code look like if you had to take more and more parameters? Is it logically complicatedCopy the code

Using Jet annotations:

    private String testString;
    private boolean testBoolean;
    private int testInt;
    private long testLong;

    private Button btnHello;
    private Button btn_test;

      / / value
      public void onCreate(Bundle savedInstanceState){
        // All attributes can be used after initialization

      public void onClick(View v){
          int id= v.getId;
          if(id== hello_world){
            //TODO Do Something}}Copy the code

Example 2

   // Support direct value from Bundle;
    private String mParam1;
    private String mParam2;

    public static BlankFragment newInstance(Activity activity) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        String param1 = "Hello ";
        String param2 = "world";
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        return fragment;

    public void onCreate(Bundle savedInstanceState) {

        Jet.bind(this, getArguments());Copy the code

Non-activity BINDING for other classes

You can also perform binding on arbitrary objects by supplying your own view root.

You can bind any object you want as long as you provide a View Root;

public class FancyFragment extends Fragment {
  @JFindView( Button button1;
  @JFindView( Button button2;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    Jet.bind(this, view);
    // TODO Use fields...
    returnview; }}Copy the code

Another use is simplifying the view holder pattern inside of a list adapter.

public class MyAdapter extends BaseAdapter {
  @Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if(view ! =null) {
      holder = (ViewHolder) view.getTag();
    } else {
      view = inflater.inflate(R.layout.whatever, parent, false);
      holder = new ViewHolder(view);
    }"John Doe");
    // etc...

    return view;

  static class ViewHolder {
    TextView name;
    TextView jobTitle;

    public ViewHolder(View view) {
      Jet.bind(this, view);
You can see this implementation in action in the provided sample.Copy the code

@JImplement :heart_eyes: :heart_eyes: :heart_eyes:

  • Implementation according to the interface class annotation, automatic call implementation class function, code decoupling necessary; Instead of direct write reflection
  • Can also be used for cross-module function calls, but more than that can be discovered on your own;


Interface class

public interface ITest {
    public void  test(a);

    public String getValue(a);
}Copy the code

Implementation class:

// @jprovider is used to identify the implementation class to avoid confusion and avoid being deleted as a non-functional call;
public class TestImpl {

    private static final String TAG = "TestImpl";

    public void test(a) {
        Log.d(TAG, "test Method invoke");

    public String getValue(a) {
        String str = "HelloWorld";
        Log.d(TAG, "GetValue Method invoke:" + str);
        returnstr; }}Copy the code

Call method:

                    ITest iTest = JetProxy.getInstance().create(ITest.class);
                    iTest.getValue();Copy the code

Automatic permission application on JPermission 6.0;

//@JPermission(all = {Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_CONTACTS}) @JPermission(Manifest.permission.CAMERA) public class MainActivity extends AppCompatActivity .... @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults); } @jpermissionGrant private void onGrand() {toast.maketext (mainactivity.this, "onGrant Success", Toast.LENGTH_SHORT) .show(); } @jpermissionDeny private void onDeny(String permisson) {toast.makeText (mainactivity.this, "onDenied Success: "+permisson, Toast.LENGTH_SHORT) .show(); }Copy the code


dependencies {
   // For internal use
   / / the compile 'com. Meiyou. Framework: jet: 0.0.10 - the SNAPSHOT'
   compile 'com. Meiyou. Framework: jet: 1.5.0'

}Copy the code

Snapshots of the development version are available in Sonatype’s snapshots repository.

Confused configuration:

# Use the JetProxy feature to avoid confusion
-keepattributes *Annotation*
-keep class * extends java.lang.annotation.Annotation { *; }
-keep @com.meiyou.jet.annotation.JImplement class * { *; }
-keep @com.meiyou.jet.annotation.JProvider class * { *; }Copy the code

To implement,

The implementation area lists some common features that I have in mind, but there are certainly many common features in the project; Welcome to raise the Issue, make the project more powerful;

  • @JTrycatch

Safely calling a method: automatically add a try Catch to the method;

Implemented using AspectJ. Specific reference: JET-AOP project;

  • Retrofit-like implementation of the RestFull request library; @ GET @ Post; @ Head, etc.;…


  • Performance test; O(1) method, 20 @jFindView attributes initialization, 50ms; It takes 5ms more than direct FindViewById, and the performance loss is basically negligible.
  • Some annotation implementations require AOP techniques; You can refer to [Jet – AOP] (git. Meiyou. Im/Android/Jet… Engineering;

Communication group:

QQ group: 547612870