Feb 19, 2017
Mar 4, 2017 (Retired)
Su Cong (sucong0826)
Source code


  1. Please copy netsugar-master folder with sources into your own project as a single module. Let your 'app' module add a dependence on it or you do this in your app build.gradle:
dependencies {
    compile project(':netsugar-master')
    compile 'org.aspectj:aspectjrt:1.8.5'
  1. Please compile 'org.aspectj:aspectjrt:1.8.5' or other versions you know such as 1.8.9+. Just like above, oh, it should be added in your 'app' module build.gradle.

  2. Config 'app' build.gradle. If you don't know how to config, please check target project. Give a link below: app build.gradle configuration

  3. Add a Pair of Annotation NetSugar provides three annotations for you to use:

    • @NetSugar(type=[NetworkType], pair=[int])
    • @Offline(pair=[int])
    • @Global

@NetSugar and @Offline are aimed at method only.Please remember, they MUST be used as a PAIR with same value.

@NetSugar(type=NetworkType.WIFI, pair=0x1)
private void playVideo() {
    // only prepare playing video works.

private void handlePlayVideoOffline(MatchResult result) {
    // when network work state is not satisfied, this method will be invoked.

As you see, @NetSugar and @Offline are showing with the same value of 'pair' at same time, it is NECESSARY. If you think there is no need for actions to handle offline or network state not matching situations, you should also provide this kind of method like above. If you won't, NoSuchMethodException will be thrown. It is caused by method searching mechanism. You may read netsugar-master/src/main/java/ to get more details.

  1. Config 'pair' value in them Like above, pairs are same value. Why I do that? For method hooking. In the previous version, there was a big issue, is that when a class has two methods with the same name, NetSugarAspect is puzzled. It will handle the execution in order. However, problems happened. Two methods which having the same name but different arguments are designed to cope with different things, method reflection api cannot do that just because of arguments. Thus, this version, I change method pairing mechanism, use a 'pair' to hook method one on one. Please note that:

    • if you won't provide @Offline or its pair value, a NoHookException or WrongPairException will be thrown.
    • if you annotate method with @Offline and same value more than one, only the first method framework checked will be executed.
    • The default pair value of @Offline is 0x0, it is used for validating inside, please don't use it. Pair value will be taken bigger than 0x0.
  2. Receiving the callback argument:MatchResult You may be inquisitive about why we provide the method with a MatchResult as above. The method annotated with @Offline will be handled automatically, that is, there is no need to consider how and where to invoke this method. The method is executed by NetSugar framework. As for you, just compose your codes when this situation happen. MatchResult is an argument which NetSugar gives you. It represents network checking result and framework has already initialized for you, just use it like an normal object. It will tell you matchType(int), reason(String), currentNetworkType(NetworkType).

    private void handleOffline(MatchResult result) {
        Toast.makeText(MainActivity.this, result.getReason(), /* time */).show();

Please note that: * The method you provide must have an argument and its type must be MatchResult. * You can define your own arguments after MatchResult in params list.

  1. Finish Injection In some Activity, you must finish this operation:
    // this means context fo current Activity
    // onCreate(Bundle savedInstanceState)

Alright, everything is ok, just enjoy the convenience NetSugar brings to you.(A little awkward... ^.~)

Limitations & Issues

  1. If you find @Global annotation, please do not use it. It has some issues about service and broadcast.
  2. Must provide correct pair value between @NetSugar and @Offline.
  3. The method annotated with @Offline will receive an argument MatchResult. Must remember that.
  4. Config your sample according to github project configuration when you find sample won't work.