Skip to content

The most lightweight router library in android.If you need to do multi module development,you can try this.

License

Notifications You must be signed in to change notification settings

RubinTry/GoRouter

Repository files navigation

The most lightweight Framework about router in android.

中文文档

Javadoc

GitHub license

Latest version

module GoRouter-api GoRouter-compiler GoRouter-annotation
version Version Version Version

Feature

  1. Support for multi-modules
  2. Full Activity support
  3. Support get Fragment instance
  4. InstantRun support
  5. MultiDex support
  6. AndroidX Support
  7. Support kotlin

How to implement?

config.gradle

    dependencies{
    //annotation
    implementation 'cn.rubintry:gorouter-annotation:1.0.3'
    //core api
    implementation 'cn.rubintry:gorouter-api:1.0.33'
    //Java
    annotationProcessor  'cn.rubintry:gorouter-compiler:1.0.5'
    //Kotlin
    kapt  'cn.rubintry:gorouter-compiler:1.0.5'
    }

How to build your project?

gradle.properties

    //If you need run sub module as Application, you should make moduleIsApplication true
    moduleIsApplication = false

mainModule.gradle

    apply plugin: 'com.android.application'
    apply from: '../config.gradle'

    dependencies {
    
    if(!moduleIsApplication.toBoolean().booleanValue()){
        implementation project(path: ':anothermodule')
    }
}

anotherModule.gradle

    if(moduleIsApplication.toBoolean().booleanValue()){
        apply plugin: 'com.android.application'
    }else{
        apply plugin: 'com.android.library'
    }
    apply from: '../config.gradle'

    android {

    defaultConfig {
        if(moduleIsApplication.toBoolean().booleanValue()){
            applicationId "Your sub module's package name"
        }
        ...
    }

    sourceSets{
        main {
            if(moduleIsApplication.toBoolean().booleanValue()){
                //If you run this module as application , you can use the default AndroidManifest.xml
                manifest.srcFile 'src/main/AndroidManifest.xml'
            }else{
                //If you run this module as library , you should create a new AndroidManifest liked follows
                manifest.srcFile 'src/main/manifest/AndroidManifest.xml'
            }
        }
    }
}

The AndroidManifest.xml when you run module as library

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="your package name">

        <application>
            <activity android:name="your activity in another module">
            </activity>
        </application>

    </manifest>

How to use?

Simple usage


Fragment

   //Get a fragment instance
   Fragment instance = GoRouter.getInstance().build("fragmentRouteKey").go()
        

Activity

   //Navigation to an Activity immediately
   GoRouter.getInstance().build("routeKey2").go()


   //With data
   Bundle data = new Bundle()
   data.putInt(key , value);
   GoRouter.getInstance().build("routeKey2" , data).go()

Target Activity

    /**
    * @author logcat
    */
    @Route(url = "routeKey2")
    public class LoginActivity extends AppCompatActivity {



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            value = getIntent().getInt(key)
        }
    }

With sharedElements

Activity

Layout

activity1

   <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activity1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:transitionName="sharedActivity"
        android:textSize="50sp"></TextView>

activity2

   <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="activity1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:transitionName="sharedActivity"
        android:textSize="50sp"></TextView>
   //Activity1
   //navigation to another activity with animation.

   tv = findViewById(R.id.tv);

   GoRouter.getInstance().build("activity1 's routeKey")
   .go(this , ActivityOptionsCompat.makeSceneTransitionAnimation(this , tv , tv.getTransitionName()).toBundle())

Advanced usage

Decoupled by exposure service

    /**
      * Expose this service in common lib.
      *
      */
    interface SimpleService implements IProvider{
       
        void simple(Context context);
    }



     /**
      * Implements this service in module B and deal with it.
      *
      */
    @Route(url = "Simple/SimpleService")
    public class SimpleServiceImpl implements SimpleService{
        @override
        public void init(Context context){
            //Here you can do something to initialize.
        }

        public void simple(Context context){
            //do something
        }
    }


     /**
      * An activity in module A
      *
      */
    public class SimpleActivity extends AppCompatActivity{
        
        @ovserride
        public void oncreate(Bundle savedInstanceState){

            SimpleService service = GoRouter.getInstance().build("Simple/SimpleService").go(SimpleService.class);

            if(service != null){
                service.simple(this.getApplicationContext());
            }
        }
    }