美文网首页
Android模块化开发

Android模块化开发

作者: 虾悠 | 来源:发表于2018-03-23 15:52 被阅读24次

随着业务复杂的和开发人员的增多,代码管理和协同开发逐渐成为开发过程中的瓶颈。好在Android 自己支持了分模块开发。搭建自己的模块化开发需要如下步骤:

  1. 搭建自己的私有仓库,存储各个模块。
    搭建私有仓库推荐nexus,搭建方法百度一下。上传AAR到仓库的方法如下:
apply plugin: 'maven'

def libVersion;
def libArtifactId;
def libGroupId = 'cn.daily.android';
def libDescription = 'lib';

task readProperty() {
    File file = project.file("publish.properties");
    if (!file.exists()) {
        file.createNewFile();
        Writer writer = file.newWriter();
        writer.writeLine("groupId=cn.daily.android");
        writer.writeLine("artifactId=" + project.name);
        writer.writeLine("version=0.0.1-SNAPSHOT");
        writer.writeLine("description=默认描述,请根据情况修改相应字段");
        writer.close();
        throw new FileNotFoundException("请修改" + file.getPath() + "\n中的默认信息!\n注意只提醒一次,否则使用默认!");
    }

    Properties properties = new Properties();
    InputStream inputStream = file.newInputStream();
    properties.load(inputStream);
    libVersion = properties.getProperty("version");
    libArtifactId = properties.getProperty("artifactId");
    libGroupId = properties.getProperty("groupId")
    libDescription = properties.getProperty("description")
    inputStream.close();
}

//上传AAR、source、JavaDoc脚本
uploadArchives {
    repositories {
        //配置仓库地址和模块相关信息
        mavenDeployer {
            snapshotRepository(url: 'http://替换成你的仓库地址/nexus/content/repositories/snapshots') {
                authentication(userName: '替换成你的nexus用户名', password: '替换成你的Nexus仓库密码')
            }
            repository(url: 'http://替换成你的仓库地址/nexus/content/repositories/releases') {
                authentication(userName: '替换成你的nexus用户名', password: '替换成你的Nexus仓库密码')
            }
            pom.project {
                version libVersion
                artifactId libArtifactId
                groupId libGroupId
                description libDescription
                packaging 'aar'
            }
        }

        //创建源文件任务
        task androidSourcesJar(type: Jar) {
            classifier = 'sources'
            from android.sourceSets.main.java.srcDirs
        }

        //上传文件
        artifacts {
            archives file(project.name + '.aar')
            archives androidSourcesJar
        }
    }
}
  1. 构建自己的路由让各个模块可以联动。
    各个模块是相互独立,Activity之间的跳转只能通过隐式调用。通过packageManager的方法 queryIntentActivities来查找满足跳转的Activity。详细代码如下:
public class Nav {
    private Context mContext;
    private Intent mIntent;

    private Fragment mFragment;
    private Bundle mBundle;
    private boolean isNeedLogin = false;
    private String mAction;
    private List<String> mCategories;
    private static List<Interceptor> sInterceptors = new ArrayList<>();

    private Nav(Context context) {
        mContext = context;
        mIntent = new Intent();
        mIntent.setAction(Intent.ACTION_VIEW);
    }

    private Nav(Fragment fragment) {
        this(fragment.getContext());
        mFragment = fragment;
    }

    public static Nav with(Context context) {
        return new Nav(context);
    }

    public static Nav with(Fragment fragment) {
        return new Nav(fragment);
    }

    public static void addInterceptor(Interceptor interceptor) {
        sInterceptors.add(interceptor);
    }

    public boolean to(String url) {
        return to(url, -1);
    }

    public boolean to(String url, int requestCode) {
        if (TextUtils.isEmpty(url)) {
            if (BuildConfig.DEBUG) {
                throw new NullPointerException("url");
            }
            return false;
        }
        return to(Uri.parse(url), requestCode);
    }


    public Nav setExtras(Bundle bundle) {
        mBundle = bundle;
        return this;
    }

