yatatsu / AutoBundle

AutoBundle generates boilerplate code for field binding with android.os.Bundle
Apache License 2.0
135 stars 13 forks source link

Library module support #27

Closed Piasy closed 7 years ago

Piasy commented 7 years ago

Currently if I use auto-bundle both at library module and app module, there will be multiple AutoBundleBindingDispathcer, which will cause the build fail:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDevDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: 
duplicate entry: com/yatatsu/autobundle/AutoBundleBindingDispatcher.class

FragmentArgs support library module, but when using it in library module, we need to call the fragment-specific builder's injectArguments method, call FragmentArgs.inject doesn't work, that's not a bad idea, could auto-bundle add this feature too?

yatatsu commented 7 years ago

Thanks your feedback! Yes, you're right, now cannot use at multi modules. Annotation processor options (like FragmentArgs) is most simple solution. But as you pointed out, you need specific helper to bind (e.g. FooFragmentAutoBundle.bind(this)).

Piasy commented 7 years ago

Yep, but at least it's better. Can we add this feature?

yatatsu commented 7 years ago

Another solution is following.

First, in library project modules,

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                className 'com.yatatsu.autobundle.processor.AutoBundleProcessor'
                arguments = [ autoBundleAsLibrary : 'com.example.library' ]
            }
        }
    }
}

and in app's build gradle,

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                className 'com.yatatsu.autobundle.processor.AutoBundleProcessor'
                arguments = [ subDispatchers : 'com.example.library.AutoBundleBindingDispatcher' ]
            }
        }
    }
}

the code generated would be...

public final class AutoBundleBindingDispatcher {
  final com.example.library.AutoBundleBindingDispatcher subDispatcher1 = new AutoBundleBindingDispatcher()
  public void bind(@NonNull Object target, @NonNull Bundle args) {
    if (target instanceof ExampleActivity) {
      ExampleActivityAutoBundle.bind((ExampleActivity)target, args);
      return;
    // bind by library's dispatcher
    subDispatcher1.bind(target, args);
  }
}

What do you think this? Is it too much option?

Piasy commented 7 years ago

That's cool, and it's one time configuration, totally ok, IMO.

yatatsu commented 7 years ago

Thanks, I will do it in a few days.

yatatsu commented 7 years ago

Now released 4.1.0, library module is supported! Please Follow README#use-in-library-module, try it.

Piasy commented 7 years ago

Thats awesome! Thanks!

shingohu commented 7 years ago

if use this way,but dagger2 can not use ??

yatatsu commented 7 years ago

I don't try with dagger2, but it may work. If you have problem, please report it.

shingohu commented 7 years ago

@yatatsu yes,I am use dagger2 and this library in library module, if I set javaCompileOptions { annotationProcessorOptions { className 'com.yatatsu.autobundle.processor.AutoBundleProcessor' arguments = [ autoBundleAsLibrary : 'com.example.library' ] } }

but can not compile dagger2, I think is not you library problem,but I dont Know how to do

yatatsu commented 7 years ago

@shingohu I got it. Maybe DSL is wrong.

https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.AnnotationProcessorOptions.html

Try this.

javaCompileOptions {
annotationProcessorOptions {
arguments = [ autoBundleAsLibrary : 'com.example.library' ]
}
}

I will fix README. Thanks!

yatatsu commented 7 years ago

I updated README!

shingohu commented 7 years ago

Thanks!😁😁