Skip to content

Commit

Permalink
AucFrame 之模块间通信
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Jul 20, 2019
1 parent 56babed commit c0bef7b
Show file tree
Hide file tree
Showing 24 changed files with 277 additions and 65 deletions.
11 changes: 5 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ buildscript {
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta05'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
for (def entrySet : ConfigUtils.getApplyPlugins().entrySet()) {
classpath entrySet.value.dep
}
}
}

allprojects {
repositories {
google()
jcenter()

}
}

Expand Down
5 changes: 5 additions & 0 deletions buildAPP.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply {
plugin "com.android.application"
plugin "kotlin-android"
plugin "kotlin-android-extensions"
plugin "com.blankj.api"
}

android {
Expand Down Expand Up @@ -49,6 +50,10 @@ dependencies {
for (def entrySet : ConfigUtils.getApplyPkgs().entrySet()) {
api entrySet.value.dep
}

if (Config.depConfig.feature.mock.isApply) {
api Config.depConfig.feature.mock.dep
}
}

def getSuffix() {
Expand Down
2 changes: 1 addition & 1 deletion buildLib.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ android {
}

dependencies {
if (project.name == 'pkg') {
if (project.name == 'pkg' || project.name == 'mock') {
// if module's name equals 'pkg', api all of export
for (def entrySet : ConfigUtils.getApplyExports().entrySet()) {
api entrySet.value.dep
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/src/main/groovy/Config.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ class Config {
static pkgConfig = []

static depConfig = [
plugin : [
gradle: new DepConfig("com.android.tools.build:gradle:3.3.0"),
kotlin: new DepConfig("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"),
api : new DepConfig("com.blankj:api-gradle-plugin:1.0"),
],

feature : [
mock : new DepConfig(":feature:mock"),

launcher: [
app: new DepConfig(":feature:launcher:app")
],
Expand Down
77 changes: 75 additions & 2 deletions buildSrc/src/main/groovy/ConfigUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import org.apache.commons.io.FileUtils
import org.gradle.BuildListener
import org.gradle.BuildResult
import org.gradle.api.Project
import org.gradle.api.ProjectEvaluationListener
import org.gradle.api.ProjectState
import org.gradle.api.Task
import org.gradle.api.execution.TaskExecutionListener
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle
import org.gradle.api.tasks.TaskState

import java.text.SimpleDateFormat

class ConfigUtils {

static getApplyPlugins() {
def plugins = getDepConfigByFilter(new DepConfigFilter() {
@Override
boolean accept(String name, DepConfig config) {
if (!name.startsWith("plugin.")) return false
if (!config.isApply) return false
return true
}
})
GLog.d("getApplyPlugins = ${GLog.object2String(plugins)}")
return plugins
}

static getApplyPkgs() {
def applyPkgs = getDepConfigByFilter(new DepConfigFilter() {
@Override
Expand Down Expand Up @@ -38,11 +57,15 @@ class ConfigUtils {

private static class ConfigBuildListener implements BuildListener {

private List<TaskInfo> taskInfoList = []
private long startBuildMillis

@Override
void buildStarted(Gradle gradle) {}

@Override
void settingsEvaluated(Settings settings) {
startBuildMillis = System.currentTimeMillis()
GLog.d("settingsEvaluated")
includeModule(settings)
}
Expand Down Expand Up @@ -79,17 +102,56 @@ class ConfigUtils {
@Override
void projectsEvaluated(Gradle gradle) {
GLog.d("projectsEvaluated")
gradle.addListener(new TaskExecutionListener() {
@Override
void beforeExecute(Task task) {
task.ext.startTime = System.currentTimeMillis()
}

@Override
void afterExecute(Task task, TaskState state) {
def exeDuration = System.currentTimeMillis() - task.ext.startTime
if (exeDuration >= 100) {
taskInfoList.add(new TaskInfo(task, exeDuration))
}
}
})
}

@Override
void buildFinished(BuildResult result) {
GLog.d("buildFinished")
if (!taskInfoList.isEmpty()) {
Collections.sort(taskInfoList, new Comparator<TaskInfo>() {
@Override
int compare(TaskInfo t, TaskInfo t1) {
return t1.exeDuration - t.exeDuration
}
})
StringBuilder sb = new StringBuilder()
int buildSec = (System.currentTimeMillis() - startBuildMillis) / 1000;
int m = buildSec / 60;
int s = buildSec % 60;
def timeInfo = (m == 0 ? "${s}s" : "${m}m ${s}s (${buildSec}s)")
sb.append("BUILD FINISHED in $timeInfo\n")
taskInfoList.each {
sb.append(String.format("%7sms %s\n", it.exeDuration, it.task.path))
}
def content = sb.toString()
GLog.l(content)
File file = new File(result.gradle.rootProject.buildDir.getAbsolutePath(),
"build_time_records_" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt")
FileUtils.write(file, content)
}
}

/**
* 在 settings.gradle 中 根据 appConfig 和 pkgConfig 来 include 本地模块
*/
private static includeModule(Settings settings) {
if (Config.pkgConfig.isEmpty()) {
Config.depConfig.feature.mock.isApply = false
}
def config = getDepConfigByFilter(new DepConfigFilter() {
@Override
boolean accept(String name, DepConfig config) {
Expand All @@ -99,8 +161,8 @@ class ConfigUtils {
config.isApply = false
}
}
if (!Config.pkgConfig.isEmpty()) {// 如果 Config.pkgConfig 不为空,说明是 pkg 调试模式
if (name.endsWith('.pkg')) {// 如果是 pkg 的话
if (name.endsWith('.pkg')) {// 如果是 pkg 的话
if (!Config.pkgConfig.isEmpty()) {// 如果 Config.pkgConfig 不为空,说明是 pkg 调试模式
def pkgName = name.substring('feature.'.length(), name.length() - 4)// 获取 pkg 模块的名字
if (!Config.pkgConfig.contains(pkgName)) {// 如果 Config.pkgConfig 中不存在,那就不让它进依赖
config.isApply = false
Expand Down Expand Up @@ -136,6 +198,17 @@ class ConfigUtils {
})
GLog.l("generateDep = ${GLog.object2String(config)}")
}

private static class TaskInfo {

Task task
long exeDuration

TaskInfo(Task task, long exeDuration) {
this.task = task
this.exeDuration = exeDuration
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.blankj.feature0.export.bean;


public class Feature0Bean {
}
2 changes: 1 addition & 1 deletion feature/feature0/pkg/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<application>
<activity
android:name=".Feature0Activity"
android:name=".main.Feature0Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="user"
android:windowSoftInputMode="stateHidden" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.blankj.feature0.pkg.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.blankj.common.CommonTitleActivity;
import com.blankj.feature0.pkg.R;
import com.blankj.feature1.export.api.Feature1Api;
import com.blankj.feature1.export.bean.Feature1Param;
import com.blankj.feature1.export.bean.Feature1Result;
import com.blankj.utilcode.util.ApiUtils;
import com.blankj.utilcode.util.ToastUtils;


public class Feature0Activity extends CommonTitleActivity {

@Override
public CharSequence bindTitle() {
return getString(R.string.feature0_title);
}

@Override
public boolean isSwipeBack() {
return false;
}

@Override
public int bindLayout() {
return R.layout.feature0_activity;
}

@Override
public void initView(@Nullable Bundle savedInstanceState, @Nullable View contentView) {
findViewById(R.id.startFeature1Btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Feature1Result result = ApiUtils.getApi(Feature1Api.class)
.startFeature1Activity(Feature0Activity.this, new Feature1Param("Feature1Param"));
ToastUtils.showLong(result.getName());
}
});
}

@Override
public void doBusiness() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:padding="16dp">

<Button
android:id="@+id/startFeature1Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_feature1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.blankj.feature1.export.api;

import android.content.Context;

import com.blankj.feature1.export.bean.Feature1Param;
import com.blankj.feature1.export.bean.Feature1Result;
import com.blankj.utilcode.util.ApiUtils;


public abstract class Feature1Api extends ApiUtils.BaseApi {

public abstract Feature1Result startFeature1Activity(Context context, Feature1Param param);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.blankj.feature1.export.bean;


public class Feature1Param {

private String name;

public Feature1Param(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.blankj.feature1.export.bean;


public class Feature1Result {

private String name;

public Feature1Result(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
2 changes: 1 addition & 1 deletion feature/feature1/pkg/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<application>
<activity
android:name=".Feature1Activity"
android:name=".main.Feature1Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="user"
android:windowSoftInputMode="stateHidden" />
Expand Down
Loading

0 comments on commit c0bef7b

Please sign in to comment.