微信 支付、分享 组件化

作者: 程序员阿兵 | 来源:发表于2019-06-24 17:10 被阅读53次

    微信分享,就需要有一个“${applicationId}.wxapi.WXEntryActivity”,这个操作在多包名或者是组件化的项目中很不方便,可以使用设置代理Alies或者使用注解生成器去实现解耦

    介绍方法

    • 通过代码生成器,编译器修改
    • 设置Activity-Alias代理类去修改

    下面介绍第一种方式:

    • 创建Java library记住是Java库而不是Android,因为很多api是Java才能使用到
    • 编写注解,指定编译器使用,并且作用在方法之上
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.SOURCE)
    public @interface EntryGenerator {
        String packageName();
        Class<?> entryTemplate();
    }
    
    
    • 对传入对包名以及指定类名进行修改
    final class EntryVisitor extends SimpleAnnotationValueVisitor7<Void, Void> {
    
        private final Filer FILER;
        private String mPackageName = null;
    
        EntryVisitor(Filer FILER) {
            this.FILER = FILER;
        }
    
        @Override
        public Void visitString(String s, Void p) {
            mPackageName = s;
            return p;
        }
    
        @Override
        public Void visitType(TypeMirror t, Void p) {
            generateJavaCode(t);
            return p;
        }
    
        private void generateJavaCode(TypeMirror typeMirror) {
            final TypeSpec targetActivity =
                    TypeSpec.classBuilder("WXEntryActivity")
                            .addModifiers(Modifier.PUBLIC)
                            .addModifiers(Modifier.FINAL)
                            .superclass(TypeName.get(typeMirror))
                            .build();
    
            final JavaFile javaFile = JavaFile.builder(mPackageName + ".wxapi", targetActivity)
                    .addFileComment("微信入口文件")
                    .build();
            try {
                javaFile.writeTo(FILER);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 实现注解代码生成器
    /**
     * Created by 桂雁彬 on 2019/6/22
     */
    
    @SuppressWarnings("unused")
    @AutoService(Processor.class)
    public final class LatteProcessor extends AbstractProcessor {
    
        @Override
        public Set<String> getSupportedAnnotationTypes() {
            final Set<String> types = new LinkedHashSet<>();
            final Set<Class<? extends Annotation>> supportAnnotations = getSupportedAnnotations();
            for (Class<? extends Annotation> annotation : supportAnnotations) {
                types.add(annotation.getCanonicalName());
            }
            return types;
        }
    
        private Set<Class<? extends Annotation>> getSupportedAnnotations() {
            final Set<Class<? extends Annotation>> annotations = new LinkedHashSet<>();
            annotations.add(EntryGenerator.class);
            return annotations;
        }
    
        @Override
        public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
            generateEntryCode(env);
            return true;
        }
    
        private void scan(RoundEnvironment env,
                          Class<? extends Annotation> annotation,
                          AnnotationValueVisitor visitor) {
    
            for (Element typeElement : env.getElementsAnnotatedWith(annotation)) {
                final List<? extends AnnotationMirror> annotationMirrors =
                        typeElement.getAnnotationMirrors();
    
                for (AnnotationMirror annotationMirror : annotationMirrors) {
                    final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues
                            = annotationMirror.getElementValues();
    
                    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
                            : elementValues.entrySet()) {
                        entry.getValue().accept(visitor, null);
                    }
                }
            }
        }
    
        private void generateEntryCode(RoundEnvironment env) {
            final EntryVisitor entryVisitor =
                    new EntryVisitor(processingEnv.getFiler());
            scan(env, EntryGenerator.class, entryVisitor);
        }
    }
    
    • 实现上面对注解
    @SuppressWarnings("unused")
    @EntryGenerator(
            packageName = "com.diabin.fastec.example",
            entryTemplate = WXEntryTemplate.class
    )
    public interface WeChatEntry {
    }
    
    

    上面指定了注解并且指定了 需要修改的包名和自己自定义的类,然后就可以在自定义的类下微信操作了

    public class WXEntryTemplate extends BaseWXEntryActivity {
    
       @Override
       protected void onResume() {
           super.onResume();
           finish();
           overridePendingTransition(0, 0);
       }
    
       @Override
       protected void onSignInSuccess(String userInfo) {
           LatteWeChat.getInstance().getSignInCallback().onSignInSuccess(userInfo);
       }
    }
    
    

    下面介绍第二种方式:Activity-alias 了解一下

       <!--微信友盟分享-->
            <activity
                android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXEntryActivity"
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:exported="true"
                android:theme="@style/AppTheme" />
            <activity
                android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXH5EntryActivity"
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:exported="true"
                android:process=":webview"
                android:theme="@style/AppTheme" />
            <activity
                android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXSnapshotEntryActivity"
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:exported="true"
                android:process=":snapshot"
                android:theme="@style/AppTheme" />
            <activity
                android:name="com.linewell.bigapp.component.accomponentshare.wxapi.WXPayEntryActivity"
                android:exported="true"
                android:launchMode="singleTop" />
            <activity-alias
                android:name="${applicationId}.wxapi.WXEntryActivity"
                android:exported="true"
                android:targetActivity="com.linewell.bigapp.component.accomponentshare.wxapi.WXEntryActivity" />
            <activity-alias
                android:name="${applicationId}.wxapi.WXPayEntryActivity"
                android:exported="true"
                android:targetActivity="com.linewell.bigapp.component.accomponentshare.wxapi.WXPayEntryActivity" />
            <!--QQ友盟分享-->
    

    上面的代码是不是很简单无脑,只需要设置对应占位符包名下就行

    相关文章

      网友评论

        本文标题:微信 支付、分享 组件化

        本文链接:https://www.haomeiwen.com/subject/adeoqctx.html