Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

兼容android studio 3.0 #57

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.dd.comgradle'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26
defaultConfig {
applicationId "com.mrzhang.androidcomponent"
minSdkVersion 15
targetSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -25,7 +24,7 @@ android {
}

dependencies {
compile project(':componentservice')
implementation project(':componentservice')
}

combuild {
Expand Down
13 changes: 6 additions & 7 deletions basiclib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 15
Expand All @@ -22,12 +21,12 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
api fileTree(dir: 'libs', include: ['*.jar'])
androidTestApi('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.picasso:picasso:2.5.2'
api 'com.android.support:appcompat-v7:26.1.0'
testApi 'junit:junit:4.12'
api 'com.squareup.okhttp3:okhttp:3.4.1'
api 'com.squareup.picasso:picasso:2.5.2'
}
3 changes: 1 addition & 2 deletions basicres/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 15
Expand All @@ -24,5 +23,5 @@ android {
}

dependencies {
compile project(':basiclib')
api project(':basiclib')
}
4 changes: 2 additions & 2 deletions build-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'groovy'
apply plugin: 'maven'

dependencies {
compile 'com.android.tools.build:gradle:2.3.2'
compile 'com.android.tools.build:gradle:3.0.1'
compile group: 'org.javassist', name: 'javassist', version: '3.20.0-GA'
//gradle sdk
compile gradleApi()
Expand All @@ -15,7 +15,7 @@ repositories {
}

group = 'com.mrzhang.andcomponent'
version = '0.0.2'
version = '0.0.5'

uploadArchives {
repositories {
Expand Down
121 changes: 64 additions & 57 deletions build-gradle/src/main/groovy/com.dd.buildgradle/ComBuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,49 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

public class ComBuild implements Plugin<Project> {
class ComBuild implements Plugin<Project> {

private def MAINMODULE = "mainmodulename"
private def DEDAULT = "app"
private def ALLMODULE = "all"
private def TAG = "ComBuild --->"
//默认是app,直接运行assembleRelease的时候,等同于运行app:assembleRelease
String compilemodule = "app"
private def compileModule = DEDAULT

void apply(Project project) {
project.extensions.create('combuild', ComExtension)

String taskNames = project.gradle.startParameter.taskNames.toString()
System.out.println("taskNames is " + taskNames);
//类似这种格式[clean, :basiclib:generateDebugSources]
System.out.println("$TAG taskNames is " + taskNames)
String module = project.path.replace(":", "")
System.out.println("current module is " + module);
System.out.println("$TAG current module is " + module)
AssembleTask assembleTask = getTaskInfo(project.gradle.startParameter.taskNames)

if (assembleTask.isAssemble) {
fetchMainmodulename(project, assembleTask);
System.out.println("compilemodule is " + compilemodule);
fetchCompileModuleName(project, assembleTask)
System.out.println("$TAG compile module is " + compileModule)
}

if (!project.hasProperty("isRunAlone")) {
throw new RuntimeException("you should set isRunAlone in " + module + "'s gradle.properties")
throw new RuntimeException("$TAG you should set isRunAlone in " + module + "'s gradle.properties")
}

//对于isRunAlone==true的情况需要根据实际情况修改其值,
// 但如果是false,则不用修改,该module作为一个lib,运行module:assembleRelease则发布aar到中央仓库
boolean isRunAlone = Boolean.parseBoolean((project.properties.get("isRunAlone")))
String mainmodulename = project.rootProject.property("mainmodulename")
String mainModuleName = project.rootProject.property(MAINMODULE)
if (isRunAlone && assembleTask.isAssemble) {
//对于要编译的组件和主项目,isRunAlone修改为true,其他组件都强制修改为false
//这就意味着组件不能引用主项目,这在层级结构里面也是这么规定的
if (module.equals(compilemodule) || module.equals(mainmodulename)) {
isRunAlone = true;
} else {
isRunAlone = false;
}
isRunAlone = module == compileModule || module == mainModuleName
}
project.setProperty("isRunAlone", isRunAlone)

//根据配置添加各种组件依赖,并且自动化生成组件加载代码
if (isRunAlone) {
project.apply plugin: 'com.android.application'
if (!module.equals(mainmodulename)) {
if (module != mainModuleName) {
project.android.sourceSets {
main {
manifest.srcFile 'src/main/runalone/AndroidManifest.xml'
Expand All @@ -55,121 +56,127 @@ public class ComBuild implements Plugin<Project> {
}
}
}
System.out.println("apply plugin is " + 'com.android.application');
if (assembleTask.isAssemble && module.equals(compilemodule)) {
System.out.println("$TAG apply plugin is " + 'com.android.application')
if (assembleTask.isAssemble && module == compileModule) {
compileComponents(assembleTask, project)
project.android.registerTransform(new ComCodeTransform(project))
}
} else {
project.apply plugin: 'com.android.library'
System.out.println("apply plugin is " + 'com.android.library');
System.out.println("$TAG apply plugin is " + 'com.android.library')
project.afterEvaluate {
Task assembleReleaseTask = project.tasks.findByPath("assembleRelease")
if (assembleReleaseTask != null) {
assembleReleaseTask.doLast {
File infile = project.file("build/outputs/aar/$module-release.aar")
File outfile = project.file("../componentrelease")
File desFile = project.file("$module-release.aar");
File desFile = project.file("$module-release.aar")
project.copy {
from infile
into outfile
rename {
String fileName -> desFile.name
}
}
System.out.println("$module-release.aar copy success ");
System.out.println("$TAG $module-release.aar copy success ")
}
}
}
}

}

/**
* 根据当前的task,获取要运行的组件,规则如下:
* assembleRelease ---app
* app:assembleRelease :app:assembleRelease ---app
* sharecomponent:assembleRelease :sharecomponent:assembleRelease ---sharecomponent
* @param assembleTask
*
* @param project 构建项目
* @param assembleTask 构建任务
*/
private void fetchMainmodulename(Project project, AssembleTask assembleTask) {
if (!project.rootProject.hasProperty("mainmodulename")) {
throw new RuntimeException("you should set compilemodule in rootproject's gradle.properties")
private void fetchCompileModuleName(Project project, AssembleTask assembleTask) {
if (!project.rootProject.hasProperty(MAINMODULE)) {
throw new RuntimeException("$TAG you should set compileModule in rootproject's gradle.properties")
}
if (assembleTask.modules.size() > 0 && assembleTask.modules.get(0) != null
&& assembleTask.modules.get(0).trim().length() > 0
&& !assembleTask.modules.get(0).equals("all")) {
compilemodule = assembleTask.modules.get(0);
&& assembleTask.modules.get(0) != ALLMODULE) {
compileModule = assembleTask.modules.get(0)
} else {
compilemodule = project.rootProject.property("mainmodulename")
compileModule = project.rootProject.property(MAINMODULE)
}
if (compilemodule == null || compilemodule.trim().length() <= 0) {
compilemodule = "app"
if (compileModule == null || compileModule.trim().length() <= 0) {
compileModule = DEDAULT
}
}

private AssembleTask getTaskInfo(List<String> taskNames) {
AssembleTask assembleTask = new AssembleTask();
AssembleTask assembleTask = new AssembleTask()
for (String task : taskNames) {
if (task.toUpperCase().contains("ASSEMBLE")
|| task.contains("aR")
|| task.toUpperCase().contains("RESGUARD")) {
if (task.toUpperCase().contains("DEBUG")) {
assembleTask.isDebug = true;
assembleTask.isDebug = true
}
assembleTask.isAssemble = true;
String[] strs = task.split(":")
assembleTask.modules.add(strs.length > 1 ? strs[strs.length - 2] : "all");
break;
assembleTask.isAssemble = true
//task类似这种格式[:app:assembleDebug],所以需要提取第一个冒号后面的名称
String[] split = task.split(":")
assembleTask.modules.add(split.length > 1 ? split[split.length - 2] : ALLMODULE)
break
}
}
return assembleTask
}

/**
* 自动添加依赖,只在运行assemble任务的才会添加依赖,因此在开发期间组件之间是完全感知不到的,这是做到完全隔离的关键
* 支持两种语法:module或者modulePackage:module,前者之间引用module工程,后者使用componentrelease中已经发布的aar
* @param assembleTask
* @param project
* 支持两种语法:module或者modulePackage:module,前者直接引用module工程,后者使用'componentrelease'中已经发布的aar
* @param assembleTask 构建任务
* @param project 构建项目
*/
private void compileComponents(AssembleTask assembleTask, Project project) {
String components;
String components
if (assembleTask.isDebug) {
components = (String) project.properties.get("debugComponent")
} else {
components = (String) project.properties.get("compileComponent")
}

if (components == null || components.length() == 0) {
System.out.println("there is no add dependencies ");
return;
System.out.println("$TAG there is no add dependencies ")
return
}
String[] compileComponents = components.split(",")
if (compileComponents == null || compileComponents.length == 0) {
System.out.println("there is no add dependencies ");
return;
System.out.println("$TAG there is no add dependencies ")
return
}
for (String str : compileComponents) {
System.out.println("comp is " + str);
if (str.contains(":")) {
File file = project.file("../componentrelease/" + str.split(":")[1] + "-release.aar")
for (String component : compileComponents) {
System.out.println("$TAG adding compile component [$component]")
//modulePackage:module语法格式,添加依赖aar
if (component.contains(":")) {
def aarPath = "../componentrelease/${component.split(":")[1]}-release.aar"
File file = project.file(aarPath)
if (file.exists()) {
project.dependencies.add("compile", str + "-release@aar")
System.out.println("add dependencies : " + str + "-release@aar");
project.dependencies.add("implementation", "$component-release@aar")
System.out.println("$TAG add dependencies : [$component-release@aar]")
} else {
throw new RuntimeException(str + " not found ! maybe you should generate a new one ")
throw new RuntimeException("$TAG $aarPath not found ! maybe you should generate a new one ")
}
} else {
project.dependencies.add("compile", project.project(':' + str))
System.out.println("add dependencies project : " + str);
//module语法格式,添加project依赖
project.dependencies.add("implementation", project.project(":$component"))
System.out.println("$TAG add dependencies project : [$component]")
}
}
}

/**
* 构建任务信息类
*/
private class AssembleTask {
boolean isAssemble = false;
boolean isDebug = false;
List<String> modules = new ArrayList<>();
boolean isAssemble = false
boolean isDebug = false
List<String> modules = new ArrayList<>()
}

}
Loading