Skip to content

Commit e6b6803

Browse files
author
Dianne Hackborn
committed
New aapt feature to do smarter filtering of configurations.
This adds a --preferred-configurations flag that specifies the specific configurations you would like to have. It is smarter than "-c" because it will avoid stripping a configuration if that would result in there being no value for the resource. It is dumber than "-c" because it can't process as many kinds of resources. It is really only intended for bitmaps and use with density configs. This required re-arranging AaptAssets to group files together by config again, like they used to be. I think this hasn't broken anything. Hopefully. Change-Id: I4e9d12ff6e6dbd1abb8fd4cb1814c6674b19d0e5
1 parent d814d4f commit e6b6803

File tree

13 files changed

+615
-286
lines changed

13 files changed

+615
-286
lines changed

tools/aapt/AaptAssets.cpp

Lines changed: 338 additions & 77 deletions
Large diffs are not rendered by default.

tools/aapt/AaptAssets.h

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ using namespace android;
2424

2525
bool valid_symbol_name(const String8& str);
2626

27+
class AaptAssets;
28+
2729
enum {
2830
AXIS_NONE = 0,
2931
AXIS_MCC = 1,
@@ -45,7 +47,10 @@ enum {
4547
AXIS_SMALLESTSCREENWIDTHDP,
4648
AXIS_SCREENWIDTHDP,
4749
AXIS_SCREENHEIGHTDP,
48-
AXIS_VERSION
50+
AXIS_VERSION,
51+
52+
AXIS_START = AXIS_MCC,
53+
AXIS_END = AXIS_VERSION,
4954
};
5055

5156
enum {
@@ -56,6 +61,7 @@ enum {
5661
SDK_MR1 = 7,
5762
SDK_FROYO = 8,
5863
SDK_HONEYCOMB_MR2 = 13,
64+
SDK_ICE_CREAM_SANDWICH = 14,
5965
};
6066

6167
/**
@@ -65,35 +71,19 @@ enum {
6571
struct AaptGroupEntry
6672
{
6773
public:
68-
AaptGroupEntry() { }
74+
AaptGroupEntry() : mParamsChanged(true) { }
6975
AaptGroupEntry(const String8& _locale, const String8& _vendor)
70-
: locale(_locale), vendor(_vendor) { }
71-
72-
String8 mcc;
73-
String8 mnc;
74-
String8 locale;
75-
String8 vendor;
76-
String8 smallestScreenWidthDp;
77-
String8 screenWidthDp;
78-
String8 screenHeightDp;
79-
String8 screenLayoutSize;
80-
String8 screenLayoutLong;
81-
String8 orientation;
82-
String8 uiModeType;
83-
String8 uiModeNight;
84-
String8 density;
85-
String8 touchscreen;
86-
String8 keysHidden;
87-
String8 keyboard;
88-
String8 navHidden;
89-
String8 navigation;
90-
String8 screenSize;
91-
String8 version;
76+
: locale(_locale), vendor(_vendor), mParamsChanged(true) { }
9277

9378
bool initFromDirName(const char* dir, String8* resType);
9479

9580
static status_t parseNamePart(const String8& part, int* axis, uint32_t* value);
96-
81+
82+
static uint32_t getConfigValueForAxis(const ResTable_config& config, int axis);
83+
84+
static bool configSameExcept(const ResTable_config& config,
85+
const ResTable_config& otherConfig, int axis);
86+
9787
static bool getMccName(const char* name, ResTable_config* out = NULL);
9888
static bool getMncName(const char* name, ResTable_config* out = NULL);
9989
static bool getLocaleName(const char* name, ResTable_config* out = NULL);
@@ -116,7 +106,7 @@ struct AaptGroupEntry
116106

117107
int compare(const AaptGroupEntry& o) const;
118108

119-
ResTable_config toParams() const;
109+
const ResTable_config& toParams() const;
120110

121111
inline bool operator<(const AaptGroupEntry& o) const { return compare(o) < 0; }
122112
inline bool operator<=(const AaptGroupEntry& o) const { return compare(o) <= 0; }
@@ -127,6 +117,33 @@ struct AaptGroupEntry
127117

128118
String8 toString() const;
129119
String8 toDirName(const String8& resType) const;
120+
121+
const String8& getVersionString() const { return version; }
122+
123+
private:
124+
String8 mcc;
125+
String8 mnc;
126+
String8 locale;
127+
String8 vendor;
128+
String8 smallestScreenWidthDp;
129+
String8 screenWidthDp;
130+
String8 screenHeightDp;
131+
String8 screenLayoutSize;
132+
String8 screenLayoutLong;
133+
String8 orientation;
134+
String8 uiModeType;
135+
String8 uiModeNight;
136+
String8 density;
137+
String8 touchscreen;
138+
String8 keysHidden;
139+
String8 keyboard;
140+
String8 navHidden;
141+
String8 navigation;
142+
String8 screenSize;
143+
String8 version;
144+
145+
mutable bool mParamsChanged;
146+
mutable ResTable_config mParams;
130147
};
131148

132149
inline int compare_type(const AaptGroupEntry& lhs, const AaptGroupEntry& rhs)
@@ -225,7 +242,7 @@ class AaptGroup : public RefBase
225242
status_t addFile(const sp<AaptFile>& file);
226243
void removeFile(size_t index);
227244

228-
void print() const;
245+
void print(const String8& prefix) const;
229246

230247
String8 getPrintableSource() const;
231248

@@ -237,7 +254,7 @@ class AaptGroup : public RefBase
237254
};
238255

239256
/**
240-
* A single directory of assets, which can contain for files and other
257+
* A single directory of assets, which can contain files and other
241258
* sub-directories.
242259
*/
243260
class AaptDir : public RefBase
@@ -254,25 +271,11 @@ class AaptDir : public RefBase
254271
const DefaultKeyedVector<String8, sp<AaptGroup> >& getFiles() const { return mFiles; }
255272
const DefaultKeyedVector<String8, sp<AaptDir> >& getDirs() const { return mDirs; }
256273

257-
status_t addFile(const String8& name, const sp<AaptGroup>& file);
258-
status_t addDir(const String8& name, const sp<AaptDir>& dir);
259-
260-
sp<AaptDir> makeDir(const String8& name);
274+
virtual status_t addFile(const String8& name, const sp<AaptGroup>& file);
261275

262276
void removeFile(const String8& name);
263277
void removeDir(const String8& name);
264278

265-
status_t renameFile(const sp<AaptFile>& file, const String8& newName);
266-
267-
status_t addLeafFile(const String8& leafName,
268-
const sp<AaptFile>& file);
269-
270-
virtual ssize_t slurpFullTree(Bundle* bundle,
271-
const String8& srcDir,
272-
const AaptGroupEntry& kind,
273-
const String8& resType,
274-
sp<FilePathStore>& fullResPaths);
275-
276279
/*
277280
* Perform some sanity checks on the names of files and directories here.
278281
* In particular:
@@ -292,11 +295,23 @@ class AaptDir : public RefBase
292295
*/
293296
status_t validate() const;
294297

295-
void print() const;
298+
void print(const String8& prefix) const;
296299

297300
String8 getPrintableSource() const;
298301

299302
private:
303+
friend class AaptAssets;
304+
305+
status_t addDir(const String8& name, const sp<AaptDir>& dir);
306+
sp<AaptDir> makeDir(const String8& name);
307+
status_t addLeafFile(const String8& leafName,
308+
const sp<AaptFile>& file);
309+
virtual ssize_t slurpFullTree(Bundle* bundle,
310+
const String8& srcDir,
311+
const AaptGroupEntry& kind,
312+
const String8& resType,
313+
sp<FilePathStore>& fullResPaths);
314+
300315
String8 mLeaf;
301316
String8 mPath;
302317

@@ -501,13 +516,15 @@ class FilePathStore : public RefBase,
501516
class AaptAssets : public AaptDir
502517
{
503518
public:
504-
AaptAssets() : AaptDir(String8(), String8()), mHaveIncludedAssets(false), mRes(NULL) { }
519+
AaptAssets();
505520
virtual ~AaptAssets() { delete mRes; }
506521

507522
const String8& getPackage() const { return mPackage; }
508523
void setPackage(const String8& package) { mPackage = package; mSymbolsPrivatePackage = package; }
509524

510-
const SortedVector<AaptGroupEntry>& getGroupEntries() const { return mGroupEntries; }
525+
const SortedVector<AaptGroupEntry>& getGroupEntries() const;
526+
527+
virtual status_t addFile(const String8& name, const sp<AaptGroup>& file);
511528

512529
sp<AaptFile> addFile(const String8& filePath,
513530
const AaptGroupEntry& entry,
@@ -524,15 +541,6 @@ class AaptAssets : public AaptDir
524541

525542
ssize_t slurpFromArgs(Bundle* bundle);
526543

527-
virtual ssize_t slurpFullTree(Bundle* bundle,
528-
const String8& srcDir,
529-
const AaptGroupEntry& kind,
530-
const String8& resType,
531-
sp<FilePathStore>& fullResPaths);
532-
533-
ssize_t slurpResourceTree(Bundle* bundle, const String8& srcDir);
534-
ssize_t slurpResourceZip(Bundle* bundle, const char* filename);
535-
536544
sp<AaptSymbols> getSymbolsFor(const String8& name);
537545

538546
const DefaultKeyedVector<String8, sp<AaptSymbols> >& getSymbols() const { return mSymbols; }
@@ -544,10 +552,10 @@ class AaptAssets : public AaptDir
544552
status_t addIncludedResources(const sp<AaptFile>& file);
545553
const ResTable& getIncludedResources() const;
546554

547-
void print() const;
555+
void print(const String8& prefix) const;
548556

549-
inline const Vector<sp<AaptDir> >& resDirs() { return mDirs; }
550-
sp<AaptDir> resDir(const String8& name);
557+
inline const Vector<sp<AaptDir> >& resDirs() const { return mResDirs; }
558+
sp<AaptDir> resDir(const String8& name) const;
551559

552560
inline sp<AaptAssets> getOverlay() { return mOverlay; }
553561
inline void setOverlay(sp<AaptAssets>& overlay) { mOverlay = overlay; }
@@ -565,12 +573,25 @@ class AaptAssets : public AaptDir
565573
setFullAssetPaths(sp<FilePathStore>& res) { mFullAssetPaths = res; }
566574

567575
private:
576+
virtual ssize_t slurpFullTree(Bundle* bundle,
577+
const String8& srcDir,
578+
const AaptGroupEntry& kind,
579+
const String8& resType,
580+
sp<FilePathStore>& fullResPaths);
581+
582+
ssize_t slurpResourceTree(Bundle* bundle, const String8& srcDir);
583+
ssize_t slurpResourceZip(Bundle* bundle, const char* filename);
584+
585+
status_t filter(Bundle* bundle);
586+
568587
String8 mPackage;
569588
SortedVector<AaptGroupEntry> mGroupEntries;
570589
DefaultKeyedVector<String8, sp<AaptSymbols> > mSymbols;
571590
String8 mSymbolsPrivatePackage;
572591

573-
Vector<sp<AaptDir> > mDirs;
592+
Vector<sp<AaptDir> > mResDirs;
593+
594+
bool mChanged;
574595

575596
bool mHaveIncludedAssets;
576597
AssetManager mIncludedAssets;

tools/aapt/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LOCAL_SRC_FILES := \
1919
Package.cpp \
2020
StringPool.cpp \
2121
XMLNode.cpp \
22+
ResourceFilter.cpp \
2223
ResourceTable.cpp \
2324
Images.cpp \
2425
Resource.cpp \

tools/aapt/Bundle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Bundle {
122122
void setRClassDir(const char* dir) { mRClassDir = dir; }
123123
const char* getConfigurations() const { return mConfigurations.size() > 0 ? mConfigurations.string() : NULL; }
124124
void addConfigurations(const char* val) { if (mConfigurations.size() > 0) { mConfigurations.append(","); mConfigurations.append(val); } else { mConfigurations = val; } }
125+
const char* getPreferredConfigurations() const { return mPreferredConfigurations.size() > 0 ? mPreferredConfigurations.string() : NULL; }
126+
void addPreferredConfigurations(const char* val) { if (mPreferredConfigurations.size() > 0) { mPreferredConfigurations.append(","); mPreferredConfigurations.append(val); } else { mPreferredConfigurations = val; } }
125127
const char* getResourceIntermediatesDir() const { return mResourceIntermediatesDir; }
126128
void setResourceIntermediatesDir(const char* dir) { mResourceIntermediatesDir = dir; }
127129
const android::Vector<const char*>& getPackageIncludes() const { return mPackageIncludes; }
@@ -244,6 +246,7 @@ class Bundle {
244246
const char* mRClassDir;
245247
const char* mResourceIntermediatesDir;
246248
android::String8 mConfigurations;
249+
android::String8 mPreferredConfigurations;
247250
android::Vector<const char*> mPackageIncludes;
248251
android::Vector<const char*> mJarFiles;
249252
android::Vector<const char*> mNoCompressExtensions;

tools/aapt/Command.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
#include "Main.h"
77
#include "Bundle.h"
8+
#include "ResourceFilter.h"
89
#include "ResourceTable.h"
910
#include "XMLNode.h"
1011

@@ -1572,7 +1573,7 @@ int doPackage(Bundle* bundle)
15721573
}
15731574

15741575
if (bundle->getVerbose()) {
1575-
assets->print();
1576+
assets->print(String8());
15761577
}
15771578

15781579
// If they asked for any fileAs that need to be compiled, do so.

tools/aapt/Images.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,10 @@ status_t preProcessImage(Bundle* bundle, const sp<AaptAssets>& assets,
980980

981981
String8 printableName(file->getPrintableSource());
982982

983+
if (bundle->getVerbose()) {
984+
printf("Processing image: %s\n", printableName.string());
985+
}
986+
983987
png_structp read_ptr = NULL;
984988
png_infop read_info = NULL;
985989
FILE* fp;
@@ -1094,6 +1098,10 @@ status_t preProcessImageToCache(Bundle* bundle, String8 source, String8 dest)
10941098

10951099
status_t error = UNKNOWN_ERROR;
10961100

1101+
if (bundle->getVerbose()) {
1102+
printf("Processing image to cache: %s => %s\n", source.string(), dest.string());
1103+
}
1104+
10971105
// Get a file handler to read from
10981106
fp = fopen(source.string(),"rb");
10991107
if (fp == NULL) {

tools/aapt/Main.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ void usage(void)
6565
" [--max-res-version VAL] \\\n"
6666
" [-I base-package [-I base-package ...]] \\\n"
6767
" [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n"
68-
" [-S resource-sources [-S resource-sources ...]] "
68+
" [-S resource-sources [-S resource-sources ...]] \\\n"
6969
" [-F apk-file] [-J R-file-dir] \\\n"
7070
" [--product product1,product2,...] \\\n"
71+
" [-c CONFIGS] [--preferred-configurations CONFIGS] \\\n"
7172
" [-o] \\\n"
7273
" [raw-files-dir [raw-files-dir] ...]\n"
7374
"\n"
@@ -154,6 +155,10 @@ void usage(void)
154155
" generate dependency files in the same directories for R.java and resource package\n"
155156
" --auto-add-overlay\n"
156157
" Automatically add resources that are only in overlays.\n"
158+
" --preferred-configurations\n"
159+
" Like the -c option for filtering out unneeded configurations, but\n"
160+
" only expresses a preference. If there is no resource available with\n"
161+
" the preferred configuration then it will not be stripped.\n"
157162
" --rename-manifest-package\n"
158163
" Rewrite the manifest so that its package name is the package name\n"
159164
" given here. Relative class names (for example .Foo) will be\n"
@@ -509,6 +514,15 @@ int main(int argc, char* const argv[])
509514
bundle.setGenDependencies(true);
510515
} else if (strcmp(cp, "-utf16") == 0) {
511516
bundle.setWantUTF16(true);
517+
} else if (strcmp(cp, "-preferred-configurations") == 0) {
518+
argc--;
519+
argv++;
520+
if (!argc) {
521+
fprintf(stderr, "ERROR: No argument supplied for '--preferred-configurations' option\n");
522+
wantUsage = true;
523+
goto bail;
524+
}
525+
bundle.addPreferredConfigurations(argv[0]);
512526
} else if (strcmp(cp, "-rename-manifest-package") == 0) {
513527
argc--;
514528
argv++;

tools/aapt/Package.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Main.h"
77
#include "AaptAssets.h"
88
#include "ResourceTable.h"
9+
#include "ResourceFilter.h"
910

1011
#include <utils/Log.h>
1112
#include <utils/threads.h>

0 commit comments

Comments
 (0)