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", |
| 44 | } |
| 45 | |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 46 | java_library { |
| 47 | name: "lib", |
| 48 | srcs: ["foo.java"], |
| 49 | } |
| 50 | |
| 51 | java_library { |
| 52 | name: "static_lib", |
| 53 | srcs: ["foo.java"], |
| 54 | } |
| 55 | `) |
| 56 | |
| 57 | app := result.ModuleForTests("app", "android_common") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 58 | stableApp := result.ModuleForTests("stable_app", "android_common") |
| 59 | corePlatformApp := result.ModuleForTests("core_platform_app", "android_common") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 60 | lib := result.ModuleForTests("lib", "android_common") |
| 61 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 62 | |
| 63 | appJavac := app.Rule("javac") |
| 64 | appR8 := app.Rule("r8") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 65 | stableAppR8 := stableApp.Rule("r8") |
| 66 | corePlatformAppR8 := corePlatformApp.Rule("r8") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 67 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 68 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 69 | |
| 70 | android.AssertStringDoesContain(t, "expected lib header jar in app javac classpath", |
| 71 | appJavac.Args["classpath"], libHeader.String()) |
| 72 | android.AssertStringDoesContain(t, "expected static_lib header jar in app javac classpath", |
| 73 | appJavac.Args["classpath"], staticLibHeader.String()) |
| 74 | |
| 75 | android.AssertStringDoesContain(t, "expected lib header jar in app r8 classpath", |
| 76 | appR8.Args["r8Flags"], libHeader.String()) |
| 77 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in app javac classpath", |
| 78 | appR8.Args["r8Flags"], staticLibHeader.String()) |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 79 | android.AssertStringDoesContain(t, "expected -ignorewarnings in app r8 flags", |
| 80 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 81 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 82 | appR8.Args["r8Flags"], "--android-platform-build") |
| 83 | android.AssertStringDoesNotContain(t, "expected no --android-platform-build in stable_app r8 flags", |
| 84 | stableAppR8.Args["r8Flags"], "--android-platform-build") |
| 85 | android.AssertStringDoesContain(t, "expected --android-platform-build in core_platform_app r8 flags", |
| 86 | corePlatformAppR8.Args["r8Flags"], "--android-platform-build") |
Remi NGUYEN VAN | bdad314 | 2022-08-04 13:19:03 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func TestR8Flags(t *testing.T) { |
| 90 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 91 | android_app { |
| 92 | name: "app", |
| 93 | srcs: ["foo.java"], |
| 94 | platform_apis: true, |
| 95 | optimize: { |
| 96 | shrink: false, |
| 97 | optimize: false, |
| 98 | obfuscate: false, |
| 99 | ignore_warnings: false, |
| 100 | }, |
| 101 | } |
| 102 | `) |
| 103 | |
| 104 | app := result.ModuleForTests("app", "android_common") |
| 105 | appR8 := app.Rule("r8") |
| 106 | android.AssertStringDoesContain(t, "expected -dontshrink in app r8 flags", |
| 107 | appR8.Args["r8Flags"], "-dontshrink") |
| 108 | android.AssertStringDoesContain(t, "expected -dontoptimize in app r8 flags", |
| 109 | appR8.Args["r8Flags"], "-dontoptimize") |
| 110 | android.AssertStringDoesContain(t, "expected -dontobfuscate in app r8 flags", |
| 111 | appR8.Args["r8Flags"], "-dontobfuscate") |
| 112 | android.AssertStringDoesNotContain(t, "expected no -ignorewarnings in app r8 flags", |
| 113 | appR8.Args["r8Flags"], "-ignorewarnings") |
Jared Duke | 40d731a | 2022-09-20 15:32:14 -0700 | [diff] [blame] | 114 | android.AssertStringDoesContain(t, "expected --android-platform-build in app r8 flags", |
| 115 | appR8.Args["r8Flags"], "--android-platform-build") |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func TestD8(t *testing.T) { |
| 119 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 120 | java_library { |
| 121 | name: "foo", |
| 122 | srcs: ["foo.java"], |
| 123 | libs: ["lib"], |
| 124 | static_libs: ["static_lib"], |
| 125 | installable: true, |
| 126 | } |
| 127 | |
| 128 | java_library { |
| 129 | name: "lib", |
| 130 | srcs: ["foo.java"], |
| 131 | } |
| 132 | |
| 133 | java_library { |
| 134 | name: "static_lib", |
| 135 | srcs: ["foo.java"], |
| 136 | } |
| 137 | `) |
| 138 | |
| 139 | foo := result.ModuleForTests("foo", "android_common") |
| 140 | lib := result.ModuleForTests("lib", "android_common") |
| 141 | staticLib := result.ModuleForTests("static_lib", "android_common") |
| 142 | |
| 143 | fooJavac := foo.Rule("javac") |
| 144 | fooD8 := foo.Rule("d8") |
| 145 | libHeader := lib.Output("turbine-combined/lib.jar").Output |
| 146 | staticLibHeader := staticLib.Output("turbine-combined/static_lib.jar").Output |
| 147 | |
| 148 | android.AssertStringDoesContain(t, "expected lib header jar in foo javac classpath", |
| 149 | fooJavac.Args["classpath"], libHeader.String()) |
| 150 | android.AssertStringDoesContain(t, "expected static_lib header jar in foo javac classpath", |
| 151 | fooJavac.Args["classpath"], staticLibHeader.String()) |
| 152 | |
| 153 | android.AssertStringDoesContain(t, "expected lib header jar in foo d8 classpath", |
| 154 | fooD8.Args["d8Flags"], libHeader.String()) |
| 155 | android.AssertStringDoesNotContain(t, "expected no static_lib header jar in foo javac classpath", |
| 156 | fooD8.Args["d8Flags"], staticLibHeader.String()) |
| 157 | } |
Jared Duke | 5979b30 | 2022-12-19 21:08:39 +0000 | [diff] [blame^] | 158 | |
| 159 | func TestProguardFlagsInheritance(t *testing.T) { |
| 160 | result := PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd.RunTestWithBp(t, ` |
| 161 | android_app { |
| 162 | name: "app", |
| 163 | static_libs: [ |
| 164 | "primary_android_lib", |
| 165 | "primary_lib", |
| 166 | ], |
| 167 | platform_apis: true, |
| 168 | } |
| 169 | |
| 170 | java_library { |
| 171 | name: "primary_lib", |
| 172 | optimize: { |
| 173 | proguard_flags_files: ["primary.flags"], |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | android_library { |
| 178 | name: "primary_android_lib", |
| 179 | static_libs: ["secondary_lib"], |
| 180 | optimize: { |
| 181 | proguard_flags_files: ["primary_android.flags"], |
| 182 | }, |
| 183 | } |
| 184 | |
| 185 | java_library { |
| 186 | name: "secondary_lib", |
| 187 | static_libs: ["tertiary_lib"], |
| 188 | optimize: { |
| 189 | proguard_flags_files: ["secondary.flags"], |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | java_library { |
| 194 | name: "tertiary_lib", |
| 195 | optimize: { |
| 196 | proguard_flags_files: ["tertiary.flags"], |
| 197 | }, |
| 198 | } |
| 199 | `) |
| 200 | |
| 201 | app := result.ModuleForTests("app", "android_common") |
| 202 | appR8 := app.Rule("r8") |
| 203 | android.AssertStringDoesContain(t, "expected primary_lib's proguard flags from direct dep", |
| 204 | appR8.Args["r8Flags"], "primary.flags") |
| 205 | android.AssertStringDoesContain(t, "expected primary_android_lib's proguard flags from direct dep", |
| 206 | appR8.Args["r8Flags"], "primary_android.flags") |
| 207 | android.AssertStringDoesContain(t, "expected secondary_lib's proguard flags from inherited dep", |
| 208 | appR8.Args["r8Flags"], "secondary.flags") |
| 209 | android.AssertStringDoesContain(t, "expected tertiary_lib's proguard flags from inherited dep", |
| 210 | appR8.Args["r8Flags"], "tertiary.flags") |
| 211 | } |