Skip to content

Commit

Permalink
fix support for library
Browse files Browse the repository at this point in the history
  • Loading branch information
Bleoo committed Feb 5, 2018
1 parent fd5f95f commit 0df37c9
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 72 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ android {
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public class FileProcessor {
}

JavaFile javaFile = JavaFile.builder(PACKAGE_NAME, typeSpecBuilder.build())
.addFileComment('Generated code from Joanna. Do not modify!')
.build()
javaFile.writeTo(outputDir)
}

private MethodSpec.Builder getConstructorBuilder(String targetClass) {
MethodSpec.Builder constructor = MethodSpec.constructorBuilder()
.addComment('Generated code from Joanna. Do not modify!')
.addModifiers(Modifier.PUBLIC)
.addParameter(ClassTypeHelper.getTargetClassType(targetClass), targetClass.toLowerCase())
return constructor
Expand Down
73 changes: 40 additions & 33 deletions buildsrc/src/main/groovy/io/bleoo/test/Joanna.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,64 @@ package io.bleoo.test

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.api.ApplicationVariantImpl
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.internal.api.BaseVariantImpl
import org.gradle.api.DomainObjectSet
import org.gradle.api.Plugin
import org.gradle.api.Project

public class Joanna implements Plugin<Project> {

void apply(Project project) {
println 'Joanna plugin apply!'
AppExtension android = project.extensions.getByType(AppExtension)
// android.registerTransform(new MyTransform(project, android))

if (project.plugins.hasPlugin(AppPlugin)) {
android.applicationVariants.all { ApplicationVariantImpl variant ->
project.plugins.all { Plugin plugin ->
if (plugin instanceof AppPlugin) {
configureViewBinderGeneration(project, project.extensions.getByType(AppExtension).applicationVariants)
} else if (plugin instanceof LibraryPlugin) {
configureViewBinderGeneration(project, project.extensions.getByType(LibraryExtension).libraryVariants)
}
}
}

//创建一个task
def createTaskName = variant.variantData.scope.getTaskName('generate', 'ViewBinder')
def createTask = project.task(createTaskName)
File outputDir = variant.variantData.scope.buildConfigSourceOutputDir
private void configureViewBinderGeneration(Project project, DomainObjectSet<BaseVariantImpl> variants) {
variants.all { BaseVariantImpl variant ->
//创建一个task
def createTaskName = variant.variantData.scope.getTaskName('generate', 'ViewBinder')
def createTask = project.task(createTaskName)
File outputDir = variant.variantData.scope.buildConfigSourceOutputDir
// def createTask = project.tasks.create('generateViewBinder')
// File outputDir = new File(project.buildDir.absolutePath
// + File.separator + 'generated'
// + File.separator + 'source'
// + File.separator + 'viewBinder')
// createTask.outputs.dir(outputDir.absolutePath)
// variant.registerJavaGeneratingTask(createTask, outputDir)
//设置task要执行的任务
createTask.doLast {
println 'Joanna plugin start!'
//设置task要执行的任务
createTask.doLast {
println 'Joanna plugin start!'

ProjectProcessor processor = new ProjectProcessor(project, outputDir)
String manifestFilePath = project.projectDir.absolutePath + File.separator +
'src' + File.separator +
'main' + File.separator +
'AndroidManifest.xml'
processor.processPackageName(manifestFilePath)
String layoutDirPath = project.projectDir.absolutePath + File.separator +
'src' + File.separator +
'main' + File.separator +
'res' + File.separator +
'layout'
processor.processLayoutXml(layoutDirPath)
}
//设置task依赖于生成BuildConfig的task,然后在生成BuildConfig后生成我们的类
String generateBuildConfigTaskName = variant.variantData.scope.generateBuildConfigTask.name
def generateBuildConfigTask = project.tasks.getByName(generateBuildConfigTaskName)
if (generateBuildConfigTask) {
createTask.dependsOn(generateBuildConfigTask)
generateBuildConfigTask.finalizedBy(createTask)
}
ProjectProcessor processor = new ProjectProcessor(project, outputDir)
String manifestFilePath = project.projectDir.absolutePath +
File.separator + 'src' +
File.separator + 'main' +
File.separator + 'AndroidManifest.xml'
processor.processPackageName(manifestFilePath)
String layoutDirPath = project.projectDir.absolutePath +
File.separator + 'src' +
File.separator + 'main' +
File.separator + 'res' +
File.separator + 'layout'
processor.processLayoutXml(layoutDirPath)
}
//设置task依赖于生成BuildConfig的task,然后在生成BuildConfig后生成我们的类
String generateBuildConfigTaskName = variant.variantData.scope.generateBuildConfigTask.name
def generateBuildConfigTask = project.tasks.getByName(generateBuildConfigTaskName)
if (generateBuildConfigTask) {
createTask.dependsOn(generateBuildConfigTask)
generateBuildConfigTask.finalizedBy(createTask)
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.bleoo.test

import com.android.build.gradle.internal.api.ApplicationVariantImpl
import groovy.util.slurpersupport.GPathResult
import org.gradle.api.Project
import org.gradle.api.file.FileTree
Expand Down Expand Up @@ -38,7 +37,9 @@ public class ProjectProcessor {
viewId = viewId.substring(viewId.indexOf('/') + 1)
viewMap.put(viewId, view.name())
}
fileProcessor.createViewBinder(file.name, viewMap)
if (viewMap.size() > 0) {
fileProcessor.createViewBinder(file.name, viewMap)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public class FileProcessor {
}

JavaFile javaFile = JavaFile.builder(PACKAGE_NAME, typeSpecBuilder.build())
.addFileComment('Generated code from Joanna. Do not modify!')
.build()
javaFile.writeTo(outputDir)
}

private MethodSpec.Builder getConstructorBuilder(String targetClass) {
MethodSpec.Builder constructor = MethodSpec.constructorBuilder()
.addComment('Generated code from Joanna. Do not modify!')
.addModifiers(Modifier.PUBLIC)
.addParameter(ClassTypeHelper.getTargetClassType(targetClass), targetClass.toLowerCase())
return constructor
Expand Down
71 changes: 39 additions & 32 deletions joanna-plugin/src/main/groovy/io/bleoo/Joanna.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,64 @@ package io.bleoo

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.internal.api.ApplicationVariantImpl
import com.android.build.gradle.internal.api.BaseVariantImpl
import org.gradle.api.DomainObjectSet
import org.gradle.api.Plugin
import org.gradle.api.Project

public class Joanna implements Plugin<Project> {

void apply(Project project) {
println 'Joanna plugin apply!'
AppExtension android = project.extensions.getByType(AppExtension)

if (project.plugins.hasPlugin(AppPlugin) || project.plugins.hasPlugin(LibraryPlugin)) {
android.applicationVariants.all { ApplicationVariantImpl variant ->
project.plugins.all { Plugin plugin ->
if (plugin instanceof AppPlugin) {
configureViewBinderGeneration(project, project.extensions.getByType(AppExtension).applicationVariants)
} else if (plugin instanceof LibraryPlugin) {
configureViewBinderGeneration(project, project.extensions.getByType(LibraryExtension).libraryVariants)
}
}
}

//创建一个task
def createTaskName = variant.variantData.scope.getTaskName('generate', 'ViewBinder')
def createTask = project.task(createTaskName)
File outputDir = variant.variantData.scope.buildConfigSourceOutputDir
private void configureViewBinderGeneration(Project project, DomainObjectSet<BaseVariantImpl> variants) {
variants.all { BaseVariantImpl variant ->
//创建一个task
def createTaskName = variant.variantData.scope.getTaskName('generate', 'ViewBinder')
def createTask = project.task(createTaskName)
File outputDir = variant.variantData.scope.buildConfigSourceOutputDir
// def createTask = project.tasks.create('generateViewBinder')
// File outputDir = new File(project.buildDir.absolutePath
// + File.separator + 'generated'
// + File.separator + 'source'
// + File.separator + 'viewBinder')
// createTask.outputs.dir(outputDir.absolutePath)
// variant.registerJavaGeneratingTask(createTask, outputDir)
//设置task要执行的任务
createTask.doLast {
println 'Joanna plugin start!'
//设置task要执行的任务
createTask.doLast {
println 'Joanna plugin start!'

ProjectProcessor processor = new ProjectProcessor(project, outputDir)
String manifestFilePath = project.projectDir.absolutePath + File.separator +
'src' + File.separator +
'main' + File.separator +
'AndroidManifest.xml'
processor.processPackageName(manifestFilePath)
String layoutDirPath = project.projectDir.absolutePath + File.separator +
'src' + File.separator +
'main' + File.separator +
'res' + File.separator +
'layout'
processor.processLayoutXml(layoutDirPath)
}
//设置task依赖于生成BuildConfig的task,然后在生成BuildConfig后生成我们的类
String generateBuildConfigTaskName = variant.variantData.scope.generateBuildConfigTask.name
def generateBuildConfigTask = project.tasks.getByName(generateBuildConfigTaskName)
if (generateBuildConfigTask) {
createTask.dependsOn(generateBuildConfigTask)
generateBuildConfigTask.finalizedBy(createTask)
}
ProjectProcessor processor = new ProjectProcessor(project, outputDir)
String manifestFilePath = project.projectDir.absolutePath +
File.separator + 'src' +
File.separator + 'main' +
File.separator + 'AndroidManifest.xml'
processor.processPackageName(manifestFilePath)
String layoutDirPath = project.projectDir.absolutePath +
File.separator + 'src' +
File.separator + 'main' +
File.separator + 'res' +
File.separator + 'layout'
processor.processLayoutXml(layoutDirPath)
}
//设置task依赖于生成BuildConfig的task,然后在生成BuildConfig后生成我们的类
String generateBuildConfigTaskName = variant.variantData.scope.generateBuildConfigTask.name
def generateBuildConfigTask = project.tasks.getByName(generateBuildConfigTaskName)
if (generateBuildConfigTask) {
createTask.dependsOn(generateBuildConfigTask)
generateBuildConfigTask.finalizedBy(createTask)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class ProjectProcessor {
viewId = viewId.substring(viewId.indexOf('/') + 1)
viewMap.put(viewId, view.name())
}
fileProcessor.createViewBinder(file.name, viewMap)
if (viewMap.size() > 0) {
fileProcessor.createViewBinder(file.name, viewMap)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':joanna-plugin', ':buildsrc'
include ':app', ':joanna-plugin', ':buildsrc', ':testlibrary'
1 change: 1 addition & 0 deletions testlibrary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions testlibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.library'
//apply plugin: 'io.bleoo.test.Joanna'

android {
compileSdkVersion 26

defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}
21 changes: 21 additions & 0 deletions testlibrary/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
10 changes: 10 additions & 0 deletions testlibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.bleoo.testlibrary">

<application>
<activity android:name=".Module1Activity" />
<activity android:name=".Module2Activity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.bleoo.testlibrary;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Module1Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_module);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.bleoo.testlibrary;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Module2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_module2);
}
}
14 changes: 14 additions & 0 deletions testlibrary/src/main/res/layout/activity_module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.github.bleoo.testlibrary.Module1Activity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="module test"/>

</android.support.constraint.ConstraintLayout>
15 changes: 15 additions & 0 deletions testlibrary/src/main/res/layout/activity_module2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.github.bleoo.testlibrary.Module2Activity">

<TextView
android:id="@+id/tv_test_module2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Module2Activity"/>

</android.support.constraint.ConstraintLayout>
3 changes: 3 additions & 0 deletions testlibrary/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">TestLibrary</string>
</resources>

0 comments on commit 0df37c9

Please sign in to comment.