随着业务复杂的和开发人员的增多,代码管理和协同开发逐渐成为开发过程中的瓶颈。好在Android 自己支持了分模块开发。搭建自己的模块化开发需要如下步骤:
- 搭建自己的私有仓库,存储各个模块。
搭建私有仓库推荐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
}
}
}
- 构建自己的路由让各个模块可以联动。
各个模块是相互独立,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);
}
}
网友评论