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

Kotlin incremental Support - Stable Version #919

Open
wants to merge 17 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@ class FreelineAnnotationCollector {
"Ljavax/inject/Inject;": "Inject"
]

public static final def CUSTOM_ANNOTATION_TARGETS = [:]

private static def sAnnotationCollection = [:]

// 收集所有注解信息 以供后续观察 DSL自定义注解规则可以参考
public static final def DEBUG_ANNOTATION_COLLECTOR = new HashSet<String>()

public static void addNewAnno(String anno, String path, String className, String entry, boolean isJar) {
String key = ANNOTATION_TARGETS[anno]

println "custom anno settings enabled :)==> $CUSTOM_ANNOTATION_TARGETS"

if (key == null){
CUSTOM_ANNOTATION_TARGETS.keySet().each { annoToken ->
if (anno.contains(annoToken)){
key = CUSTOM_ANNOTATION_TARGETS[annoToken]
}
}
}

if (!sAnnotationCollection.containsKey(key)) {
sAnnotationCollection[key] = []
//print 出增加适配的key
println "new anno --> ${(['path': path, 'className': className, 'entry': entry, 'isJar': isJar]).toString()}"
}

sAnnotationCollection[key].add(['path': path, 'className': className, 'entry': entry, 'isJar': isJar])
Expand Down Expand Up @@ -62,6 +80,10 @@ class FreelineAnnotationCollector {
println json
FreelineUtils.saveJson(json, FreelineUtils.joinPath(buildCacheDirPath, "freeline_annotation_info.json"), true)

def allAnnotationJson = new JsonBuilder(DEBUG_ANNOTATION_COLLECTOR).toPrettyString()
println allAnnotationJson
FreelineUtils.saveJson(allAnnotationJson, FreelineUtils.joinPath(buildCacheDirPath, "freeline_debug_annotation_collection.json"), true)

sAnnotationCollection.clear()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class FreelineClassVisitor extends ClassVisitor implements Opcodes {
@Override
AnnotationVisitor visitAnnotation(String desc, boolean visible) {
collectAnno(desc)
println "anno-- class $className--> $desc"
return super.visitAnnotation(desc, visible)
}

Expand Down Expand Up @@ -94,6 +95,7 @@ class FreelineClassVisitor extends ClassVisitor implements Opcodes {

@Override
AnnotationVisitor visitAnnotation(String annoDesc, boolean visible) {
println "anno-- method $name--> $annoDesc"
collectAnno(annoDesc)
return super.visitAnnotation(annoDesc, visible)
}
Expand All @@ -107,6 +109,7 @@ class FreelineClassVisitor extends ClassVisitor implements Opcodes {
return new FieldVisitor(ASM4, fv) {
@Override
AnnotationVisitor visitAnnotation(String annoDesc, boolean visible) {
println "anno-- field $name--> $annoDesc"
collectAnno(annoDesc)
return super.visitAnnotation(annoDesc, visible)
}
Expand All @@ -121,6 +124,10 @@ class FreelineClassVisitor extends ClassVisitor implements Opcodes {
FreelineAnnotationCollector.addNewAnno(anno, filePath, className, entry, isJar)
}
}
// 收集所有的注解信息已供自定义注解处理器规则的书写
if (!FreelineAnnotationCollector.DEBUG_ANNOTATION_COLLECTOR.contains(desc)){
FreelineAnnotationCollector.DEBUG_ANNOTATION_COLLECTOR.add(desc)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class FreelineExtension {

String launcher = ""

String kotlinPath = "kotlinc"

List<String> extraResourceDependencyPaths = []

List<String> excludeResourceDependencyPaths = []
Expand All @@ -31,6 +33,8 @@ class FreelineExtension {

List<String> checkSourcesMd5 = []

Map<String,String> annotationMap = [:]

boolean foceLowerVersion = false

boolean applicationProxy = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class FreelineInitializer {
def ignoreResourceIds = extension.ignoreResourceIds
def checkSourcesMd5 = extension.checkSourcesMd5
def useSystemGradle = extension.useSystemGradle
def annotationMap = extension.annotationMap
def kotlincPath = extension.kotlinPath
def customAnnotationSupportEnabled = false

println "anno custom Map =====> $annotationMap"

def projectDescription = [:]

Expand Down Expand Up @@ -68,6 +73,14 @@ class FreelineInitializer {
projectDescription.use_jdk8 = isUseJdk8(projectDescription.android_gradle_plugin_version as String)
projectDescription.ignore_resource_ids = FreelineCompat.compatIgnoreResourceIds(ignoreResourceIds)
projectDescription.use_system_gradle = useSystemGradle
projectDescription.annotationMap = annotationMap
projectDescription.kotlincPath = kotlincPath

if (annotationMap.size() > 0){
customAnnotationSupportEnabled = true
}

projectDescription.customAnnotationSupportEnabled = customAnnotationSupportEnabled //加上这个是否开启的bool值 省的在python里面判断

def useMd5PathArray = [];
for (String path : checkSourcesMd5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ class FreelinePlugin implements Plugin<Project> {
}
}
}

if (customAnnotationSupportEnabled){
extension.annotationMap.each { entry ->
//在 Freeline注解收集器中注册
FreelineAnnotationCollector.CUSTOM_ANNOTATION_TARGETS.put(entry.key,entry.value)
FreelineAnnotationCollector.ANNOTATION_CLASSES.add(entry.key)
}
}

}

// find databinding compiler jar path
Expand Down
28 changes: 26 additions & 2 deletions freeline_core/android_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ def get_backup_dir(self):
os.makedirs(backup_dir)
return backup_dir

# 也是用来解决kotlin适配之后的R类增量问题 之前的@get_backup_dir 并不能搜索到library里面的R类
def get_freeline_backup_r_dir(self):
dirpath = os.path.join(self._cache_dir, 'freeline-backup-r')
if not os.path.exists(dirpath):
os.makedirs(dirpath)
return dirpath

def get_backup_res_dir(self):
dir_path = os.path.join(self.get_backup_dir(), 'res')
if not os.path.isdir(dir_path):
Expand Down Expand Up @@ -511,7 +518,7 @@ def check_r_md5(self):
old_md5 = None
old_r_file = self._finder.get_dst_r_path(config=self._config)
self.debug("{} old R.java path: {}".format(self._name, old_r_file))
new_r_file = DirectoryFinder.get_r_file_path(self._finder.get_backup_dir())
new_r_file = DirectoryFinder.get_r_file_path(self._finder.get_freeline_backup_r_dir())
self.debug("{} new R.java path: {}".format(self._name, new_r_file))
if old_r_file and os.path.exists(old_r_file):
old_md5 = get_md5(old_r_file)
Expand All @@ -534,9 +541,10 @@ def fix_for_windows(path):
if is_windows_system():
buf = fix_unicode_parse_error(get_file_content(path), path)
write_file_content(path, buf)

#todo 检查R文件增量问题 library模块里面的
def check_javac_task(self):
changed_count = len(self._changed_files['src'])
res_changed_count = len(self._changed_files['res'])
apt_changed_count = 0
if 'apt' in self._changed_files:
apt_changed_count = len(self._changed_files['apt'])
Expand Down Expand Up @@ -567,12 +575,26 @@ def check_javac_task(self):
elif apt_changed_count != 0:
self.debug('{} has apt files changed so that it need javac task.'.format(self._name))
self._is_need_javac = True
elif res_changed_count != 0:
self.debug('{} has res files changeed so that it need javac task for R.java'.format(self._name))
# 照顾到Kotlin部分的R.java
self._is_need_javac = True
else:
self.debug('{} code only change R.java, need not go ahead'.format(self._name))
self._is_need_javac = False
else:
self._is_need_javac = True

return self._is_need_javac

def check_kotlinc_task(self):
changed_count = len(self._changed_files['kotlin'])
if changed_count == 0:
self.debug('{} project kotlin files have no change, need not go ahead'.format(self._name))
return False
else:
return True

def _is_only_r_changed(self):
is_only_r_changed = True
for fpath in self._changed_files['src']:
Expand All @@ -581,6 +603,8 @@ def _is_only_r_changed(self):
else:
self._is_r_file_changed = True
self.debug('find R.java modified in src list')
if len(self._changed_files['kotlin']) > 0:
is_only_r_changed = False
return is_only_r_changed

def fill_classpaths(self):
Expand Down
8 changes: 8 additions & 0 deletions freeline_core/build_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def execute(self):
self._invoker.run_javac_task()


class IncKotlincCommand(MacroCommand):
def __init__(self, pro, invoker):
MacroCommand.__init__(self, '{}_inc_kotlinc_compile'.format(pro))
self._invoker = invoker
def execute(self):
self._invoker.run_kotlinc_task()


class IncDexCommand(MacroCommand):
def __init__(self, pro, invoker):
MacroCommand.__init__(self, '{}_inc_dex_compile'.format(pro))
Expand Down
Loading