Skip to content
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
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ build:windows --define=protobuf_allow_msvc=true

common --enable_platform_specific_config

common --desugar_java8_libs
8 changes: 7 additions & 1 deletion rules/android_binary/r8.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def process_r8(ctx, validation_ctx, jvm_ctx, packaged_resources_ctx, build_info_

android_jar = get_android_sdk(ctx).android_jar
proguard_specs = proguard.get_proguard_specs(ctx, packaged_resources_ctx.resource_proguard_config)
desugared_lib_config = ctx.file._desugared_lib_config

# Get min SDK version from attribute, manifest_values, or depot floor
effective_min_sdk = min_sdk_version.DEPOT_FLOOR
Expand Down Expand Up @@ -107,12 +108,17 @@ def process_r8(ctx, validation_ctx, jvm_ctx, packaged_resources_ctx, build_info_
args.add(deploy_jar) # jar to optimize + desugar + dex
args.add("--pg-map-output", proguard_mappings_output_file)

r8_inputs = [android_jar, deploy_jar] + proguard_specs
if ctx.fragments.android.desugar_java8_libs and desugared_lib_config:
args.add("--desugared-lib", desugared_lib_config)
r8_inputs.append(desugared_lib_config)

java.run(
ctx = ctx,
host_javabase = common.get_host_javabase(ctx),
executable = get_android_toolchain(ctx).r8.files_to_run,
arguments = [args],
inputs = depset([android_jar, deploy_jar] + proguard_specs, transitive = [neverlink_jars]),
inputs = depset(r8_inputs, transitive = [neverlink_jars]),
outputs = [dexes_zip, proguard_mappings_output_file],
mnemonic = "AndroidR8",
jvm_flags = ["-Xmx8G"],
Expand Down
10 changes: 10 additions & 0 deletions test/rules/android_binary/r8_integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ build_test(
name = "android_binary_with_neverlink_deps_build_test",
targets = ["//test/rules/android_binary/r8_integration/java/com/neverlink:android_binary_with_neverlink_deps"],
)

py_test(
name = "r8_desugaring_test",
srcs = ["r8_desugaring_test.py"],
args = ["$(location @androidsdk//:dexdump)"],
data = [
"//test/rules/android_binary/r8_integration/java/com/desugaring:desugaring_app_r8",
"@androidsdk//:dexdump",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.desugaring" >

<uses-sdk
android:minSdkVersion="28"
android:targetSdkVersion="34" />

<application>
<activity
android:name="com.desugaring.DesugaringActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
16 changes: 16 additions & 0 deletions test/rules/android_binary/r8_integration/java/com/desugaring/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("//rules:rules.bzl", "android_binary", "android_library")

android_library(
name = "duration_lib",
srcs = ["DurationUser.java"],
)

android_binary(
name = "desugaring_app_r8",
srcs = ["DesugaringActivity.java"],
manifest = "AndroidManifest.xml",
proguard_specs = ["proguard.cfg"],
resource_files = glob(["res/**"]),
visibility = ["//test/rules/android_binary/r8_integration:__pkg__"],
deps = [":duration_lib"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.desugaring;

import android.app.Activity;
import android.os.Bundle;
import java.time.Duration;

/** Activity that exercises Duration.toSeconds() to test core library desugaring. */
public class DesugaringActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Force the compiler to retain the call to DurationUser.getSeconds
long seconds = DurationUser.getSeconds(Duration.ofMinutes(5));
setTitle("Seconds: " + seconds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.desugaring;

import java.time.Duration;

/**
* A class that uses Duration.toSeconds(), which was added in API 31.
* This simulates a third-party library (like Google Nav SDK) that calls
* methods not available on all supported API levels.
*
* Without core library desugaring, this causes NoSuchMethodError on
* API 26-30 devices.
*/
public class DurationUser {
public static long getSeconds(Duration duration) {
// Duration.toSeconds() requires API 31+
return duration.toSeconds();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-dontobfuscate
-keep class com.desugaring.DurationUser { *; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Desugaring Test</string>
</resources>
65 changes: 65 additions & 0 deletions test/rules/android_binary/r8_integration/r8_desugaring_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import subprocess
import sys
import unittest
import zipfile


class R8DesugaringTest(unittest.TestCase):
"""Tests R8 core library desugaring integration."""

def _get_dexdump_output(self, apk_name):
tmp = os.environ["TEST_TMPDIR"]
apk_directory = "test/rules/android_binary/r8_integration/java/com/desugaring"
apk_path = os.path.join(apk_directory, apk_name)
apk_tmp = os.path.join(tmp, apk_name)

with zipfile.ZipFile(apk_path) as zf:
zf.extract("classes.dex", apk_tmp)

dexdump_proc = subprocess.run(
[dexdump, "-d", os.path.join(apk_tmp, "classes.dex")],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
return dexdump_proc.stdout.decode()

def test_duration_to_seconds_is_desugared(self):
output = self._get_dexdump_output("desugaring_app_r8.apk")

# Duration.toSeconds() (API 31) must not appear as a raw call in the DEX.
# If present, R8 was not passed --desugared-lib and the call will cause
# NoSuchMethodError on API 28-30 devices.
self.assertNotIn(
"Ljava/time/Duration;.toSeconds:()J",
output,
)

def test_desugared_duration_class_present(self):
output = self._get_dexdump_output("desugaring_app_r8.apk")

# The DurationUser class should still be in the DEX (kept by proguard rules)
self.assertIn(
"Class descriptor : 'Lcom/desugaring/DurationUser;'",
output,
)


if __name__ == "__main__":
dexdump = sys.argv.pop()
unittest.main(argv=None)