Skip to content

Commit 90e4b72

Browse files
ajsinclaircopybara-github
authored andcommitted
Support for desugaring Java 17 Records.
There are three dex flows: * mobile-install: This uses the dex archive format of the globals * optimized: this uses the class JAR formate of the globals * non-optimized: this passes the plain globals directly to the dexmerger One input to the command to generate the globals file is the min SDK version. For now we have a global value. This means we can define the targets using genrules. If the min SDK version becomes dynamic based on a configuration value the targets will have to be ported to custom rules. The open source flow doesn't work currently but most of the components are there. PiperOrigin-RevId: 688746011 Change-Id: I9abfdb209580598f02fd03b884301b3d2f1e7c05
1 parent 0fe7386 commit 90e4b72

File tree

12 files changed

+124
-13
lines changed

12 files changed

+124
-13
lines changed

mobile_install/BUILD

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ bzl_library(
3131
],
3232
),
3333
deps = [
34-
":constants",
35-
":launcher_direct",
3634
":providers",
37-
":resources",
3835
":transform",
3936
":utils",
4037
"//mobile_install/adapters:bzl",
41-
"//rules:bzl",
4238
"//rules:min_sdk_version_bzl",
39+
"//rules:utils_bzl",
4340
"//rules:visibility_bzl",
41+
"//rules/flags:bzl",
4442
"@rules_java//java/common",
4543
],
4644
)

mobile_install/mi.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Aspect for mobile-install."""
1515

1616
load("//rules:min_sdk_version.bzl", "min_sdk_version")
17-
load("//rules:utils.bzl", "ANDROID_SDK_TOOLCHAIN_TYPE")
17+
load("//rules:utils.bzl", "ANDROID_SDK_TOOLCHAIN_TYPE", "ANDROID_TOOLCHAIN_TYPE")
1818
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
1919
load("//rules/flags:flags.bzl", "flags")
2020
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
@@ -85,6 +85,7 @@ def make_aspect(
8585
implementation = aspect_impl,
8686
toolchains = [
8787
ANDROID_SDK_TOOLCHAIN_TYPE,
88+
ANDROID_TOOLCHAIN_TYPE,
8889
],
8990
**opt_kwargs
9091
)

mobile_install/process.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Processes the target or collected data."""
1515

16+
load("//rules:utils.bzl", "get_android_toolchain", "utils")
1617
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
1718
load(":apks.bzl", "make_split_apks")
1819
load(":native_libs.bzl", "make_native_zip", "make_swigdeps_file")
@@ -81,6 +82,9 @@ def process(
8182
sibling,
8283
)
8384

85+
if hasattr(get_android_toolchain(ctx), "desugar_globals_dex_archive"):
86+
merged_dex_shards.append(utils.only(get_android_toolchain(ctx).desugar_globals_dex_archive.files.to_list()))
87+
8488
# Creates the custom R.
8589
r_dex = make_r(
8690
ctx,

rules/acls/record_desugaring.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
# limitations under the License.
1414
"""Allowlist to enable Java 17 Record desugaring."""
1515

16-
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
17-
18-
visibility(PROJECT_VISIBILITY)
19-
2016
# keep sorted
2117
RECORD_DESUGARING_ROLLOUT = [
2218
]

rules/android_binary/impl.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ def _process_deploy_jar(ctx, validation_ctx, stamp_ctx, packaged_resources_ctx,
466466
dex_archives = info.dex_archives_dict.get("".join(incremental_dexopts), depset()).to_list()
467467
if ctx.fragments.android.desugar_java8:
468468
desugared_jars = []
469+
if acls.in_record_desugaring_rollout(str(ctx.label)):
470+
desugared_jars.append(utils.only(get_android_toolchain(ctx).desugar_globals_jar.files.to_list()))
469471
desugar_dict = {d.jar: d.desugared_jar for d in dex_archives if d.desugared_jar}
470472

471473
for jar in binary_runtime_jars:

rules/dex.bzl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,22 @@ def _process_incremental_dexing(
189189
toolchain_type = toolchain_type,
190190
)
191191

192-
# TODO(b/130571505): Implement this after SpawnActionTemplate is supported in Starlark
193-
android_common.create_dex_merger_actions(
194-
ctx,
192+
# TODO(asinclair): Remove this once the rollout is complete.
193+
dex_merger_actions_args = dict(
195194
output = dexes,
196195
input = shards,
197196
dexopts = dexopts,
198197
dexmerger = dexmerger,
199198
min_sdk_version = min_sdk_version,
200199
)
200+
if acls.in_record_desugaring_rollout(str(ctx.label)):
201+
dex_merger_actions_args["desugar_globals"] = utils.only(get_android_toolchain(ctx).desugar_globals.files.to_list())
202+
203+
# TODO(b/130571505): Implement this after SpawnActionTemplate is supported in Starlark
204+
android_common.create_dex_merger_actions(
205+
ctx,
206+
**dex_merger_actions_args
207+
)
201208
_java.singlejar(
202209
ctx,
203210
output = output,

src/java/com/example/sampleapp/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package="com.example.sampleapp"
44
android:versionCode="1"
55
android:versionName="1.0">
6-
<uses-sdk android:minSdkVersion="21" />
6+
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34"/>
77
<application android:label="@string/app_name"
88
android:debuggable="true">
99
<activity android:name=".SampleApp"

src/java/com/example/sampleapp/SampleApp.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public class SampleApp extends Activity {
2828
public void onCreate(Bundle state) {
2929
super.onCreate(state);
3030

31+
SampleRecord record = new SampleRecord("name", 1);
32+
3133
setContentView(R.layout.basic_activity);
34+
TextView tv = findViewById(R.id.record_text);
35+
tv.setText(record.toString());
3236
}
3337

3438
public native String getString();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2024 The Bazel Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.sampleapp;
17+
18+
public record SampleRecord(String name, int value) {}

src/java/com/example/sampleapp/res/layout/basic_activity.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
android:layout_width="wrap_content"
1010
android:layout_height="wrap_content" />
1111

12+
<TextView
13+
android:id="@+id/record_text"
14+
android:text="@string/hello_world"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content" />
17+
1218
</LinearLayout>

0 commit comments

Comments
 (0)