0

generate_gradle.py: Fix junit_binary(), enable --all by default

Also updates templates for gradle plugin 3.0 and makes
--split-project work again for Chromium codebase.

Bug: 620034, webrtc:8547, 786676
Change-Id: I89e937ff8a70cf30f890765b98efa0d331fba77e
Reviewed-on: https://chromium-review.googlesource.com/779499
Commit-Queue: agrieve <agrieve@chromium.org>
Reviewed-by: Peter Wen <wnwen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#518312}
This commit is contained in:
Andrew Grieve
2017-11-21 18:41:10 +00:00
committed by Commit Bot
parent 7cb9efa887
commit 5b89b23829
5 changed files with 46 additions and 54 deletions

@ -49,7 +49,6 @@ apply plugin: "com.android.application"
android {
compileSdkVersion "{{ compile_sdk_version }}"
buildToolsVersion "{{ build_tools_version }}"
publishNonDefault true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
@ -81,12 +80,6 @@ android {
{% include 'dependencies.jinja' %}
android.variantFilter { variant ->
if (variant.buildType.name.equals('release')) {
variant.setIgnore(true);
}
}
afterEvaluate {
def tasksToDisable = tasks.findAll {
return (it.name.equals('generateDebugSources') // causes unwanted AndroidManifest.java

@ -22,7 +22,7 @@
{% endmacro %}
dependencies {
{{ expand_deps(main, 'compile') }}
{{ expand_deps(test, 'testCompile') }}
{{ expand_deps(android_test, 'androidTestCompile') }}
{{ expand_deps(main, 'implementation') }}
{{ expand_deps(test, 'testImplementation') }}
{{ expand_deps(android_test, 'androidTestImplementation') }}
}

@ -42,10 +42,6 @@ _GRADLE_BUILD_FILE = 'build.gradle'
_MODULE_ALL = '_all'
_DEFAULT_TARGETS = [
# TODO(agrieve): .build_config seem not quite right for this target
# because it has resources as deps of android_apk() rather than using an
# android_library() intermediate target.
# '//android_webview:system_webview_apk',
'//android_webview/test/embedded_test_server:aw_net_test_support_apk',
'//android_webview/test:webview_instrumentation_apk',
'//android_webview/test:webview_instrumentation_test_apk',
@ -57,6 +53,8 @@ _DEFAULT_TARGETS = [
'//chrome/android:chrome_sync_shell_test_apk',
'//content/public/android:content_junit_tests',
'//content/shell/android:content_shell_apk',
# Needed even with --all since it's a library.
'//tools/android/errorprone_plugin:errorprone_plugin_java',
]
_EXCLUDED_PREBUILT_JARS = [
@ -197,7 +195,7 @@ class _ProjectEntry(object):
def ProjectName(self):
"""Returns the Gradle project name."""
return self.GradleSubdir().replace(os.path.sep, '>')
return self.GradleSubdir().replace(os.path.sep, '.')
def BuildConfig(self):
"""Reads and returns the project's .build_config JSON."""
@ -220,7 +218,8 @@ class _ProjectEntry(object):
return self.DepsInfo()['type']
def IsValid(self):
return self.GetType() in ('android_apk', 'java_library', 'java_binary')
return self.GetType() in (
'android_apk', 'java_library', 'java_binary', 'junit_binary')
def ResZips(self):
return self.DepsInfo().get('owned_resources_zips', [])
@ -546,13 +545,12 @@ def _GenerateGradleFile(entry, generator, build_vars, source_properties,
else:
target_type = 'java_library'
elif deps_info['type'] == 'java_binary':
if gradle['main_class'] == 'org.chromium.testing.local.JunitTestMain':
target_type = 'android_junit'
variables['sourceSetName'] = 'test'
variables['depCompileName'] = 'testCompile'
else:
target_type = 'java_binary'
variables['main_class'] = gradle['main_class']
target_type = 'java_binary'
variables['main_class'] = gradle['main_class']
elif deps_info['type'] == 'junit_binary':
target_type = 'android_junit'
variables['sourceSetName'] = 'test'
variables['depCompileName'] = 'testCompile'
else:
return None
@ -727,7 +725,10 @@ def main():
default=os.path.join('$CHROMIUM_OUTPUT_DIR', 'gradle'))
parser.add_argument('--all',
action='store_true',
help='Generate all java targets (slows down IDE)')
help='Include all .java files reachable from any '
'apk/test/binary target. On by default unless '
'--split-projects is used (--split-projects can '
'slow down Studio given too many targets).')
parser.add_argument('--use-gradle-process-resources',
action='store_true',
help='Have gradle generate R.java rather than ninja')
@ -763,35 +764,38 @@ def main():
args.canary)
logging.warning('Creating project at: %s', generator.project_dir)
args.all = args.all or not args.split_projects
targets_from_args = set(args.targets or _DEFAULT_TARGETS)
if args.extra_targets:
targets_from_args.update(args.extra_targets)
if args.all:
# Run GN gen if necessary (faster than running "gn gen" in the no-op case).
_RunNinja(constants.GetOutDirectory(), ['build.ninja'])
# Query ninja for all __build_config targets.
targets = _QueryForAllGnTargets(output_dir)
else:
targets = args.targets or _DEFAULT_TARGETS
if args.extra_targets:
targets.extend(args.extra_targets)
targets = [re.sub(r'_test_apk$', '_test_apk__apk', t) for t in targets]
# TODO(wnwen): Utilize Gradle's test constructs for our junit tests?
targets = [re.sub(r'_junit_tests$', '_junit_tests__java_binary', t)
for t in targets]
targets = [re.sub(r'_test_apk$', '_test_apk__apk', t)
for t in targets_from_args]
main_entries = [_ProjectEntry.FromGnTarget(t) for t in targets]
logging.warning('Building .build_config files...')
_RunNinja(output_dir, [e.NinjaBuildConfigTarget() for e in main_entries])
if args.split_projects:
if args.all:
# There are many unused libraries, so restrict to those that are actually
# used when using --all with individual project entries.
if args.all:
main_entries = [e for e in main_entries if (
e.GetType() == 'android_apk' or
e.GnTarget().endswith('_test_apk__apk') or
e.GnTarget().endswith('_junit_tests__java_binary'))]
# used by apks/binaries/tests or that are explicitly mentioned in --targets.
main_entries = [e for e in main_entries if (
e.GetType() in ('android_apk', 'java_binary', 'junit_binary') or
e.GnTarget() in targets_from_args or
e.GnTarget().endswith('_test_apk__apk'))]
if args.split_projects:
main_entries = _FindAllProjectEntries(main_entries)
logging.info('Found %d dependent build_config targets.', len(main_entries))
logging.info('Generating for %d targets.', len(main_entries))
entries = [e for e in _CombineTestEntries(main_entries) if e.IsValid()]
logging.info('Creating %d projects for targets.', len(entries))
@ -845,8 +849,8 @@ def main():
_ExtractZips(generator.project_dir, zip_tuples)
logging.warning('Project created!')
logging.warning('Generated projects work with %s',
'Android Studio ' + '3.1' if args.canary else '3.0')
logging.warning('Generated projects work with Android Studio %s',
'3.1' if args.canary else '3.0')
logging.warning('For more tips: https://chromium.googlesource.com/chromium'
'/src.git/+/master/docs/android_studio.md')

@ -1172,6 +1172,7 @@ if (enable_java_templates) {
_java_binary_target_name = "${target_name}__java_binary"
_test_runner_target_name = "${target_name}__test_runner_script"
_main_class = "org.chromium.testing.local.JunitTestMain"
_build_config = "$target_gen_dir/$target_name.build_config"
_build_config_target_name = "${target_name}__build_config"
@ -1194,6 +1195,7 @@ if (enable_java_templates) {
write_build_config(_build_config_target_name) {
type = "junit_binary"
build_config = _build_config
main_class = _main_class
# Robolectric can handle deps that set !supports_android as well those
# that set requires_android.
@ -1242,7 +1244,7 @@ if (enable_java_templates) {
if (defined(_java_sources_file)) {
java_sources_file = _java_sources_file
}
main_class = "org.chromium.testing.local.JunitTestMain"
main_class = _main_class
wrapper_script_name = "helper/$target_name"
if (!defined(srcjar_deps)) {
srcjar_deps = []

@ -8,20 +8,13 @@ Make sure you have followed
[android build instructions](android_build_instructions.md) already.
```shell
build/android/gradle/generate_gradle.py [--canary] # Use --canary for Android Studio 3.1 canary
build/android/gradle/generate_gradle.py --output-directory out/Debug [--canary] # Use --canary for Android Studio 3.1 canary
```
This creates a project at `out/Debug/gradle`. To create elsewhere:
```shell
build/android/gradle/generate_gradle.py --output-directory out/My-Out-Dir --project-dir my-project
```
By default, common targets are generated. To add more targets to generate
projects for:
```shell
build/android/gradle/generate_gradle.py --extra-target //chrome/android:chrome_public_apk
build/android/gradle/generate_gradle.py --output-directory out/Debug --project-dir my-project
```
For first-time Android Studio users:
@ -150,10 +143,11 @@ resources, native libraries, etc.
### What works
* Android Studio v3.0 and v3.1 canary with `--canary` flag.
* Java editing and gradle compile.
* Java editing and gradle compile (mostly).
* Instrumentation tests included as androidTest.
* Symlinks to existing .so files in jniLibs (doesn't generate them).
* Editing resource xml files.
* Editing resource xml files
* Layout editor (somewhat :P).
* Java debugging (see
[here](/docs/android_debugging_instructions.md#Android-Studio)).
* Import resolution and refactoring across all modules.
@ -162,6 +156,5 @@ resources, native libraries, etc.
### What doesn't work (yet) ([crbug](https://bugs.chromium.org/p/chromium/issues/detail?id=620034))
* Gradle being aware of assets.
* Layout editor.
* Native code editing.
* Having the "Make Project" button work correctly.