Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | func TestR8(t *testing.T) { |
| 24 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 25 | android_app { |
| 26 | name: "app", |
| 27 | srcs: ["foo.java"], |
| 28 | libs: ["lib"], |
| 29 | static_libs: ["static_lib"], |
| 30 | platform_apis: true, |
| 31 | } |
| 32 | |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 33 | android_app { |
| 34 | name: "stable_app", |
| 35 | srcs: ["foo.java"], |
| 36 | sdk_version: "current", |
| 37 | min_sdk_version: "31", |
| 38 | } |
| 39 | |
| 40 | android_app { |
| 41 | name: "core_platform_app", |
| 42 | srcs: ["foo.java"], |
| 43 | sdk_version: "core_platform", |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame^] | 44 | min_sdk_version: "31", |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 47 | java_library { |
| 48 | name: "lib", |
| 49 | srcs: ["foo.java"], |
| 50 | } |
| 51 | |
| 52 | java_library { |
| 53 | name: "static_lib", |
| 54 | srcs: ["foo.java"], |
| 55 | } |
| 56 | `) |
| 57 | |
| 58 | app := result.ModuleForTests("app", "android_common") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 59 | stableApp := result.ModuleForTests("stable_app", "android_common") |
| 60 | corePlatformApp := result.ModuleForTests("core_platform_app", "android_common") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 61 | lib := result.ModuleForTests("lib", "android_common") |
| 62 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 63 | |
| 64 | appJavac := app.Rule("javac") |
| 65 | appR8 := app.Rule("r8") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 66 | stableAppR8 := stableApp.Rule("r8") |
| 67 | corePlatformAppR8 := corePlatformApp.Rule("r8") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 68 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 69 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 70 | |
| 71 | android.AssertStringDoesContain(t, "expected lib header jar in app javac classpath", |
| 72 | appJavac.Args["classpath"], libHeader.String()) |
| 73 | android.AssertStringDoesContain(t, "expected static_lib header jar in app javac classpath", |
| 74 | appJavac.Args["classpath"], staticLibHeader.String()) |
| 75 | |
| 76 | android.AssertStringDoesContain(t, "expected lib header jar in app r8 classpath", |
| 77 | appR8.Args["r8Flags"], libHeader.String()) |
| 78 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in app javac classpath", |
| 79 | appR8.Args["r8Flags"], staticLibHeader.String()) |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 80 | android.AssertStringDoesContain(t, "expected -ignorewarnings in app r8 flags", |
| 81 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 82 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 83 | appR8.Args["r8Flags"], "--android-platform-build") |
| 84 | android.AssertStringDoesNotContain(t, "expected no --android-platform-build in stable_app r8 flags", |
| 85 | stableAppR8.Args["r8Flags"], "--android-platform-build") |
| 86 | android.AssertStringDoesContain(t, "expected --android-platform-build in core_platform_app r8 flags", |
| 87 | corePlatformAppR8.Args["r8Flags"], "--android-platform-build") |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func TestR8Flags(t *testing.T) { |
| 91 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 92 | android_app { |
| 93 | name: "app", |
| 94 | srcs: ["foo.java"], |
| 95 | platform_apis: true, |
| 96 | optimize: { |
| 97 | shrink: false, |
| 98 | optimize: false, |
| 99 | obfuscate: false, |
| 100 | ignore_warnings: false, |
| 101 | }, |
| 102 | } |
| 103 | `) |
| 104 | |
| 105 | app := result.ModuleForTests("app", "android_common") |
| 106 | appR8 := app.Rule("r8") |
| 107 | android.AssertStringDoesContain(t, "expected -dontshrink in app r8 flags", |
| 108 | appR8.Args["r8Flags"], "-dontshrink") |
| 109 | android.AssertStringDoesContain(t, "expected -dontoptimize in app r8 flags", |
| 110 | appR8.Args["r8Flags"], "-dontoptimize") |
| 111 | android.AssertStringDoesContain(t, "expected -dontobfuscate in app r8 flags", |
| 112 | appR8.Args["r8Flags"], "-dontobfuscate") |
| 113 | android.AssertStringDoesNotContain(t, "expected no -ignorewarnings in app r8 flags", |
| 114 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 115 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 116 | appR8.Args["r8Flags"], "--android-platform-build") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func TestD8(t *testing.T) { |
| 120 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 121 | java_library { |
| 122 | name: "foo", |
| 123 | srcs: ["foo.java"], |
| 124 | libs: ["lib"], |
| 125 | static_libs: ["static_lib"], |
| 126 | installable: true, |
| 127 | } |
| 128 | |
| 129 | java_library { |
| 130 | name: "lib", |
| 131 | srcs: ["foo.java"], |
| 132 | } |
| 133 | |
| 134 | java_library { |
| 135 | name: "static_lib", |
| 136 | srcs: ["foo.java"], |
| 137 | } |
| 138 | `) |
| 139 | |
| 140 | foo := result.ModuleForTests("foo", "android_common") |
| 141 | lib := result.ModuleForTests("lib", "android_common") |
| 142 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 143 | |
| 144 | fooJavac := foo.Rule("javac") |
| 145 | fooD8 := foo.Rule("d8") |
| 146 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 147 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 148 | |
| 149 | android.AssertStringDoesContain(t, "expected lib header jar in foo javac classpath", |
| 150 | fooJavac.Args["classpath"], libHeader.String()) |
| 151 | android.AssertStringDoesContain(t, "expected static_lib header jar in foo javac classpath", |
| 152 | fooJavac.Args["classpath"], staticLibHeader.String()) |
| 153 | |
| 154 | android.AssertStringDoesContain(t, "expected lib header jar in foo d8 classpath", |
| 155 | fooD8.Args["d8Flags"], libHeader.String()) |
| 156 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in foo javac classpath", |
| 157 | fooD8.Args["d8Flags"], staticLibHeader.String()) |
| 158 | } |
Jared Duke | 5979b30 | 2022-12-19 21:08:39 +0000 | [diff] [blame] | 159 | |
| 160 | func TestProguardFlagsInheritance(t *testing.T) { |
| 161 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 162 | android_app { |
| 163 | name: "app", |
| 164 | static_libs: [ |
| 165 | "primary_android_lib", |
| 166 | "primary_lib", |
| 167 | ], |
| 168 | platform_apis: true, |
| 169 | } |
| 170 | |
| 171 | java_library { |
| 172 | name: "primary_lib", |
| 173 | optimize: { |
| 174 | proguard_flags_files: ["primary.flags"], |
| 175 | }, |
| 176 | } |
| 177 | |
| 178 | android_library { |
| 179 | name: "primary_android_lib", |
| 180 | static_libs: ["secondary_lib"], |
| 181 | optimize: { |
| 182 | proguard_flags_files: ["primary_android.flags"], |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | java_library { |
| 187 | name: "secondary_lib", |
| 188 | static_libs: ["tertiary_lib"], |
| 189 | optimize: { |
| 190 | proguard_flags_files: ["secondary.flags"], |
| 191 | }, |
| 192 | } |
| 193 | |
| 194 | java_library { |
| 195 | name: "tertiary_lib", |
| 196 | optimize: { |
| 197 | proguard_flags_files: ["tertiary.flags"], |
| 198 | }, |
| 199 | } |
| 200 | `) |
| 201 | |
| 202 | app := result.ModuleForTests("app", "android_common") |
| 203 | appR8 := app.Rule("r8") |
| 204 | android.AssertStringDoesContain(t, "expected primary_lib's proguard flags from direct dep", |
| 205 | appR8.Args["r8Flags"], "primary.flags") |
| 206 | android.AssertStringDoesContain(t, "expected primary_android_lib's proguard flags from direct dep", |
| 207 | appR8.Args["r8Flags"], "primary_android.flags") |
| 208 | android.AssertStringDoesContain(t, "expected secondary_lib's proguard flags from inherited dep", |
| 209 | appR8.Args["r8Flags"], "secondary.flags") |
| 210 | android.AssertStringDoesContain(t, "expected tertiary_lib's proguard flags from inherited dep", |
| 211 | appR8.Args["r8Flags"], "tertiary.flags") |
| 212 | } |