Skip to content

Instantly share code, notes, and snippets.

@programminghoch10
Created August 8, 2024 15:58
Show Gist options
  • Save programminghoch10/1194fe26f3c0642c8393921abbcce03e to your computer and use it in GitHub Desktop.
Save programminghoch10/1194fe26f3c0642c8393921abbcce03e to your computer and use it in GitHub Desktop.
statically linked aapt2 binary for android
diff --git a/Android.bp b/Android.bp
index a2a2cbdc8..16dc0e699 100644
--- a/Android.bp
+++ b/Android.bp
@@ -252,7 +252,7 @@ cc_library {
target: {
android: {
static: {
- enabled: false,
+ enabled: true,
},
},
windows: {
diff --git a/libs/androidfw/Android.bp b/libs/androidfw/Android.bp
index 77800a305f02..d668d80701f0 100644
--- a/libs/androidfw/Android.bp
+++ b/libs/androidfw/Android.bp
@@ -52,6 +52,10 @@ cc_defaults {
host: {
cflags: ["-DSTATIC_ANDROIDFW_FOR_TOOLS"],
},
+ android: {
+ cflags: ["-DSTATIC_ANDROIDFW_FOR_TOOLS"],
+ enabled: true,
+ },
},
}
@@ -126,7 +130,7 @@ cc_library {
"backup_flags_cc_lib",
],
static: {
- enabled: false,
+ enabled: true,
},
},
host: {
diff --git a/tools/aapt2/Android.bp b/tools/aapt2/Android.bp
index b054a57e85af..eecae2b1b36d 100644
--- a/tools/aapt2/Android.bp
+++ b/tools/aapt2/Android.bp
@@ -37,6 +37,7 @@ toolSources = [
cc_defaults {
name: "aapt2_defaults",
+ static_executable: true,
cpp_std: "gnu++2b",
cflags: [
"-Wall",
@@ -48,6 +49,7 @@ cc_defaults {
"-fno-exceptions",
"-fno-rtti",
"-Wno-deprecated-declarations",
+ "-Wno-non-virtual-dtor",
],
target: {
windows: {
@@ -59,6 +61,11 @@ cc_defaults {
darwin: {
cflags: ["-D_DARWIN_UNLIMITED_STREAMS"],
},
+ android: {
+ enabled: true,
+// enabled: false,
+// static: { enabled: true },
+ },
},
header_libs: ["jni_headers"],
static_libs: [
@@ -85,9 +92,9 @@ cc_defaults {
// ==========================================================
// ==========================================================
-// Build the host static library: aapt2
+// Build the static library: aapt2
// ==========================================================
-cc_library_host_static {
+cc_library_static {
name: "libaapt2",
srcs: [
"compile/IdAssigner.cpp",
@@ -167,13 +174,14 @@ cc_library_host_static {
export_proto_headers: true,
type: "full",
},
+ host_supported: true,
defaults: ["aapt2_defaults"],
}
// ==========================================================
-// Build the host tests: aapt2_tests
+// Build the tests: aapt2_tests
// ==========================================================
-cc_test_host {
+cc_test {
name: "aapt2_tests",
srcs: [
"test/Builders.cpp",
@@ -181,7 +189,7 @@ cc_test_host {
"test/Fixture.cpp",
"**/*_test.cpp",
] + toolSources,
- static_libs: [
+ whole_static_libs: [
"libaapt2",
"libgmock",
],
@@ -192,19 +200,32 @@ cc_test_host {
"integration-tests/ConvertTest/**/*",
"integration-tests/DumpTest/**/*",
],
+ host_supported: true,
+}
+
+cc_binary {
+ name: "aapt2_android",
+ srcs: ["Main.cpp"] + toolSources,
+ use_version_lib: true,
+ whole_static_libs: ["libaapt2"],
+ defaults: ["aapt2_defaults"],
+ dist: {
+// targets: ["aapt2_artifacts"],
+ },
}
// ==========================================================
-// Build the host executable: aapt2
+// Build the executable: aapt2
// ==========================================================
-cc_binary_host {
+cc_binary {
name: "aapt2",
srcs: ["Main.cpp"] + toolSources,
use_version_lib: true,
- static_libs: ["libaapt2"],
+ host_supported: true,
+ whole_static_libs: ["libaapt2"],
defaults: ["aapt2_defaults"],
dist: {
- targets: ["aapt2_artifacts"],
+// targets: ["aapt2_artifacts"],
},
}
diff --git a/tools/aapt2/link/ReferenceLinker.cpp b/tools/aapt2/link/ReferenceLinker.cpp
index c69b32513167..4de7e1d8f9d4 100644
--- a/tools/aapt2/link/ReferenceLinker.cpp
+++ b/tools/aapt2/link/ReferenceLinker.cpp
@@ -298,7 +298,7 @@ const SymbolTable::Symbol* ReferenceLinker::ResolveSymbolCheckVisibility(const R
return nullptr;
}
- if (!IsSymbolVisible(*symbol, reference, callsite)) {
+ if (!IsSymbolVisible(*symbol, reference, callsite) && false) {
if (out_error) *out_error = "is private";
return nullptr;
}
diff --git a/incfs/include/incfs_inline.h b/incfs/include/incfs_inline.h
index 68d33d1..5f04801 100644
--- a/incfs/include/incfs_inline.h
+++ b/incfs/include/incfs_inline.h
@@ -73,7 +73,10 @@ inline Features features() {
}
inline bool isIncFsFd(int fd) {
- return IncFs_IsIncFsFd(fd);
+ // useless stub code to remove the reference below
+ if (fd) return true;
+ return false;
+ //return IncFs_IsIncFsFd(fd);
}
inline bool isIncFsPath(std::string_view path) {
@@ -351,7 +354,9 @@ inline LoadingState toLoadingState(IncFsErrorCode res) {
}
inline LoadingState isFullyLoaded(int fd) {
- return toLoadingState(IncFs_IsFullyLoaded(fd));
+ // useless stub code to remove reference below
+ return toLoadingState(fd > 0);
+ //return toLoadingState(IncFs_IsFullyLoaded(fd));
}
inline LoadingState isFullyLoaded(const Control& control, std::string_view path) {
return toLoadingState(IncFs_IsFullyLoadedByPath(control, details::c_str(path)));
@@ -487,3 +492,4 @@ inline std::optional<LastReadError> getLastReadError(const Control& control) {
inline bool operator==(const IncFsFileId& l, const IncFsFileId& r) {
return memcmp(&l, &r, sizeof(l)) == 0;
}
+

Build statically linked aapt2 binaries for android

  1. Check out the android source tree normally
  2. Apply the patches from this gist
  3. Run source build/envsetup.sh
  4. Run breakfast gsi_<arch>
    where <arch> is one of: arm/arm64/x86/x86_64
  5. Run m aapt2_android
  6. Retrieve result from out/target/product/generic*/system/bin/aapt2_android
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment