1
+ #! /bin/bash
2
+ #
3
+ # Copyright 2023 The Bazel Authors. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # For these tests to run do the following:
18
+ #
19
+ # 1. Install an Android SDK from https://developer.android.com
20
+ # 2. Set the $ANDROID_HOME environment variable
21
+ # 3. Uncomment the line in WORKSPACE containing android_sdk_repository
22
+ #
23
+ # Note that if the environment is not set up as above android_integration_test
24
+ # will silently be ignored and will be shown as passing.
25
+
26
+ # Load the test setup defined in the parent directory
27
+ CURRENT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
28
+
29
+ source " ${CURRENT_DIR} /android_helper.sh" \
30
+ || { echo " android_helper.sh not found!" >&2 ; exit 1; }
31
+ fail_if_no_android_sdk
32
+
33
+ source " ${CURRENT_DIR} /../../integration_test_setup.sh" \
34
+ || { echo " integration_test_setup.sh not found!" >&2 ; exit 1; }
35
+
36
+ resolve_android_toolchains " $1 "
37
+
38
+ function test_DexFileSplitter_synthetic_classes_crossing_dexfiles() {
39
+ create_new_workspace
40
+ setup_android_sdk_support
41
+
42
+ mkdir -p java/com/testapp
43
+
44
+ cat > java/com/testapp/AndroidManifest.xml << EOF
45
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
46
+ package="com.testapp"
47
+ android:versionCode="1"
48
+ android:versionName="1.0" >
49
+
50
+ <uses-sdk
51
+ android:minSdkVersion="30"
52
+ android:targetSdkVersion="30" />
53
+
54
+ <application android:label="Test App" >
55
+ <activity
56
+ android:name="com.testapp.MainActivity"
57
+ android:label="App"
58
+ android:exported="true">
59
+ <intent-filter>
60
+ <action android:name="android.intent.action.MAIN" />
61
+ <category android:name="android.intent.category.LAUNCHER" />
62
+ </intent-filter>
63
+ </activity>
64
+ </application>
65
+ </manifest>
66
+ EOF
67
+
68
+ cat > java/com/testapp/MainActivity.java << EOF
69
+ package com.testapp;
70
+
71
+ import android.app.Activity;
72
+ import android.os.Bundle;
73
+ import android.util.Log;
74
+
75
+ public class MainActivity extends Activity {
76
+ @Override
77
+ public void onCreate(Bundle savedInstanceState) {
78
+ super.onCreate(savedInstanceState);
79
+ Log.i("tag", "info");
80
+ }
81
+ }
82
+ EOF
83
+
84
+ generate_java_file_with_many_synthetic_classes > java/com/testapp/BigLib.java
85
+
86
+ cat > java/com/testapp/BUILD << EOF
87
+ android_binary(
88
+ name = "testapp",
89
+ srcs = [
90
+ "MainActivity.java",
91
+ ":BigLib.java",
92
+ ],
93
+ manifest = "AndroidManifest.xml",
94
+ )
95
+ EOF
96
+
97
+ bazel build java/com/testapp || fail " Test app should have built succesfully"
98
+
99
+ dex_file_count=$( unzip -l bazel-bin/java/com/testapp/testapp.apk | grep " classes[0-9]*.dex" | wc -l)
100
+ if [[ ! " $dex_file_count " -ge " 2" ]]; then
101
+ echo " Expected at least 2 dexes in app, found: $dex_file_count "
102
+ exit 1
103
+ fi
104
+ }
105
+
106
+
107
+ function generate_java_file_with_many_synthetic_classes() {
108
+ echo " package com.testapp;"
109
+ echo " public class BigLib {"
110
+
111
+ # First generate enough inner classes to fill up most of the dex
112
+ for (( i = 0 ; i < 21400 ; i++ )) do
113
+
114
+ echo " public static class Bar$i {"
115
+ echo " public int bar() {"
116
+ echo " return $i ;"
117
+ echo " }"
118
+ echo " }"
119
+
120
+ done
121
+
122
+ # Then create enough synethetic classes via lambdas to fill up the rest of the
123
+ # dex and into another dex file.
124
+ echo " public interface IntSupplier {"
125
+ echo " int supply();"
126
+ echo " }"
127
+
128
+ echo " public static class Foo {"
129
+ echo " public IntSupplier[] foo() {"
130
+ echo " return new IntSupplier[] {"
131
+
132
+ for (( i = 0 ; i < 6000 ; i++ )) do
133
+ echo " () -> $i ,"
134
+ done
135
+
136
+ echo " };"
137
+ echo " }"
138
+ echo " }"
139
+ echo " }"
140
+ }
141
+
142
+ run_suite " Tests for DexFileSplitter with synthetic classes crossing dexfiles"
0 commit comments