    public Nav needLogin(boolean isNeedLogin) {
        this.isNeedLogin = isNeedLogin;
        return this;
    }

    public Nav setAction(String action) {
        mAction = action;
        return this;
    }

    public Nav addCategory(String category) {
        if (mCategories == null) {
            mCategories = new ArrayList<>();
        }
        mCategories.add(category);
        return this;
    }

    public Nav removeCategory(String category) {
        if (mCategories != null && mCategories.size() > 0) {
            mCategories.remove(category);
        }
        return this;
    }

    public boolean to(Uri uri, int requestCode) {

        if (uri == null) {
            if (BuildConfig.DEBUG) {
                throw new NullPointerException("uri");
            }
            return false;
        }

        if (!TextUtils.isEmpty(mAction)) {
            mIntent.setAction(mAction);
        }

        if (mCategories != null && mCategories.size() > 0) {
            for (int i = 0; i < mCategories.size(); i++) {
                mIntent.addCategory(mCategories.get(i));
            }
        }

        if (mBundle != null) {
            mIntent.putExtras(mBundle);
        }

        if (sInterceptors != null) {
            for (int i = 0; i < sInterceptors.size(); i++) {
                uri = sInterceptors.get(i).before(uri);
            }
        }
        mIntent.setData(uri.normalizeScheme());
        return parseIntent(requestCode);
    }


    private boolean parseIntent(int requestCode) {
        List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentActivities
                (mIntent, PackageManager.MATCH_ALL);
        try {
            if (resolveInfos == null || resolveInfos.size() == 0) {
                throw new ActivityNotFoundException("Not match any Activity:" + mIntent.toString());
            } else {
                ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
                //优先匹配应用自己的Activity
                for (int i = 0; i < resolveInfos.size(); i++) {
                    if (resolveInfos.get(i).activityInfo.packageName.contains(mContext
                            .getPackageName())) {
                        activityInfo = resolveInfos.get(i).activityInfo;
                        break;
                    }
                }
                mIntent.setClassName(activityInfo.packageName, activityInfo.name);
            }
            startIntent(mIntent, requestCode);
            return true;
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void startIntent(Intent intent, int requestCode) {
        if (mFragment != null) {
            mFragment.startActivityForResult(intent, requestCode);
        } else if (mContext instanceof Activity) {
            ((Activity) mContext).startActivityForResult(intent, requestCode);
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        }
    }

    private boolean isLogin(Context context) {
        return false;
    }

    public interface Interceptor {
        Uri before(Uri uri);
    }

}

相关文章

  • Android 模块化开发

    一、模块化浅谈 1. Android 模块化开发介绍; 模块化开发思路就是:单独开发每个模块,用集成的方式把他们组...

  • 使用ARouter进行Android模块化开发

    Android模块化 关于为什么要进行Android模块化开发,网上也已经讲烂了,不过归结起来,大体是可以总结为:...

  • Module 2 Module

    Android模块化开发小demo 内置Android路由, 支持注解设置路由 如果你只想使用内置的Android...

  • Android开发最佳实践

    Android 开发最佳实践 使用Android Studio 使用 String.xml 创建模块化的UI布局,...

  • android组件化开发资料

    1、Android组件化开发实践 2、Android架构思考(模块化、多进程) 3、糯米移动组件架构演进之路 4、...

  • 是时候来一波Android插件化了

    是时候来一波Android插件化了 是时候来一波Android插件化了前言Android开发演进模块化介绍插件化介...

  • android 收藏的一些好文

    1.常用git命令清单 2.Android组件化、模块化开发 3.Android 组件化案例 4. 5.

  • 模块化开发

    js模块化开发vue模块化开发

  • Android 将Library 发布本地maven仓库

    Android 将Library 发布本地maven仓库 (Mac OS环境) (最近在学习项目模块化开发,写下 ...

  • Native模块化编程

    下面带来Native模块化、插件化的相关系统图: 因为笔者是开发Android的,所以带来的是Android部分的...

网友评论

      本文标题:Android模块化开发

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