From e6b2e2632671812ac41b2b9b725f80bce04c04e4 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 21:36:24 -0800 Subject: [PATCH 01/12] ADFA-7138 Add sqlite-rewrite-runtime, the helpers the requery rewrite redirects to The build-time rewrite (next commits) moves an app from android.database.sqlite to requery, which lacks a few platform entry points: open flags, CancellationSignal overloads, Context.openOrCreateDatabase and deleteDatabase, and the DatabaseUtils query helpers. The rewritten classes call static helpers for these instead. The helpers are a Java-only module, so one AAR serves every ABI. Code on the Go injects it into apps; it is not shipped in the template's libs/, so it always matches the plugin's rules. The module is registered in settings.gradle.kts and in AndroidModuleConf, which builds it as a release AAR for the apps built with Code on the Go. Tests: the pure helper functions run on the JVM in the gradle-plugin tests (next commit). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../plugins/conf/AndroidModuleConf.kt | 13 +- settings.gradle.kts | 1 + .../sqlite-rewrite-runtime/build.gradle.kts | 30 +++ .../sqliterewrite/SqliteRewriteRuntime.java | 171 ++++++++++++++++++ 4 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 subprojects/sqlite-rewrite-runtime/build.gradle.kts create mode 100644 subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java diff --git a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt index e418cb787a..3330baec4d 100644 --- a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt +++ b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/conf/AndroidModuleConf.kt @@ -55,15 +55,24 @@ private val disableCoreLibDesugaringForModules = ":subprojects:sqlite-js", // Injected into debug builds of apps built with CoGo like :logsender (ADFA-7131). ":subprojects:sqlite-debugger", + // Injected into every variant of apps built with CoGo that opted into the rewrite, for the + // rewritten classes to call (ADFA-7138). + ":subprojects:sqlite-rewrite-runtime", ) /** * Modules without the v7/v8 flavors: libraries for other projects, which need one artifact. * :subprojects:sqlite-js and :subprojects:sqlite-debugger are AARs for apps built with CoGo, with - * all the ABIs they run on. + * all the ABIs they run on. :subprojects:sqlite-rewrite-runtime is Java-only, one AAR for apps + * built with CoGo. */ private val modulesWithoutAbiFlavors = - setOf(":plugin-api", ":subprojects:sqlite-js", ":subprojects:sqlite-debugger") + setOf( + ":plugin-api", + ":subprojects:sqlite-js", + ":subprojects:sqlite-debugger", + ":subprojects:sqlite-rewrite-runtime", + ) /** * The name of the build type used for automated testing. diff --git a/settings.gradle.kts b/settings.gradle.kts index a3b77f05a5..0912ba830d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -172,6 +172,7 @@ include( ":subprojects:shizuku-starter", ":subprojects:sqlite-debugger", ":subprojects:sqlite-js", + ":subprojects:sqlite-rewrite-runtime", ":subprojects:tooling-api", ":subprojects:tooling-api-events", ":subprojects:tooling-api-impl", diff --git a/subprojects/sqlite-rewrite-runtime/build.gradle.kts b/subprojects/sqlite-rewrite-runtime/build.gradle.kts new file mode 100644 index 0000000000..1ca7d55699 --- /dev/null +++ b/subprojects/sqlite-rewrite-runtime/build.gradle.kts @@ -0,0 +1,30 @@ +plugins { + id("com.android.library") +} + +description = "Runtime helpers for classes rewritten from android.database.sqlite to requery (ADFA-7138)" + +android { + namespace = "org.appdevforall.sqliterewrite" + + defaultConfig { + // requery's own minSdk (its AAR manifest says 21); apps rewritten to use it need no more + minSdk = 21 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + buildFeatures { + viewBinding = false + buildConfig = false + } +} + +dependencies { + // Not packaged: requery's AAR is a file dependency, which brings none of its own dependencies, so + // androidx.core comes from the app (AppCompat and most androidx libraries depend on it) + compileOnly(libs.androidx.core) +} diff --git a/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java b/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java new file mode 100644 index 0000000000..8e810f980f --- /dev/null +++ b/subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java @@ -0,0 +1,171 @@ +package org.appdevforall.sqliterewrite; + +import android.content.Context; +import android.database.DatabaseErrorHandler; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteStatement; +import android.os.CancellationSignal; +import android.text.TextUtils; + +import java.io.File; + +/** + * Targets of the calls that CoGo's Gradle plugin redirects when it rewrites an app from android.database.sqlite to requery (ADFA-7138). + * + *

+ * The methods are written against Android's classes on purpose: the plugin rewrites this library like every other class of the app, so {@code SQLiteDatabase} and {@code DatabaseErrorHandler} below become requery's in the final APK. The plugin leaves this package's own calls alone, otherwise a redirect would call itself. + * + *

+ * Flag mapping, Android -> requery 3.49.0 (read from its classes.jar): + * + *

+ * OPEN_READWRITE (0)                           -> OPEN_READWRITE (2)
+ * OPEN_READONLY (1)                            -> OPEN_READONLY (1)
+ * CREATE_IF_NECESSARY (0x10000000)             -> OPEN_CREATE (4), so 6 with read-write; dropped with OPEN_READONLY
+ * ENABLE_WRITE_AHEAD_LOGGING (0x20000000)      -> same value in requery, kept
+ * NO_LOCALIZED_COLLATORS (16)                  -> dropped, requery has no collator support
+ * any other bit                                -> dropped
+ * 
+ * + * Other bits are dropped because requery hands them to sqlite3_open_v2, where 16 and 8 mean SQLITE_OPEN_EXCLUSIVE and SQLITE_OPEN_DELETEONCLOSE: an Android flag must never turn into one of those. + */ +public final class SqliteRewriteRuntime { + private static final int ANDROID_OPEN_READONLY = 1; + private static final int ANDROID_CREATE_IF_NECESSARY = 0x10000000; + private static final int ANDROID_ENABLE_WRITE_AHEAD_LOGGING = 0x20000000; + + private static final int REQUERY_OPEN_READONLY = 1; + private static final int REQUERY_OPEN_READWRITE = 2; + private static final int REQUERY_OPEN_CREATE = 4; + private static final int REQUERY_ENABLE_WRITE_AHEAD_LOGGING = 0x20000000; + + private static final int CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x8; + + /** + * Same as Context.deleteDatabase, which also returns false on any exception. requery's deleteDatabase removes the -journal, -wal, -shm and -mj files next to the file, like Android's. + */ + public static boolean deleteDatabase(Context context, String name) { + try { + return SQLiteDatabase.deleteDatabase(context.getDatabasePath(name)); + } catch (Exception e) { + return false; + } + } + + public static long longForQuery(SQLiteDatabase db, String query, String[] selectionArgs) { + SQLiteStatement prog = db.compileStatement(query); + try { + return longForQuery(prog, selectionArgs); + } finally { + prog.close(); + } + } + + public static long longForQuery(SQLiteStatement prog, String[] selectionArgs) { + prog.bindAllArgsAsStrings(selectionArgs); + return prog.simpleQueryForLong(); + } + + public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, + int flags) { + return SQLiteDatabase.openDatabase(path, factory, translateOpenFlags(flags)); + } + + public static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, + int flags, DatabaseErrorHandler errorHandler) { + return SQLiteDatabase.openDatabase(path, factory, translateOpenFlags(flags), + errorHandler); + } + + public static SQLiteDatabase openOrCreateDatabase(Context context, String name, int mode, + SQLiteDatabase.CursorFactory factory) { + return openOrCreateDatabase(context, name, mode, factory, null); + } + + /** + * Context.openOrCreateDatabase without the file permission bits of {@code mode}: Android applies them with a hidden API, and the modes other than private were deprecated in API 17. + */ + public static SQLiteDatabase openOrCreateDatabase(Context context, String name, int mode, + SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { + int flags = ANDROID_CREATE_IF_NECESSARY; + if ((mode & CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { + flags |= ANDROID_ENABLE_WRITE_AHEAD_LOGGING; + } + File file = context.getDatabasePath(name); + File parent = file.getParentFile(); + if (parent != null && !parent.isDirectory()) { + // getDatabasePath does this for the default directory only + parent.mkdirs(); + } + return openDatabase(file.getPath(), factory, flags, errorHandler); + } + + /** + * The DatabaseUtils methods that take SQLiteDatabase or SQLiteStatement, as Android implements them (requery has no DatabaseUtils). They call only members requery has, so they stay on this class. + */ + public static long queryNumEntries(SQLiteDatabase db, String table) { + return queryNumEntries(db, table, null, null); + } + + public static long queryNumEntries(SQLiteDatabase db, String table, String selection) { + return queryNumEntries(db, table, selection, null); + } + + public static long queryNumEntries(SQLiteDatabase db, String table, String selection, + String[] selectionArgs) { + String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : ""; + return longForQuery(db, "select count(*) from " + table + s, selectionArgs); + } + + public static String stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs) { + SQLiteStatement prog = db.compileStatement(query); + try { + return stringForQuery(prog, selectionArgs); + } finally { + prog.close(); + } + } + + public static String stringForQuery(SQLiteStatement prog, String[] selectionArgs) { + prog.bindAllArgsAsStrings(selectionArgs); + return prog.simpleQueryForString(); + } + + /** + * Android's signal for requery's. Android's signal holds one listener, so this replaces any listener the caller set on it (Android's setOnCancelListener documents that it replaces the previous one). It fires at once when the signal is cancelled already. + */ + public static androidx.core.os.CancellationSignal toCancellationSignal( + CancellationSignal signal) { + if (signal == null) { + return null; + } + final androidx.core.os.CancellationSignal converted = new androidx.core.os.CancellationSignal(); + if (signal.isCanceled()) { + converted.cancel(); + return converted; + } + signal.setOnCancelListener(new CancellationSignal.OnCancelListener() { + @Override + public void onCancel() { + converted.cancel(); + } + }); + return converted; + } + + /** Converts Android's open flags to requery's. */ + public static int translateOpenFlags(int androidFlags) { + boolean readOnly = (androidFlags & ANDROID_OPEN_READONLY) != 0; + int flags = readOnly ? REQUERY_OPEN_READONLY : REQUERY_OPEN_READWRITE; + // SQLite accepts READONLY, READWRITE and READWRITE|CREATE, and rejects READONLY|CREATE + if (!readOnly && (androidFlags & ANDROID_CREATE_IF_NECESSARY) != 0) { + flags |= REQUERY_OPEN_CREATE; + } + if ((androidFlags & ANDROID_ENABLE_WRITE_AHEAD_LOGGING) != 0) { + flags |= REQUERY_ENABLE_WRITE_AHEAD_LOGGING; + } + return flags; + } + + private SqliteRewriteRuntime() {} +} From e392fe9f31dcd8a8ca475e086a5f19725394e186 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 21:36:24 -0800 Subject: [PATCH 02/12] ADFA-7138 Rewrite android.database.sqlite to requery in the Gradle plugin Apps built in Code on the Go that opt in must run on requery's SQLite (3.49.0) so the debugger can hook it. SqliteRewriter is an ASM class visitor, applied by an AGP transform to the app's and its libraries' classes: - renames android.database.sqlite types to requery's, and String[] descriptors to Object[]; - redirects open flags, CancellationSignal, Context.openOrCreateDatabase and deleteDatabase, and the DatabaseUtils query helpers to sqlite-rewrite-runtime; - fails the build, naming class, method and line, for APIs requery lacks: OpenParams, COLLATE UNICODE (requery has no ICU), casts to Android's cursor classes, method handles, unredirectable Context database calls; - copies frames (COPY_FRAMES), inserting a String[] cast at the start of a method whose parameter is retyped to Object[] so its frames stay valid; memoizes hierarchy lookups; keeps the cursor taint at join points; looks up missing members through every renamed ancestor; adds OPEN_CREATE only to read-write opens. androidx.core is exempt from the build-failure rule: its SQLiteCursorCompat calls setFillWindowForwardOnly, which a real transform run found missing. FrameworkSQLiteOpenHelperFactory is not redirected to requery's factory, which ignores noBackupDirectory and allowDataLossOnRecovery; WorkManager sets both, so its database would be recreated empty on every start. androidx's framework classes are rewritten instead and honour both options. Tests: SqliteRewriterTest and SqliteRewriteResolutionTest rewrite Java samples and the real androidx.sqlite 2.6.2 framework classes and check that every reference resolves in requery 3.49.0 (except execPerConnectionSQL, which throws UnsupportedOperationException when called; Room 2.8.4 and WorkManager 2.10.0 were surveyed with javap and run on a device); BytecodeVerifier runs the JVM verifier over rewritten classes; the runtime helper's pure functions run on the JVM. The tests fail on CI when their jars are missing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/build.gradle.kts | 16 + .../gradle/sqlite/SqliteRewriteRules.kt | 271 ++++ .../sqlite/SqliteRewriteVisitorFactory.kt | 34 + .../gradle/sqlite/SqliteRewriter.kt | 759 ++++++++++ .../gradle/sqlite/BytecodeVerifier.kt | 98 ++ .../gradle/sqlite/BytecodeVerifierTest.kt | 58 + .../sqlite/SqliteRewriteResolutionTest.kt | 349 +++++ .../gradle/sqlite/SqliteRewriteRuntimeTest.kt | 48 + .../gradle/sqlite/SqliteRewriterTest.kt | 1299 +++++++++++++++++ .../gradle/sqlite/SqliteTestSupport.kt | 278 ++++ gradle/libs.versions.toml | 3 + 11 files changed, 3213 insertions(+) create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index 06560adcf6..d631c8404a 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -93,10 +93,21 @@ dependencies { // claim the minAgpCheck guard below keeps honest by recompiling them against it. add("androidBuildTool", libs.android.gradle.plugin) + // The SQLite rewrite (ADFA-7138). AGP's runtime classpath carries ASM, but its API + // artifacts expose only asm itself, so the rewrite compiles against the rest of it here. + compileOnly(libs.ow2.asm) + compileOnly(libs.ow2.asmCommons) + compileOnly(libs.ow2.asmTree) + compileOnly(libs.ow2.asmAnalysis) + testImplementation(gradleTestKit()) testImplementation(libs.tests.junit.jupiter) testImplementation(libs.tests.google.truth) testImplementation(projects.shared) + testImplementation(libs.ow2.asm) + testImplementation(libs.ow2.asmCommons) + testImplementation(libs.ow2.asmTree) + testImplementation(libs.ow2.asmAnalysis) testRuntimeOnly(libs.tests.junit.platformLauncher) } @@ -125,6 +136,10 @@ dependencies { "minAgpCheckImplementation"(libs.composite.constants) "minAgpCheckImplementation"(projects.gradlePluginConfig) "minAgpCheckImplementation"(projects.buildInfo) + "minAgpCheckCompileOnly"(libs.ow2.asm) + "minAgpCheckCompileOnly"(libs.ow2.asmCommons) + "minAgpCheckCompileOnly"(libs.ow2.asmTree) + "minAgpCheckCompileOnly"(libs.ow2.asmAnalysis) } tasks.named("check") { @@ -160,6 +175,7 @@ gradlePlugin { "Gradle plugin for applying LogSender-specific configuration to projects that are built with AndroidIDE" tags.set(setOf("androidide", "logsender")) } + } } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt new file mode 100644 index 0000000000..56281d05e7 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRules.kt @@ -0,0 +1,271 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +/** + * The rule tables of the android.database.sqlite -> requery rewrite (ADFA-7138), from the Wave 0 + * comparison of android.jar against requery 3.49.0. Names are JVM internal names and descriptors + * with Android's original names: the rename runs after the other rules. + */ +internal object SqliteRewriteRules { + const val SQLITE_PKG = "android/database/sqlite/" + private const val REQUERY_SQLITE_PKG = "io/requery/android/database/sqlite/" + private const val REQUERY_DB_PKG = "io/requery/android/database/" + + const val DATABASE = "${SQLITE_PKG}SQLiteDatabase" + const val CURSOR_FACTORY = "${SQLITE_PKG}SQLiteDatabase\$CursorFactory" + const val CURSOR_DRIVER = "${SQLITE_PKG}SQLiteCursorDriver" + const val OPEN_HELPER = "${SQLITE_PKG}SQLiteOpenHelper" + const val QUERY_BUILDER = "${SQLITE_PKG}SQLiteQueryBuilder" + const val SQLITE_CURSOR = "${SQLITE_PKG}SQLiteCursor" + const val OPEN_PARAMS = "${SQLITE_PKG}SQLiteDatabase\$OpenParams" + const val ERROR_HANDLER = "android/database/DatabaseErrorHandler" + const val ABSTRACT_WINDOWED_CURSOR = "android/database/AbstractWindowedCursor" + const val ABSTRACT_CURSOR = "android/database/AbstractCursor" + const val CROSS_PROCESS_CURSOR = "android/database/CrossProcessCursor" + + /** + * Android cursor types that requery's cursors do not extend or implement: its SQLiteCursor extends + * its own AbstractWindowedCursor and AbstractCursor, which implement only android.database.Cursor. + */ + val ANDROID_ONLY_CURSOR_TYPES: Set = setOf(ABSTRACT_WINDOWED_CURSOR, ABSTRACT_CURSOR, CROSS_PROCESS_CURSOR) + const val CURSOR_WINDOW = "android/database/CursorWindow" + const val ANDROID_SIGNAL = "android/os/CancellationSignal" + const val CORE_SIGNAL = "androidx/core/os/CancellationSignal" + const val CONTEXT = "android/content/Context" + + val COLLATE_UNICODE = Regex("COLLATE\\s+UNICODE", RegexOption.IGNORE_CASE) + val COLLATE_LOCALIZED = Regex("COLLATE\\s+LOCALIZED", RegexOption.IGNORE_CASE) + + const val HELPER = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" + const val HELPER_PACKAGE_PREFIX = "org/appdevforall/sqliterewrite/" + const val FRAMEWORK_PACKAGE_PREFIX = "androidx/sqlite/db/framework/" + + /** + * androidx.core's SQLiteCursorCompat calls SQLiteCursor.setFillWindowForwardOnly, which requery lacks, from a + * helper only an app that calls the compat method reaches. Every app that depends on androidx.core has the + * class, so failing the build for it would fail them all. + */ + const val CORE_DATABASE_PACKAGE_PREFIX = "androidx/core/database/" + const val REQUERY_PACKAGE_PREFIX = "io/requery/" + + /** The 10 shared classes and the two error handlers requery's copies use. */ + val RENAMES: Map = + buildMap { + for (name in listOf( + "SQLiteClosable", + "SQLiteCursor", + "SQLiteCursorDriver", + "SQLiteDatabase", + "SQLiteDatabase\$CursorFactory", + "SQLiteOpenHelper", + "SQLiteProgram", + "SQLiteQuery", + "SQLiteQueryBuilder", + "SQLiteStatement", + )) { + put("$SQLITE_PKG$name", "$REQUERY_SQLITE_PKG$name") + } + put(ERROR_HANDLER, "${REQUERY_DB_PKG}DatabaseErrorHandler") + put("android/database/DefaultDatabaseErrorHandler", "${REQUERY_DB_PKG}DefaultDatabaseErrorHandler") + } + + /** The Android classes that get renamed. */ + val SQLITE_CLASSES: Set = RENAMES.keys.filter { it.startsWith(SQLITE_PKG) }.toSet() + + /** Renamed superclasses of the renamed classes, from the Wave 0 supertypes.tsv. */ + val RENAMED_SUPERS: Map> = + mapOf( + "${SQLITE_PKG}SQLiteDatabase" to listOf("${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteProgram" to listOf("${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteQuery" to listOf("${SQLITE_PKG}SQLiteProgram", "${SQLITE_PKG}SQLiteClosable"), + "${SQLITE_PKG}SQLiteStatement" to listOf("${SQLITE_PKG}SQLiteProgram", "${SQLITE_PKG}SQLiteClosable"), + ) + + /** + * Members requery lacks, or has with another meaning. Key: "owner.name" -> descriptors, or an + * empty set for every descriptor (fields and the like). + */ + val MISSING_MEMBERS: Map> = + buildMap> { + fun no( + owner: String, + name: String, + desc: String = "", + ) { + getOrPut("$SQLITE_PKG$owner.$name") { mutableSetOf() }.add(desc) + } + no("SQLiteClosable", "onAllReferencesReleasedFromContainer", "()V") + no("SQLiteClosable", "releaseReferenceFromContainer", "()V") + no("SQLiteCursor", "setFillWindowForwardOnly", "(Z)V") + // requery has only the 3-argument constructor, and its window is its own CursorWindow + no("SQLiteCursor", "", "(L$DATABASE;L$CURSOR_DRIVER;Ljava/lang/String;L${SQLITE_PKG}SQLiteQuery;)V") + no("SQLiteCursor", "setWindow", "(L$CURSOR_WINDOW;)V") + // inherited from android's AbstractCursor and AbstractWindowedCursor, which requery's copies lack + // those (found with javap over both jars; SqliteRewriteResolutionTest probes the rest) + for (name in listOf("isBlob", "isFloat", "isLong", "isString", "isFieldUpdated")) { + no("SQLiteCursor", name, "(I)Z") + } + no("SQLiteCursor", "getUpdatedField", "(I)Ljava/lang/Object;") + // requery's own mWindow has its own CursorWindow type + no("SQLiteCursor", "mWindow", "L$CURSOR_WINDOW;") + no("SQLiteCursor", "getNotificationUris", "()Ljava/util/List;") + no("SQLiteCursor", "setNotificationUris", "(Landroid/content/ContentResolver;Ljava/util/List;)V") + // The constants of SQLiteDatabase that requery lacks (JOURNAL_MODE_*, SYNC_MODE_*, NO_LOCALIZED_COLLATORS, + // SQLITE_MAX_LIKE_PATTERN_LENGTH) are not listed: javac copies them into callers, so no field access remains. + no("SQLiteDatabase", "setLockingEnabled", "(Z)V") + no("SQLiteDatabase", "isDbLockedByOtherThreads", "()Z") + no("SQLiteDatabase", "yieldIfContended", "()Z") + no("SQLiteDatabase", "getSyncedTables", "()Ljava/util/Map;") + no("SQLiteDatabase", "createInMemory", "(L$OPEN_PARAMS;)L$DATABASE;") + no("SQLiteDatabase", "setCustomScalarFunction", "(Ljava/lang/String;Ljava/util/function/UnaryOperator;)V") + no("SQLiteDatabase", "setCustomAggregateFunction", "(Ljava/lang/String;Ljava/util/function/BinaryOperator;)V") + no("SQLiteDatabase", "execPerConnectionSQL", "(Ljava/lang/String;[Ljava/lang/Object;)V") + no("SQLiteDatabase", "markTableSyncable", "(Ljava/lang/String;Ljava/lang/String;)V") + no("SQLiteDatabase", "markTableSyncable", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V") + no("SQLiteOpenHelper", "setLookasideConfig", "(II)V") + no("SQLiteOpenHelper", "setOpenParams", "(L$OPEN_PARAMS;)V") + no("SQLiteOpenHelper", "setIdleConnectionTimeout", "(J)V") + no("SQLiteProgram", "getUniqueId", "()I") + no("SQLiteQueryBuilder", "isDistinct", "()Z") + no("SQLiteQueryBuilder", "appendWhereStandalone", "(Ljava/lang/CharSequence;)V") + no("SQLiteQueryBuilder", "getProjectionMap", "()Ljava/util/Map;") + no("SQLiteQueryBuilder", "setProjectionGreylist", "(Ljava/util/Collection;)V") + no("SQLiteQueryBuilder", "getProjectionGreylist", "()Ljava/util/Collection;") + no("SQLiteQueryBuilder", "getCursorFactory", "()L$CURSOR_FACTORY;") + no("SQLiteQueryBuilder", "isStrict", "()Z") + no("SQLiteQueryBuilder", "setStrictColumns", "(Z)V") + no("SQLiteQueryBuilder", "isStrictColumns", "()Z") + no("SQLiteQueryBuilder", "setStrictGrammar", "(Z)V") + no("SQLiteQueryBuilder", "isStrictGrammar", "()Z") + no("SQLiteQueryBuilder", "insert", "(L$DATABASE;Landroid/content/ContentValues;)J") + no("SQLiteQueryBuilder", "update", "(L$DATABASE;Landroid/content/ContentValues;Ljava/lang/String;[Ljava/lang/String;)I") + no("SQLiteQueryBuilder", "delete", "(L$DATABASE;Ljava/lang/String;[Ljava/lang/String;)I") + // requery's versions have no selectionArgs parameter + no( + "SQLiteQueryBuilder", + "buildQuery", + "([Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", + ) + no( + "SQLiteQueryBuilder", + "buildUnionSubQuery", + "(Ljava/lang/String;[Ljava/lang/String;Ljava/util/Set;ILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", + ) + // added to android.jar after the Wave 0 members.tsv (API 35/36); requery has none of them + no("SQLiteDatabase", "beginTransactionReadOnly", "()V") + no("SQLiteDatabase", "beginTransactionWithListenerReadOnly", "(L${SQLITE_PKG}SQLiteTransactionListener;)V") + no("SQLiteDatabase", "createRawStatement", "(Ljava/lang/String;)L${SQLITE_PKG}SQLiteRawStatement;") + no("SQLiteDatabase", "getLastChangedRowCount", "()J") + no("SQLiteDatabase", "getLastInsertRowId", "()J") + no("SQLiteDatabase", "getTotalChangedRowCount", "()J") + // the OpenParams overloads + no("SQLiteDatabase", "openDatabase", "(Ljava/io/File;L$OPEN_PARAMS;)L$DATABASE;") + no("SQLiteOpenHelper", "", "(Landroid/content/Context;Ljava/lang/String;IL$OPEN_PARAMS;)V") + } + + /** + * Method names with an entry in [MISSING_MEMBERS], so other calls need no hierarchy lookup. Constructors + * are left out: a subclass has no constructor of its parent's, so only the renamed classes themselves match. + */ + val MISSING_MEMBER_NAMES: Set = + MISSING_MEMBERS.keys + .map { it.substringAfterLast('.') } + .filter { it != "" } + .toSet() + + /** Names of the fields the missing-member rule can match: entries for every descriptor, or with a field descriptor. */ + val MISSING_FIELD_NAMES: Set = + MISSING_MEMBERS + .filterValues { descs -> descs.any { !it.startsWith("(") } } + .keys + .map { it.substringAfterLast('.') } + .toSet() + + /** A call whose descriptor changes: selectionArgs-style String[] to Object[], and the signal. */ + class DescriptorRule( + val owner: String, + val name: String, + val desc: String, + val objectArrayParams: Set, + ) + + private val S = "Ljava/lang/String;" + private val SA = "[Ljava/lang/String;" + private val SIG = "L$ANDROID_SIGNAL;" + private const val CF = "L$CURSOR_FACTORY;" + private const val CUR = "Landroid/database/Cursor;" + + val DESCRIPTOR_RULES: List = + run { + fun rule( + name: String, + params: String, + ret: String, + vararg objectArrays: Int, + ) = listOf( + DescriptorRule(DATABASE, name, "($params)$ret", objectArrays.toSet()), + DescriptorRule(DATABASE, name, "($params$SIG)$ret", objectArrays.toSet()), + ) + rule("query", "Z$S$SA$S$SA$S$S$S$S", CUR, 4) + + rule("query", "$S$SA$S$SA$S$S$S", CUR, 3) + + rule("query", "$S$SA$S$SA$S$S$S$S", CUR, 3) + + rule("queryWithFactory", "${CF}Z$S$SA$S$SA$S$S$S$S", CUR, 5) + + rule("rawQuery", "$S$SA", CUR, 1) + + rule("rawQueryWithFactory", "$CF$S$SA$S", CUR, 2) + + // the one signal-only entry; its signal-less form is not an Android method + DescriptorRule(DATABASE, "validateSql", "(${S}$SIG)V", emptySet()) + + DescriptorRule(QUERY_BUILDER, "query", "(L$DATABASE;$SA$S$SA$S$S$S$S$SIG)$CUR", emptySet()) + } + + /** Names of the calls whose descriptor changes. */ + val DESCRIPTOR_RULE_NAMES: Set = DESCRIPTOR_RULES.map { it.name }.toSet() + + /** SQLiteCursorDriver.query, matched on any owner: an app class may implement the interface. */ + const val DRIVER_QUERY_NAME = "query" + const val DRIVER_QUERY_DESC = "($CF[Ljava/lang/String;)Landroid/database/Cursor;" + const val DRIVER_QUERY_NEW_DESC = "($CF[Ljava/lang/Object;)Landroid/database/Cursor;" + + const val HELPER_OPEN_DESC = "(Ljava/lang/String;${CF}I)L$DATABASE;" + const val HELPER_OPEN_HANDLER_DESC = "(Ljava/lang/String;${CF}IL$ERROR_HANDLER;)L$DATABASE;" + const val CONTEXT_OPEN_DESC = "(Ljava/lang/String;I$CF)L$DATABASE;" + const val CONTEXT_OPEN_HANDLER_DESC = "(Ljava/lang/String;I${CF}L$ERROR_HANDLER;)L$DATABASE;" + const val CONTEXT_DELETE_DESC = "(Ljava/lang/String;)Z" + const val HELPER_SIGNAL_DESC = "(L$ANDROID_SIGNAL;)L$CORE_SIGNAL;" + + const val DATABASE_UTILS = "android/database/DatabaseUtils" + + /** + * DatabaseUtils methods that take renamed types and that the runtime reimplements, as name plus + * Android descriptor. requery has no DatabaseUtils. The rest of DatabaseUtils' and other platform + * methods that mention renamed types fail the build. + */ + val REDIRECTED_UTILS: Set = + run { + val db = "L$DATABASE;" + val stmt = "L${SQLITE_PKG}SQLiteStatement;" + setOf( + "queryNumEntries($db$S)J", + "queryNumEntries($db$S$S)J", + "queryNumEntries($db$S$S$SA)J", + "longForQuery($db$S$SA)J", + "longForQuery($stmt$SA)J", + "stringForQuery($db$S$SA)$S", + "stringForQuery($stmt$SA)$S", + ) + } + + /** How a renamed class appears in a descriptor. */ + val RENAMED_DESCRIPTOR_TOKENS: List = RENAMES.keys.map { "L$it;" } + + val CONTEXT_FAMILY: Set = + setOf( + CONTEXT, + "android/content/ContextWrapper", + "android/view/ContextThemeWrapper", + "android/app/Activity", + "android/app/Application", + "android/app/Service", + "android/app/IntentService", + ) +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt new file mode 100644 index 0000000000..6d70446ab1 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteVisitorFactory.kt @@ -0,0 +1,34 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.AsmClassVisitorFactory +import com.android.build.api.instrumentation.ClassContext +import com.android.build.api.instrumentation.ClassData +import com.android.build.api.instrumentation.InstrumentationParameters +import org.objectweb.asm.ClassVisitor + +/** Moves classes from android.database.sqlite to requery; see [SqliteRewriter]. */ +abstract class SqliteRewriteVisitorFactory : AsmClassVisitorFactory { + override fun createClassVisitor( + classContext: ClassContext, + nextClassVisitor: ClassVisitor, + ): ClassVisitor = SqliteRewriter.createVisitor(nextClassVisitor, AgpHierarchy(classContext)) + + override fun isInstrumentable(classData: ClassData): Boolean = !RewritePolicy.isExcluded(classData.className.replace('.', '/')) + + private class AgpHierarchy( + private val context: ClassContext, + ) : ClassHierarchy { + override fun superTypes(internalName: String): Collection? { + val data = context.loadClassData(internalName.replace('/', '.')) ?: return null + val result = linkedSetOf() + val pending = ArrayDeque(data.superClasses + data.interfaces) + while (pending.isNotEmpty()) { + val name = pending.removeFirst() + if (result.add(name.replace('.', '/'))) { + context.loadClassData(name)?.let { pending.addAll(it.interfaces) } + } + } + return result + } + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt new file mode 100644 index 0000000000..f356a6e93f --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt @@ -0,0 +1,759 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.FramesComputationMode +import org.gradle.api.logging.Logging +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassVisitor +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Handle +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.commons.ClassRemapper +import org.objectweb.asm.commons.Remapper +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.FrameNode +import org.objectweb.asm.tree.InsnList +import org.objectweb.asm.tree.InvokeDynamicInsnNode +import org.objectweb.asm.tree.JumpInsnNode +import org.objectweb.asm.tree.LdcInsnNode +import org.objectweb.asm.tree.LineNumberNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.TypeInsnNode +import org.objectweb.asm.tree.VarInsnNode +import org.objectweb.asm.tree.analysis.Analyzer +import org.objectweb.asm.tree.analysis.AnalyzerException +import org.objectweb.asm.tree.analysis.BasicInterpreter +import org.objectweb.asm.tree.analysis.BasicValue +import org.objectweb.asm.tree.analysis.Frame +import com.itsaky.androidide.gradle.sqlite.SqliteRewriteRules as Rules + +/** Thrown with every unsupported reference of a class, so the build names them all at once. */ +class SqliteRewriteException( + message: String, +) : RuntimeException(message) + +/** All supertypes (classes and interfaces, transitive) of a class in internal names, or null. */ +fun interface ClassHierarchy { + fun superTypes(internalName: String): Collection? + + companion object { + val NONE = ClassHierarchy { null } + } +} + +/** Which rules run on a class. The rename always runs. */ +enum class RewritePolicy( + val redirect: Boolean, + val fail: Boolean, +) { + FULL(true, true), + + /** androidx's framework classes (still reachable through AndroidSQLiteDriver; see the docs) and androidx.core's database helpers. */ + NO_FAIL(true, false), + + /** The runtime helper: its redirect targets must not be redirected themselves. */ + RENAME_ONLY(false, false), + ; + + companion object { + fun forClass(internalName: String): RewritePolicy = + when { + internalName.startsWith(Rules.HELPER_PACKAGE_PREFIX) -> RENAME_ONLY + internalName.startsWith(Rules.FRAMEWORK_PACKAGE_PREFIX) -> NO_FAIL + internalName.startsWith(Rules.CORE_DATABASE_PACKAGE_PREFIX) -> NO_FAIL + else -> FULL + } + + /** Classes the rewrite never touches: requery's own. */ + fun isExcluded(internalName: String): Boolean = internalName.startsWith(Rules.REQUERY_PACKAGE_PREFIX) + } +} + +object SqliteRewriter { + private val remapper = + object : Remapper() { + override fun map(internalName: String): String = Rules.RENAMES[internalName] ?: internalName + } + + /** + * What the plugin registers with AGP, and what [rewrite] writes. Frames are copied, so the one change + * that invalidates them, a retyped `SQLiteCursorDriver.query` parameter, is handled in [ClassRewrite]. + */ + val FRAMES_MODE = FramesComputationMode.COPY_FRAMES + + private val logger by lazy { Logging.getLogger(SqliteRewriter::class.java) } + + /** For tests: the transformed bytes of one class. */ + fun rewrite( + bytes: ByteArray, + hierarchy: ClassHierarchy = ClassHierarchy.NONE, + warn: (String) -> Unit = {}, + ): ByteArray { + val writer = ClassWriter(ClassWriter.COMPUTE_MAXS) + ClassReader(bytes).accept(createVisitor(writer, hierarchy, warn), 0) + return writer.toByteArray() + } + + /** + * Buffers the class, applies the call rules on the original (Android) names, then renames on + * the way to [next]. Throws from visitEnd, which fails the AGP transform with the message. + */ + fun createVisitor( + next: ClassVisitor, + hierarchy: ClassHierarchy, + warn: (String) -> Unit = { logger.warn(it) }, + ): ClassVisitor = + object : ClassNode(Opcodes.ASM9) { + override fun visitEnd() { + super.visitEnd() + if (RewritePolicy.isExcluded(name)) { + accept(next) + return + } + ClassRewrite(this, RewritePolicy.forClass(name), hierarchy, warn).run() + accept(ClassRemapper(next, remapper)) + } + } + + private class TaintValue( + type: Type?, + val sqliteCursor: Boolean, + ) : BasicValue(type) { + // Frame.merge keeps the old value when the merged one equals it, so the taint must count + override fun equals(other: Any?): Boolean = + other is BasicValue && super.equals(other) && sqliteCursor == ((other as? TaintValue)?.sqliteCursor ?: false) + + override fun hashCode(): Int = super.hashCode() * 31 + if (sqliteCursor) 1 else 0 + } + + private class ClassRewrite( + val cn: ClassNode, + val policy: RewritePolicy, + val hierarchy: ClassHierarchy, + val warn: (String) -> Unit, + ) { + val violations = linkedSetOf() + + fun run() { + if (policy.fail) { + checkDeclarations() + } + for (m in cn.methods) { + if (policy.fail && isContextDatabaseMethod(m.name, m.desc) && + (cn.superName in Rules.CONTEXT_FAMILY || isContext(cn.name)) + ) { + violations += "method '${m.name}${m.desc}' overrides Context.${m.name}; calls to it are redirected to requery's " + + "runtime and would skip the override" + } + if (policy.redirect && m.name == Rules.DRIVER_QUERY_NAME && m.desc == Rules.DRIVER_QUERY_DESC) { + retypeParameters(m, Rules.DRIVER_QUERY_NEW_DESC) + } + rewriteMethod(m) + } + if (violations.isNotEmpty()) { + throw SqliteRewriteException( + "Code on the Go cannot move '${cn.name.replace('/', '.')}' from android.database.sqlite to " + + "requery (cogo.sqliteDebugger=true):\n" + violations.joinToString("\n") { " - $it" }, + ) + } + } + + /** + * Gives [m] the descriptor [newDesc], which differs from its own in parameters that become wider + * (`String[]` to `Object[]`), and keeps the body valid. The body and its copied frames still treat those + * parameters as the old type, so each is cast back on entry. Frames before the first full one are relative + * to the method's implicit first frame, which now has the wide type, so every frame is written in full. + */ + fun retypeParameters( + m: MethodNode, + newDesc: String, + ) { + val oldDesc = m.desc + m.desc = newDesc + if (m.instructions.size() == 0) return + expandFrames(m, oldDesc) + val old = Type.getArgumentTypes(oldDesc) + val new = Type.getArgumentTypes(newDesc) + var slot = if (m.access and Opcodes.ACC_STATIC != 0) 0 else 1 + val prologue = InsnList() + for (i in old.indices) { + if (old[i] != new[i]) { + prologue.add(VarInsnNode(Opcodes.ALOAD, slot)) + prologue.add(TypeInsnNode(Opcodes.CHECKCAST, old[i].internalName)) + prologue.add(VarInsnNode(Opcodes.ASTORE, slot)) + } + slot += old[i].size + } + m.instructions.insert(prologue) + } + + /** Rewrites every compressed frame of [m] as a full one, starting from [originalDesc]'s parameters. */ + fun expandFrames( + m: MethodNode, + originalDesc: String, + ) { + var locals = ArrayList() + if (m.access and Opcodes.ACC_STATIC == 0) locals += cn.name + for (t in Type.getArgumentTypes(originalDesc)) { + locals += + when (t.sort) { + Type.BOOLEAN, Type.CHAR, Type.BYTE, Type.SHORT, Type.INT -> Opcodes.INTEGER + Type.FLOAT -> Opcodes.FLOAT + Type.LONG -> Opcodes.LONG + Type.DOUBLE -> Opcodes.DOUBLE + else -> t.internalName + } + } + for (frame in m.instructions) { + if (frame !is FrameNode) continue + var stack: List = emptyList() + when (frame.type) { + Opcodes.F_NEW, Opcodes.F_FULL -> { + locals = ArrayList(frame.local) + stack = ArrayList(frame.stack) + } + + Opcodes.F_APPEND -> { + locals.addAll(frame.local) + } + + Opcodes.F_CHOP -> { + repeat(frame.local.size) { locals.removeAt(locals.lastIndex) } + } + + Opcodes.F_SAME1 -> { + stack = ArrayList(frame.stack) + } + } + frame.type = Opcodes.F_NEW + frame.local = ArrayList(locals) + frame.stack = ArrayList(stack) + } + } + + fun checkDeclarations() { + for (f in cn.fields) { + if (f.desc.contains(Rules.OPEN_PARAMS)) violations += "field '${f.name}' has type OpenParams, which requery lacks" + } + for (m in cn.methods) { + if (m.desc.contains(Rules.OPEN_PARAMS)) { + violations += "method '${m.name}${m.desc}' uses OpenParams, which requery lacks" + } + } + } + + private val ancestorsCache = HashMap>() + private val superTypesCache = HashMap?>() + + private fun superTypes(owner: String): Collection? = superTypesCache.getOrPut(owner) { hierarchy.superTypes(owner) } + + /** Owners that cannot extend a renamed class: the JDK, Kotlin, and Android outside the renamed set. */ + private fun cannotBeSqlite(owner: String): Boolean = + owner.startsWith("java/") || owner.startsWith("javax/") || owner.startsWith("kotlin/") || owner.startsWith("kotlinx/") || + (owner.startsWith("android/") && owner !in Rules.SQLITE_CLASSES) + + /** The renamed classes that [owner] is or extends, nearest first, so the rule tables see them all. */ + fun renamedAncestors(owner: String): List { + if (owner.startsWith("[")) return emptyList() + return ancestorsCache.getOrPut(owner) { + val direct = + when { + owner in Rules.SQLITE_CLASSES -> listOf(owner) + cannotBeSqlite(owner) -> emptyList() + else -> superTypes(owner)?.filter { it in Rules.SQLITE_CLASSES }.orEmpty() + } + direct.flatMap { listOf(it) + Rules.RENAMED_SUPERS[it].orEmpty() }.distinct() + } + } + + fun isContext(owner: String): Boolean = owner in Rules.CONTEXT_FAMILY || superTypes(owner)?.contains(Rules.CONTEXT) == true + + fun rewriteMethod(m: MethodNode) { + val tainted = if (policy.fail) cursorCastTaint(m) else emptySet() + var line = -1 + var insn: AbstractInsnNode? = m.instructions.first + while (insn != null) { + val following = insn.next + if (insn is LineNumberNode) { + line = insn.line + } + when (insn) { + is MethodInsnNode -> { + methodInsn(m, insn, line) + } + + is FieldInsnNode -> { + fieldInsn(m, insn, line) + } + + is TypeInsnNode -> { + typeInsn(m, insn, line, tainted) + } + + is LdcInsnNode -> { + (insn.cst as? Type)?.let { + if (it.descriptor.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + } + (insn.cst as? String)?.let { sqlConstant(m, line, it) } + } + + is InvokeDynamicInsnNode -> { + invokeDynamic(m, insn, line) + } + } + insn = following + } + } + + /** requery's native SQLite has no ICU: there is no UNICODE collator, and LOCALIZED compares bytes. */ + fun sqlConstant( + m: MethodNode, + line: Int, + text: String, + ) { + if (!policy.fail) return + if (Rules.COLLATE_UNICODE.containsMatchIn(text)) { + violate( + m, + line, + "SQL text uses COLLATE UNICODE, which does not exist in requery's SQLite (no ICU); the statement would fail", + ) + } + if (Rules.COLLATE_LOCALIZED.containsMatchIn(text)) { + val where = if (line >= 0) "line $line" else "no line number" + warn( + "${cn.name.replace('/', '.')}.${m.name} ($where): COLLATE LOCALIZED works in requery's SQLite but sorts by " + + "byte value (no ICU), not by the device locale", + ) + } + } + + fun invokeDynamic( + m: MethodNode, + insn: InvokeDynamicInsnNode, + line: Int, + ) { + if (insn.desc.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + for (arg in insn.bsmArgs) { + when (arg) { + is String -> sqlConstant(m, line, arg) + is Handle -> handle(m, line, arg) + is Type -> if (arg.descriptor.contains(Rules.OPEN_PARAMS)) violate(m, line, "OpenParams, which requery lacks") + } + } + } + + fun violate( + m: MethodNode, + line: Int, + what: String, + ) { + if (!policy.fail) return + val where = if (line >= 0) "line $line" else "no line number" + violations += "${m.name}${m.desc} ($where): $what" + } + + fun fieldInsn( + m: MethodNode, + f: FieldInsnNode, + line: Int, + ) { + fieldProblem(f.owner, f.name, f.desc)?.let { violate(m, line, it) } + } + + fun fieldProblem( + owner: String, + name: String, + desc: String, + ): String? { + if (desc.contains(Rules.OPEN_PARAMS) || owner == Rules.OPEN_PARAMS) { + return "OpenParams, which requery lacks" + } + // only SQLiteCursor.mWindow is listed today; skip the hierarchy lookup for every other field + if (name !in Rules.MISSING_FIELD_NAMES) return null + return renamedAncestors(owner) + .firstOrNull { Rules.MISSING_MEMBERS["$it.$name"]?.let { descs -> "" in descs || desc in descs } == true } + ?.let { "${simple(it)}.$name does not exist in requery" } + } + + fun typeInsn( + m: MethodNode, + t: TypeInsnNode, + line: Int, + tainted: Set, + ) { + if (t.desc.startsWith(Rules.OPEN_PARAMS)) { + violate(m, line, "OpenParams, which requery lacks") + } + if (t.opcode == Opcodes.CHECKCAST && t.desc in Rules.ANDROID_ONLY_CURSOR_TYPES && t in tainted && + !isGuardedByInstanceof(m, t) + ) { + violate( + m, + line, + "cast of an SQLite cursor to ${t.desc.replace('/', '.')}; requery's cursors are not instances of it, " + + "so the cast would throw", + ) + } + } + + /** What the rules do with one call or method handle. */ + sealed class Action { + object None : Action() + + class Fail( + val what: String, + ) : Action() + + /** Replace the call by an INVOKESTATIC of the runtime helper. */ + class Redirect( + val name: String, + val desc: String, + ) : Action() + + /** Same call, new descriptor; [signal] inserts the CancellationSignal conversion. */ + class Retype( + val desc: String, + val signal: Boolean, + ) : Action() + } + + /** The platform methods on Context that are redirected: name, then the descriptors they match. */ + private fun isContextDatabaseMethod( + name: String, + desc: String, + ) = (name == "openOrCreateDatabase" && (desc == Rules.CONTEXT_OPEN_DESC || desc == Rules.CONTEXT_OPEN_HANDLER_DESC)) || + (name == "deleteDatabase" && desc == Rules.CONTEXT_DELETE_DESC) + + /** + * Decides, without changing anything, what happens to `owner.name desc` invoked with [opcode] + * (the opcode a method handle's kind stands for). Redirects and retyping apply only when the + * policy allows; a [Action.Fail] is reported only when it fails. + */ + fun decide( + opcode: Int, + owner: String, + name: String, + desc: String, + ): Action { + if (owner == Rules.OPEN_PARAMS || desc.contains(Rules.OPEN_PARAMS)) { + return Action.Fail("OpenParams, which requery lacks") + } + if (policy.redirect) { + redirectTarget(opcode, owner, name, desc)?.let { return it } + if (name == Rules.DRIVER_QUERY_NAME && desc == Rules.DRIVER_QUERY_DESC) { + return Action.Retype(Rules.DRIVER_QUERY_NEW_DESC, false) + } + } + if (isContextDatabaseMethod(name, desc) && !owner.startsWith("[")) { + // the redirect above did not take it: super.* calls and owners that are not known to be a Context + if (opcode == Opcodes.INVOKESPECIAL && isContext(owner)) { + return Action.Fail( + "super.$name on a Context cannot be redirected to requery; call it on this or on a Context instead", + ) + } + if (superTypes(owner) == null && owner !in Rules.CONTEXT_FAMILY) { + return Action.Fail( + "$owner.$name is a Context database method if $owner is a Context, " + + "but $owner cannot be resolved, so it cannot be redirected to requery", + ) + } + } + + // the lookup is skipped for owners no rule below can match, which is nearly every call + val ancestors = if (needsAncestors(owner, name, desc)) renamedAncestors(owner) else emptyList() + if (ancestors.isEmpty()) { + if (owner.startsWith("android/") && mentionsRenamed(desc)) { + return Action.Fail( + "${simple(owner)}.$name$desc takes or returns an SQLite class that requery replaces; " + + "the platform method would not link", + ) + } + return Action.None + } + for (c in ancestors) { + if (Rules.MISSING_MEMBERS["$c.$name"]?.let { it.isEmpty() || desc in it } == true) { + return Action.Fail("${simple(c)}.$name$desc does not exist in requery") + } + } + if (desc.contains(Rules.CURSOR_WINDOW)) { + return Action.Fail("${simple(ancestors.first())}.$name uses android.database.CursorWindow; requery has its own") + } + if (!policy.redirect) return Action.None + val rule = + Rules.DESCRIPTOR_RULES.firstOrNull { it.owner in ancestors && it.name == name && it.desc == desc } ?: return Action.None + val args = Type.getArgumentTypes(desc).map { it.descriptor }.toMutableList() + rule.objectArrayParams.forEach { args[it] = "[Ljava/lang/Object;" } + val signal = args.last() == "L${Rules.ANDROID_SIGNAL};" + if (signal) { + args[args.lastIndex] = "L${Rules.CORE_SIGNAL};" + } + return Action.Retype("(" + args.joinToString("") + ")" + Type.getReturnType(desc).descriptor, signal) + } + + fun methodInsn( + m: MethodNode, + mi: MethodInsnNode, + line: Int, + ) { + when (val action = decide(mi.opcode, mi.owner, mi.name, mi.desc)) { + Action.None -> {} + + is Action.Fail -> { + violate(m, line, action.what) + } + + is Action.Redirect -> { + mi.setOpcode(Opcodes.INVOKESTATIC) + mi.owner = Rules.HELPER + mi.name = action.name + mi.desc = action.desc + mi.itf = false + } + + is Action.Retype -> { + if (action.signal) { + m.instructions.insertBefore( + mi, + MethodInsnNode(Opcodes.INVOKESTATIC, Rules.HELPER, "toCancellationSignal", Rules.HELPER_SIGNAL_DESC, false), + ) + } + mi.desc = action.desc + } + } + } + + /** + * A method handle among an invokedynamic's bootstrap arguments (`db::rawQuery`) points at a + * member the call rules would change or reject, but the handle itself cannot be patched in + * place the way an instruction is. Such handles fail the build with a hint to write a lambda, + * whose body is an ordinary call. + */ + fun handle( + m: MethodNode, + line: Int, + h: Handle, + ) { + val what = + when (h.tag) { + Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC -> { + fieldProblem(h.owner, h.name, h.desc) + } + + else -> { + val opcode = + when (h.tag) { + Opcodes.H_INVOKESTATIC -> Opcodes.INVOKESTATIC + Opcodes.H_INVOKEINTERFACE -> Opcodes.INVOKEINTERFACE + Opcodes.H_INVOKEVIRTUAL -> Opcodes.INVOKEVIRTUAL + else -> Opcodes.INVOKESPECIAL + } + when (val action = decide(opcode, h.owner, h.name, h.desc)) { + Action.None -> null + is Action.Fail -> action.what + is Action.Redirect -> "the runtime takes this call over" + is Action.Retype -> "its descriptor changes in requery" + } + } + } + if (what != null) { + violate(m, line, "method reference ${simple(h.owner)}::${h.name} cannot be rewritten ($what); use a lambda instead") + } + } + + /** Returns the replacement for a call the runtime helper takes over, or null. */ + fun redirectTarget( + opcode: Int, + owner: String, + name: String, + desc: String, + ): Action.Redirect? = + when { + opcode == Opcodes.INVOKESTATIC && owner == Rules.DATABASE && name == "openDatabase" && + (desc == Rules.HELPER_OPEN_DESC || desc == Rules.HELPER_OPEN_HANDLER_DESC) -> { + Action.Redirect(name, desc) + } + + opcode == Opcodes.INVOKEVIRTUAL && name == "openOrCreateDatabase" && + (desc == Rules.CONTEXT_OPEN_DESC || desc == Rules.CONTEXT_OPEN_HANDLER_DESC) && isContext(owner) -> { + Action.Redirect(name, "(L${Rules.CONTEXT};" + desc.substring(1)) + } + + opcode == Opcodes.INVOKEVIRTUAL && name == "deleteDatabase" && desc == Rules.CONTEXT_DELETE_DESC && isContext(owner) -> { + Action.Redirect(name, "(L${Rules.CONTEXT};Ljava/lang/String;)Z") + } + + opcode == Opcodes.INVOKESTATIC && owner == Rules.DATABASE_UTILS && (name + desc) in Rules.REDIRECTED_UTILS -> { + Action.Redirect(name, desc) + } + + else -> { + null + } + } + + fun needsAncestors( + owner: String, + name: String, + desc: String, + ) = owner in Rules.SQLITE_CLASSES || name in Rules.MISSING_MEMBER_NAMES || name in Rules.DESCRIPTOR_RULE_NAMES || + desc.contains(Rules.CURSOR_WINDOW) + + private fun previousReal(node: AbstractInsnNode): AbstractInsnNode? { + var n = node.previous + while (n != null && n.opcode < 0) n = n.previous + return n + } + + private fun nextReal(node: AbstractInsnNode): AbstractInsnNode? { + var n = node.next + while (n != null && n.opcode < 0) n = n.next + return n + } + + /** True when the instruction before [label] never falls through to it. */ + private fun endsWithExit(label: AbstractInsnNode): Boolean { + val op = previousReal(label)?.opcode ?: return false + return op == Opcodes.GOTO || op == Opcodes.ATHROW || op in Opcodes.IRETURN..Opcodes.RETURN + } + + /** + * The idiom `if (c instanceof X) ... (X) c`: the operand of [cast] is a local read right before it, + * and an INSTANCEOF of the same type on that local sits before it in a branch that contains the + * cast, with no store to the local in between. A guard that jumps over the cast when false (IFEQ) + * covers the instructions up to its target; one that jumps to the cast when true (IFNE) must be + * followed by code that cannot fall through to its target. The cast then cannot throw, so it is + * not reported. + */ + fun isGuardedByInstanceof( + m: MethodNode, + cast: TypeInsnNode, + ): Boolean { + val local = (previousReal(cast) as? VarInsnNode)?.takeIf { it.opcode == Opcodes.ALOAD }?.`var` ?: return false + val castIndex = m.instructions.indexOf(cast) + var node: AbstractInsnNode? = cast.previous + while (node != null) { + if (node is VarInsnNode && node.`var` == local && node.opcode in Opcodes.ISTORE..Opcodes.ASTORE) return false + if (node is TypeInsnNode && node.opcode == Opcodes.INSTANCEOF && node.desc == cast.desc && + (previousReal(node) as? VarInsnNode)?.let { it.opcode == Opcodes.ALOAD && it.`var` == local } == true + ) { + val jump = nextReal(node) as? JumpInsnNode + if (jump != null) { + val target = m.instructions.indexOf(jump.label) + when (jump.opcode) { + Opcodes.IFEQ -> { + if (target > castIndex) return true + } + + Opcodes.IFNE -> { + if (target <= castIndex && target > m.instructions.indexOf(jump) && endsWithExit(jump.label)) return true + } + } + } + } + node = node.previous + } + return false + } + + fun mentionsRenamed(desc: String) = desc.contains("android/database/") && Rules.RENAMED_DESCRIPTOR_TOKENS.any { desc.contains(it) } + + fun simple(name: String) = name.substringAfterLast('/') + + /** + * The CHECKCASTs to AbstractWindowedCursor whose operand came from an SQLite API (a call on + * one of the renamed classes returning a cursor, or a parameter or instance field declared as + * SQLiteCursor), through locals and the stack. Approximate: values that went through other + * calls, array elements or static fields are not followed (a miss). + */ + fun cursorCastTaint(m: MethodNode): Set { + val casts = + m.instructions.filter { + it is TypeInsnNode && it.opcode == Opcodes.CHECKCAST && it.desc in Rules.ANDROID_ONLY_CURSOR_TYPES + } + if (casts.isEmpty()) return emptySet() + val interpreter = + object : BasicInterpreter(Opcodes.ASM9) { + override fun newValue(type: Type?): BasicValue? = + super.newValue(type)?.let { + if (it.type == + null + ) { + it + } else { + TaintValue(it.type, false) + } + } + + override fun newParameterValue( + isInstanceMethod: Boolean, + local: Int, + type: Type?, + ): BasicValue? { + val r = super.newParameterValue(isInstanceMethod, local, type) + return if (r != null && type?.descriptor == "L${Rules.SQLITE_CURSOR};") TaintValue(r.type, true) else r + } + + override fun unaryOperation( + insn: AbstractInsnNode, + value: BasicValue, + ): BasicValue? { + val r = super.unaryOperation(insn, value) + if (insn is FieldInsnNode && r != null && insn.desc == "L${Rules.SQLITE_CURSOR};") { + return TaintValue(r.type, true) + } + if (insn.opcode == Opcodes.CHECKCAST && r != null) { + val castTo = (insn as TypeInsnNode).desc + return TaintValue(r.type, (value as? TaintValue)?.sqliteCursor == true || castTo == Rules.SQLITE_CURSOR) + } + return r + } + + override fun naryOperation( + insn: AbstractInsnNode, + values: MutableList, + ): BasicValue? { + val r = super.naryOperation(insn, values) + if (insn is MethodInsnNode && r != null && r.type != null) { + val ret = Type.getReturnType(insn.desc) + val cursorLike = + ret.sort == Type.OBJECT && + (ret.internalName == "android/database/Cursor" || ret.internalName == Rules.SQLITE_CURSOR) + if (cursorLike && (renamedAncestors(insn.owner).isNotEmpty() || insn.owner == Rules.CURSOR_FACTORY)) { + return TaintValue(r.type, true) + } + } + return r + } + + override fun merge( + value1: BasicValue, + value2: BasicValue, + ): BasicValue { + val tainted = (value1 as? TaintValue)?.sqliteCursor == true || (value2 as? TaintValue)?.sqliteCursor == true + // BasicInterpreter.merge answers UNINITIALIZED for values that differ, taint included + if (tainted && value1.type != null && value1.type == value2.type) return TaintValue(value1.type, true) + return super.merge(value1, value2) + } + } + val analyzer = Analyzer(interpreter) + val frames: Array?> = + try { + analyzer.analyze(cn.name, m) + } catch (e: AnalyzerException) { + return emptySet() + } + val insns = m.instructions + return casts + .filter { cast -> + val frame = frames[insns.indexOf(cast)] + frame != null && (frame.getStack(frame.stackSize - 1) as? TaintValue)?.sqliteCursor == true + }.toSet() + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt new file mode 100644 index 0000000000..11f751032a --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifier.kt @@ -0,0 +1,98 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.commons.ClassRemapper +import org.objectweb.asm.commons.Remapper +import java.io.PrintWriter +import java.io.StringWriter +import java.net.URLClassLoader + +/** + * Rewrites compiled samples the way the plugin does and verifies the result twice: with ASM's + * CheckClassAdapter (types, through a loader that sees requery, android.jar, androidx core and + * androidx sqlite) and by making the JVM link each class, which runs its verifier over the + * StackMapTable. asm-util is on the test runtime classpath through AGP only, hence the reflection. + */ +internal object BytecodeVerifier { + private val renamer = + object : Remapper() { + override fun map(internalName: String): String = SqliteRewriteRules.RENAMES[internalName] ?: internalName + } + + private val jars by lazy { + listOfNotNull( + TestJars.requeryJar, + TestJars.androidJar, + TestJars.androidxCoreJar, + TestJars.androidxSqliteJar, + TestJars.kotlinStdlibJar, + ) + } + + /** Original (Android-named) samples are added on top of the jars, renamed ones to [after]. */ + private class Universe( + val before: ClassIndex, + val after: ClassIndex, + ) + + private fun universe(originals: Map): Universe { + val before = ClassIndex(listOf(TestJars.androidJar, TestJars.androidxCoreJar)) + val after = ClassIndex(jars) + for (bytes in originals.values) { + before.addClass(bytes) + val renamed = ClassWriter(0) + ClassReader(bytes).accept(ClassRemapper(renamed, renamer), 0) + after.addClass(renamed.toByteArray()) + } + return Universe(before, after) + } + + /** Rewrites every class, then verifies all of them. Returns the rewritten bytes by original name. */ + fun rewriteAndVerify(originals: Map): Map { + val u = universe(originals) + val hierarchy = ClassHierarchy { name -> if (u.before.knows(name)) u.before.superTypes(name) else null } + val out = originals.mapValues { SqliteRewriter.rewrite(it.value, hierarchy) } + verify(out.values.associateBy { ClassReader(it).className }) + return out + } + + /** Fails with the verifier's output when any class in [classes] (by its final name) does not verify. */ + fun verify(classes: Map) { + val loader = SampleLoader(classes) + val checkClass = Class.forName("org.objectweb.asm.util.CheckClassAdapter") + val check = + checkClass.getMethod( + "verify", + ClassReader::class.java, + ClassLoader::class.java, + Boolean::class.javaPrimitiveType, + PrintWriter::class.java, + ) + val problems = StringBuilder() + for ((name, bytes) in classes) { + val sw = StringWriter() + check.invoke(null, ClassReader(bytes), loader, false, PrintWriter(sw)) + if (sw.toString().isNotBlank()) problems.append("CheckClassAdapter on $name:\n").append(sw).append('\n') + try { + // reflection links the class, which verifies it + Class.forName(name.replace('/', '.'), false, loader).declaredMethods + } catch (e: LinkageError) { + problems.append("JVM verification of $name failed: $e\n") + } + } + check(problems.isEmpty()) { "Rewritten bytecode does not verify:\n$problems" } + } + + /** A loader over the same classpath as [verify], defining [classes] (by final name). */ + fun loader(classes: Map): ClassLoader = SampleLoader(classes) + + private class SampleLoader( + private val classes: Map, + ) : URLClassLoader(jars.map { it.toURI().toURL() }.toTypedArray(), ClassLoader.getPlatformClassLoader()) { + override fun findClass(name: String): Class<*> { + val bytes = classes[name.replace('.', '/')] ?: return super.findClass(name) + return defineClass(name, bytes, 0, bytes.size) + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt new file mode 100644 index 0000000000..e3c6ba8da2 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/BytecodeVerifierTest.kt @@ -0,0 +1,58 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Label +import org.objectweb.asm.Opcodes + +/** The verifier helper itself must catch a bad type and a stale frame, or its "OK" means nothing. */ +class BytecodeVerifierTest { + private fun clazz(build: (ClassWriter) -> Unit): Map { + val w = ClassWriter(0) + w.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "p/Bad", null, "java/lang/Object", null) + build(w) + w.visitEnd() + return mapOf("p/Bad" to w.toByteArray()) + } + + @Test + fun `rejects an operand of the wrong type`() { + val classes = + clazz { w -> + w.visitMethod(Opcodes.ACC_STATIC, "m", "(Ljava/lang/Object;)I", null, null).apply { + visitCode() + visitVarInsn(Opcodes.ALOAD, 0) + visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false) + visitInsn(Opcodes.IRETURN) + visitMaxs(1, 1) + visitEnd() + } + } + val e = assertThrows(IllegalStateException::class.java) { BytecodeVerifier.verify(classes) } + assertThat(e.message).contains("p/Bad") + } + + @Test + fun `rejects a stack map frame that disagrees with the code`() { + val classes = + clazz { w -> + w.visitMethod(Opcodes.ACC_STATIC, "m", "(Ljava/lang/Object;)I", null, null).apply { + visitCode() + val l = Label() + visitVarInsn(Opcodes.ALOAD, 0) + visitJumpInsn(Opcodes.IFNULL, l) + visitLabel(l) + // the frame claims a String where the parameter is an Object + visitFrame(Opcodes.F_NEW, 1, arrayOf("java/lang/String"), 0, arrayOf()) + visitInsn(Opcodes.ICONST_0) + visitInsn(Opcodes.IRETURN) + visitMaxs(1, 1) + visitEnd() + } + } + val e = assertThrows(IllegalStateException::class.java) { BytecodeVerifier.verify(classes) } + assertThat(e.message).contains("p/Bad") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt new file mode 100644 index 0000000000..0661826724 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteResolutionTest.kt @@ -0,0 +1,349 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.utils.FileProvider +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Opcodes +import org.objectweb.asm.Type +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.FieldInsnNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode + +class SqliteRewriteResolutionTest { + private val helperSource = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + + private class Ref( + val owner: String, + val name: String, + val desc: String, + val field: Boolean, + val isStatic: Boolean, + ) { + override fun toString() = "$owner.$name$desc" + } + + private fun ClassIndex.resolve( + ref: Ref, + caller: String? = null, + protectedOk: Boolean = false, + ) = resolve(ref.owner, ref.name, ref.desc, ref.field, ref.isStatic, caller, protectedOk) + + private fun refsOf(n: ClassNode): List = + n.methods.flatMap { m -> + m.instructions.mapNotNull { insn -> + when (insn) { + is MethodInsnNode -> { + Ref(insn.owner, insn.name, insn.desc, false, insn.opcode == Opcodes.INVOKESTATIC) + } + + is FieldInsnNode -> { + Ref( + insn.owner, + insn.name, + insn.desc, + true, + insn.opcode == Opcodes.GETSTATIC || insn.opcode == Opcodes.PUTSTATIC, + ) + } + + else -> { + null + } + } + } + } + + @Test + fun `the transformed helper only references what requery, android and androidx core define`() { + val classes = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val unresolved = mutableListOf() + var checked = 0 + for ((name, bytes) in classes) { + val n = node(SqliteRewriter.rewrite(bytes)) + assertThat(n.name).isEqualTo(name) + for (ref in refsOf(n)) { + if (ref.owner in classes.keys) continue + checked++ + if (index.resolve(ref, caller = n.name) == null) { + unresolved += ref.toString() + } + assertThat( + ref.owner, + ).doesNotMatch("android/database/sqlite/SQLite(Database|Open|Cursor|Query|Statement|Program|Closable).*") + } + } + assertThat(checked).isGreaterThan(10) + assertThat(unresolved).isEmpty() + } + + @Test + fun `every public or protected member of the 10 renamed android classes, inherited ones included, resolves in requery or fails the build`() { + val android = TestJars.androidJar + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(android, TestJars.androidxCoreJar), + ) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + + val unresolved = mutableListOf() + val failed = mutableListOf() + var total = 0 + var inheritedTotal = 0 + java.util.zip.ZipFile(android).use { zip -> + fun load(name: String) = + zip.getEntry("$name.class")?.let { e -> + ClassNode().also { ClassReader(zip.getInputStream(e)).accept(it, ClassReader.SKIP_CODE) } + } + + /** Android classes the class extends or implements and that stay android's: their members are inherited. */ + fun inheritedFrom(cls: ClassNode): List { + val result = mutableListOf() + val pending = ArrayDeque(listOfNotNull(cls.superName) + cls.interfaces) + val seen = HashSet() + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + if (!seen.add(n) || n in SqliteRewriteRules.RENAMES) continue + val c = load(n) ?: continue + result += c + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return result + } + for (cls in SqliteRewriteRules.SQLITE_CLASSES) { + val owner = load(cls)!! + val isInterface = owner.access and Opcodes.ACC_INTERFACE != 0 + val visible = Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED + // the member through its declaring class and through every renamed subclass; inherited ones through the class + val callOwners = listOf(cls) + SqliteRewriteRules.RENAMED_SUPERS.filterValues { cls in it }.keys + val declarers = listOf(owner to callOwners) + inheritedFrom(owner).map { it to listOf(cls) } + for ((declarer, owners) in declarers) { + val inherited = declarer !== owner + // constants are copied into callers by javac, so no field access to them exists + val fields = declarer.fields.filter { it.access and visible != 0 && it.value == null } + val methods = declarer.methods.filter { it.access and visible != 0 && !(inherited && it.name == "") } + val members = + fields.map { Triple(true, it.name, it.desc) to it.access } + + methods.map { Triple(false, it.name, it.desc) to it.access } + for ((member, memberAccess) in members) { + val (field, name, desc) = member + for (callOwner in owners) { + if (callOwner != cls && name == "") continue + total++ + if (inherited) inheritedTotal++ + val synthetic = synthesize(declarer, callOwner, isInterface, field, name, desc, memberAccess) + val label = "${cls.substringAfterLast('/')}.$name:$desc" + val out = + try { + node(SqliteRewriter.rewrite(synthetic)) + } catch (e: SqliteRewriteException) { + failed += label + continue + } + val calls = refsOf(out).filter { it.owner != "p/Probe" } + // the probe makes exactly one member reference, plus a signal conversion when inserted + val main = calls.first { it.name == name } + if (index.resolve(main, protectedOk = memberAccess and Opcodes.ACC_PROTECTED != 0) == null) { + unresolved += "$label via ${callOwner.substringAfterLast('/')}" + } + } + } + } + } + } + assertThat(inheritedTotal).isGreaterThan(100) + println("members checked: $total, fail rule: ${failed.size}") + assertThat(unresolved).isEmpty() + // everything the rule table lists as missing must be hit, and nothing else may fail + val expected = + SqliteRewriteRules.MISSING_MEMBERS.flatMap { (key, descs) -> + val prefix = key.substringBeforeLast('.').substringAfterLast('/') + "." + key.substringAfterLast('.') + ":" + descs.map { prefix + it } + } + val unexpected = + failed.filterNot { f -> + expected.contains(f) || expected.any { it.endsWith(":") && f.startsWith(it) } || + f.contains("OpenParams") || f.contains("android/database/CursorWindow") + } + assertThat(unexpected).isEmpty() + } + + @Test + fun `every other platform member that mentions a renamed class is redirected to a target that resolves or fails the build`() { + val index = ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar)) + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + val renamedTokens = SqliteRewriteRules.RENAMED_DESCRIPTOR_TOKENS + val unresolved = mutableListOf() + var probed = 0 + var redirected = 0 + java.util.zip.ZipFile(TestJars.androidJar).use { zip -> + for (entry in zip.entries()) { + if (!entry.name.startsWith("android/") || !entry.name.endsWith(".class")) continue + val owner = ClassNode().also { ClassReader(zip.getInputStream(entry)).accept(it, ClassReader.SKIP_CODE) } + if (owner.name in SqliteRewriteRules.RENAMES) continue + val isInterface = owner.access and Opcodes.ACC_INTERFACE != 0 + for (method in owner.methods) { + if (method.access and (Opcodes.ACC_PUBLIC or Opcodes.ACC_PROTECTED) == 0) continue + if (renamedTokens.none { method.desc.contains(it) }) continue + probed++ + val probe = synthesize(owner, owner.name, isInterface, false, method.name, method.desc, method.access) + val out = + try { + node(SqliteRewriter.rewrite(probe)) + } catch (e: SqliteRewriteException) { + continue + } + val call = refsOf(out).first { it.name == method.name } + redirected++ + if (call.owner != "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" || index.resolve(call) == null) { + unresolved += "${owner.name}.${method.name}${method.desc}" + } + } + } + } + println("platform members probed: $probed, redirected: $redirected") + assertThat(probed).isGreaterThan(10) + assertThat(redirected).isGreaterThan(8) + assertThat(unresolved).isEmpty() + } + + @Test + fun `the real androidx framework classes transform, verify, and resolve in requery`() { + val framework = TestJars.androidxSqliteFrameworkJar + val sources = mutableMapOf() + java.util.zip.ZipFile(framework).use { zip -> + for (entry in zip.entries()) { + if (entry.name.startsWith(SqliteRewriteRules.FRAMEWORK_PACKAGE_PREFIX) && entry.name.endsWith(".class")) { + sources[entry.name.removeSuffix(".class")] = zip.getInputStream(entry).readBytes() + } + } + } + // FrameworkSQLiteOpenHelper and its OpenHelper, FrameworkSQLiteDatabase, -Statement, -Program, and the helpers + assertThat(sources.keys).containsAtLeast( + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelper", + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelper\$OpenHelper", + "androidx/sqlite/db/framework/FrameworkSQLiteDatabase", + "androidx/sqlite/db/framework/FrameworkSQLiteStatement", + "androidx/sqlite/db/framework/FrameworkSQLiteProgram", + "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory", + ) + + val before = ClassIndex(listOf(TestJars.androidJar, TestJars.androidxCoreJar, TestJars.androidxSqliteJar, framework)) + val hierarchy = ClassHierarchy { name -> if (before.knows(name)) before.superTypes(name) else null } + val transformed = sources.mapValues { SqliteRewriter.rewrite(it.value, hierarchy) } + assertThat(transformed.keys).isEqualTo(sources.keys) + + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + // the framework jar also holds androidx.sqlite.util.ProcessLock, which is not rewritten + val index = + ClassIndex(listOf(TestJars.requeryJar, TestJars.androidJar, TestJars.androidxCoreJar, TestJars.androidxSqliteJar, framework)) + helper.values.forEach { index.addClass(SqliteRewriter.rewrite(it)) } + transformed.values.forEach { index.addClass(it) } + + val unresolved = mutableListOf() + var requeryRefs = 0 + for ((name, bytes) in transformed) { + for (ref in refsOf(node(bytes))) { + if (ref.owner.startsWith("kotlin/") || ref.owner.startsWith("[")) continue + if (ref.owner.startsWith("io/requery/")) requeryRefs++ + if (index.resolve(ref, caller = name) == null) unresolved += "$name -> $ref" + } + } + assertThat(requeryRefs).isGreaterThan(30) + assertThat(unresolved).isEmpty() + // requery has no execPerConnectionSQL of its own: the reference resolves to the interface's default, which throws + assertThat( + index.resolve( + "io/requery/android/database/sqlite/SQLiteDatabase", + "execPerConnectionSQL", + "(Ljava/lang/String;[Ljava/lang/Object;)V", + false, + ), + ).isEqualTo("androidx/sqlite/db/SupportSQLiteDatabase") + // every android.database.sqlite reference is gone, so nothing in the package links to the platform's SQLite + for (bytes in transformed.values) { + for (ref in refsOf(node(bytes))) { + assertThat( + ref.owner, + ).doesNotMatch("android/database/sqlite/SQLite(Database|Open|Cursor|Query|Statement|Program|Closable).*") + } + } + BytecodeVerifier.verify(transformed) + } + + /** A class `p/Probe` with one method that performs one member reference with a valid stack. */ + private fun synthesize( + owner: ClassNode, + callOwner: String, + isInterface: Boolean, + field: Boolean, + name: String, + desc: String, + access: Int, + ): ByteArray { + val cn = ClassNode() + cn.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "p/Probe", null, "java/lang/Object", null) + val m = MethodNode(Opcodes.ASM9, Opcodes.ACC_PUBLIC or Opcodes.ACC_STATIC, "probe", "()V", null, null) + val isStatic = access and Opcodes.ACC_STATIC != 0 + if (field) { + m.visitFieldInsn(if (isStatic) Opcodes.GETSTATIC else Opcodes.GETFIELD, callOwner, name, desc) + m.visitInsn(if (Type.getType(desc).size == 2) Opcodes.POP2 else Opcodes.POP) + } else { + if (!isStatic) m.visitInsn(Opcodes.ACONST_NULL) + for (arg in Type.getArgumentTypes(desc)) { + m.visitInsn( + when (arg.sort) { + Type.LONG -> Opcodes.LCONST_0 + Type.FLOAT -> Opcodes.FCONST_0 + Type.DOUBLE -> Opcodes.DCONST_0 + Type.OBJECT, Type.ARRAY -> Opcodes.ACONST_NULL + else -> Opcodes.ICONST_0 + }, + ) + } + val opcode = + when { + name == "" -> Opcodes.INVOKESPECIAL + isStatic -> Opcodes.INVOKESTATIC + isInterface -> Opcodes.INVOKEINTERFACE + else -> Opcodes.INVOKEVIRTUAL + } + m.visitMethodInsn(opcode, callOwner, name, desc, isInterface) + when (Type.getReturnType(desc).size) { + 2 -> m.visitInsn(Opcodes.POP2) + 1 -> m.visitInsn(Opcodes.POP) + } + } + m.visitInsn(Opcodes.RETURN) + m.visitMaxs(8, 0) + cn.methods.add(m) + val w = ClassWriter(0) + cn.accept(w) + return w.toByteArray() + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt new file mode 100644 index 0000000000..e8e892d959 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRuntimeTest.kt @@ -0,0 +1,48 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.utils.FileProvider +import org.junit.jupiter.api.Test + +/** The pure functions of the runtime helper, run on the JVM from its bytecode compiled against android.jar. */ +class SqliteRewriteRuntimeTest { + private val runtime: Class<*> by lazy { + val source = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + .readText() + val classes = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", source)), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + BytecodeVerifier.loader(classes).loadClass("org.appdevforall.sqliterewrite.SqliteRewriteRuntime") + } + + private fun translate(flags: Int) = runtime.getMethod("translateOpenFlags", Int::class.javaPrimitiveType).invoke(null, flags) as Int + + private val readOnly = 1 + private val createIfNecessary = 0x10000000 + private val wal = 0x20000000 + + @Test + fun `read-write and create keep requery's values`() { + assertThat(translate(0)).isEqualTo(2) + assertThat(translate(createIfNecessary)).isEqualTo(6) + assertThat(translate(createIfNecessary or wal)).isEqualTo(6 or wal) + } + + @Test + fun `read-only never gets the create bit, which SQLite rejects`() { + assertThat(translate(readOnly)).isEqualTo(1) + assertThat(translate(readOnly or createIfNecessary)).isEqualTo(1) + } + + @Test + fun `other Android bits are dropped`() { + assertThat(translate(16)).isEqualTo(2) + assertThat(translate(8 or createIfNecessary)).isEqualTo(6) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt new file mode 100644 index 0000000000..36bfbf40fd --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt @@ -0,0 +1,1299 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodInsnNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.TypeInsnNode + +private const val R = "io/requery/android/database/sqlite/" +private const val A = "android/database/sqlite/" +private const val HELPER = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime" + +internal fun node(bytes: ByteArray) = ClassNode().also { ClassReader(bytes).accept(it, 0) } + +internal fun ClassNode.method(name: String): MethodNode = methods.first { it.name == name } + +internal fun MethodNode.calls(): List = instructions.filterIsInstance() + +internal fun MethodNode.call(name: String): MethodInsnNode = calls().first { it.name == name } + +internal fun src( + name: String, + body: String, +) = JavaSource(name, body) + +class SqliteRewriterTest { + private val hierarchy = + ClassHierarchy { name -> + when (name) { + "p/MyActivity" -> { + listOf( + "android/app/Activity", + "android/content/ContextWrapper", + "android/content/Context", + "java/lang/Object", + ) + } + + "p/MyBuilder" -> { + listOf(A + "SQLiteQueryBuilder", "java/lang/Object") + } + + else -> { + null + } + } + } + + private fun transform( + vararg sources: JavaSource, + extraClasspath: List = emptyList(), + ): Map = + BytecodeVerifier.rewriteAndVerify(Compiler.compile(sources.toList(), listOf(TestJars.androidJar) + extraClasspath)) + + @Test + fun `renames the shared classes, the error handlers and overrides, and leaves the kept ones`() { + val out = + transform( + src( + "p.Rename", + """ + package p; + import android.content.Context; + import android.database.*; + import android.database.sqlite.*; + public class Rename extends SQLiteOpenHelper { + public Rename(Context c) { super(c, "n", null, 1, new DefaultDatabaseErrorHandler()); } + @Override public void onCreate(SQLiteDatabase db) { db.execSQL("x"); } + @Override public void onUpgrade(SQLiteDatabase db, int a, int b) {} + public void use(SQLiteDatabase db, CursorWindow w, Cursor c, SQLiteDatabase.CursorFactory f, + SQLiteTransactionListener l) throws SQLiteException { + db.beginTransactionWithListener(l); + SQLiteStatement s = db.compileStatement("y"); + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + } + } + """, + ), + )["p/Rename"]!! + val n = node(out) + assertThat(n.superName).isEqualTo(R + "SQLiteOpenHelper") + assertThat(n.methods.first { it.name == "onCreate" }.desc).isEqualTo("(L${R}SQLiteDatabase;)V") + val init = n.method("") + assertThat(init.calls().first { it.name == "" && it.owner == R + "SQLiteOpenHelper" }.desc) + .isEqualTo( + "(Landroid/content/Context;Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;IL${"io/requery/android/database/DatabaseErrorHandler"};)V", + ) + assertThat(init.instructions.filterIsInstance().map { it.desc }) + .contains("io/requery/android/database/DefaultDatabaseErrorHandler") + val use = n.method("use") + assertThat(use.desc).isEqualTo( + "(L${R}SQLiteDatabase;Landroid/database/CursorWindow;Landroid/database/Cursor;L${R}SQLiteDatabase\$CursorFactory;L${A}SQLiteTransactionListener;)V", + ) + assertThat(use.exceptions).containsExactly(A + "SQLiteException") + assertThat(use.call("beginTransactionWithListener").desc).isEqualTo("(L${A}SQLiteTransactionListener;)V") + assertThat(use.call("compileStatement").desc).isEqualTo("(Ljava/lang/String;)L${R}SQLiteStatement;") + assertThat(use.instructions.filterIsInstance().map { it.desc }).contains(R + "SQLiteQueryBuilder") + } + + @Test + fun `rewrites String array to Object array in calls and in the cursor driver override`() { + val out = + transform( + src( + "p.Args", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class Args implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { return null; } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + void calls(SQLiteDatabase db, SQLiteDatabase.CursorFactory f, SQLiteCursorDriver d, Args me, String[] a) { + db.rawQuery("s", a); + db.query("t", a, "w", a, null, null, null); + db.query("t", a, "w", a, null, null, null, "1"); + db.query(true, "t", a, "w", a, null, null, null, "1"); + db.rawQueryWithFactory(f, "s", a, "t"); + db.queryWithFactory(f, true, "t", a, "w", a, null, null, null, "1"); + d.query(f, a); + me.query(f, a); + } + } + """, + ), + )["p/Args"]!! + val n = node(out) + val decl = n.methods.first { it.name == "query" } + assertThat(decl.desc).isEqualTo("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + val calls = n.method("calls").calls() + assertThat(calls.first { it.name == "rawQuery" }.desc) + .isEqualTo("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + assertThat(calls.filter { it.name == "query" && it.owner == R + "SQLiteDatabase" }.map { it.desc }).containsExactly( + "(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + "(Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + "(ZLjava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", + ) + assertThat(calls.first { it.name == "rawQueryWithFactory" }.desc).isEqualTo( + "(L${R}SQLiteDatabase\$CursorFactory;Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;)Landroid/database/Cursor;", + ) + assertThat( + calls + .first { + it.name == "queryWithFactory" + }.desc, + ).contains("Ljava/lang/String;[Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)") + // the interface call, and the call on the implementing class itself + assertThat(calls.filter { it.name == "query" && it.owner != R + "SQLiteDatabase" }.map { it.desc }.toSet()) + .containsExactly("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + } + + @Test + fun `converts the cancellation signal right before the call`() { + val out = + transform( + src( + "p.Signal", + """ + package p; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class Signal { + void go(SQLiteDatabase db, SQLiteQueryBuilder qb, SQLiteDatabase.CursorFactory f, CancellationSignal s, String[] a) { + db.rawQuery("x", a, s); + db.validateSql("x", s); + qb.query(db, a, "w", a, null, null, null, "1", s); + db.rawQueryWithFactory(f, "x", a, "t", s); + } + } + """, + ), + )["p/Signal"]!! + val go = node(out).method("go") + val insns = go.instructions.toArray().toList() + for (name in listOf("rawQuery", "validateSql", "query", "rawQueryWithFactory")) { + val call = go.call(name) + assertThat(call.desc).contains("Landroidx/core/os/CancellationSignal;)") + val before = insns[insns.indexOf(call) - 1] as MethodInsnNode + assertThat(before.owner).isEqualTo(HELPER) + assertThat(before.name).isEqualTo("toCancellationSignal") + assertThat(before.desc).isEqualTo("(Landroid/os/CancellationSignal;)Landroidx/core/os/CancellationSignal;") + } + assertThat(go.call("rawQuery").desc).isEqualTo( + "(Ljava/lang/String;[Ljava/lang/Object;Landroidx/core/os/CancellationSignal;)Landroid/database/Cursor;", + ) + } + + @Test + fun `redirects open flags and the context calls (also on subclasses), and leaves androidx's factory alone`() { + val out = + transform( + src( + "androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory", + "package androidx.sqlite.db.framework; public class FrameworkSQLiteOpenHelperFactory { public FrameworkSQLiteOpenHelperFactory() {} }", + ), + src( + "p.MyActivity", + "package p; public class MyActivity extends android.app.Activity {" + + " void own(android.database.sqlite.SQLiteDatabase.CursorFactory f) {" + + " this.openOrCreateDatabase(\"n\", 0, f); deleteDatabase(\"n\"); } }", + ), + src( + "p.Redirect", + """ + package p; + import android.content.Context; + import android.database.DatabaseErrorHandler; + import android.database.sqlite.*; + public class Redirect { + Object go(Context c, SQLiteDatabase.CursorFactory f, DatabaseErrorHandler h, MyActivity a, Other o) { + SQLiteDatabase.openDatabase("p", f, SQLiteDatabase.OPEN_READONLY); + SQLiteDatabase.openDatabase("p", f, SQLiteDatabase.CREATE_IF_NECESSARY, h); + SQLiteDatabase.openOrCreateDatabase("p", f); + c.openOrCreateDatabase("n", 0, f); + c.openOrCreateDatabase("n", 0, f, h); + c.deleteDatabase("n"); + a.deleteDatabase("n"); + o.deleteDatabase("n"); + return new androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory(); + } + } + """, + ), + src("p.Other", "package p; public class Other { public boolean deleteDatabase(String n) { return true; } }"), + ) + val go = node(out["p/Redirect"]!!).method("go") + + fun redirected(name: String) = go.calls().filter { it.owner == HELPER && it.name == name } + + assertThat(redirected("openDatabase").map { it.desc }).containsExactly( + "(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;I)L${R}SQLiteDatabase;", + "(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;IL${"io/requery/android/database/DatabaseErrorHandler"};)L${R}SQLiteDatabase;", + ) + assertThat(redirected("openOrCreateDatabase").map { it.desc }).containsExactly( + "(Landroid/content/Context;Ljava/lang/String;IL${R}SQLiteDatabase\$CursorFactory;)L${R}SQLiteDatabase;", + "(Landroid/content/Context;Ljava/lang/String;IL${R}SQLiteDatabase\$CursorFactory;Lio/requery/android/database/DatabaseErrorHandler;)L${R}SQLiteDatabase;", + ) + // Context, the Activity subclass; not the unrelated class with the same method + assertThat(redirected("deleteDatabase")).hasSize(2) + assertThat(go.calls().any { it.owner == "p/Other" && it.name == "deleteDatabase" }).isTrue() + // without flags: stays a plain, renamed call + assertThat(go.calls().any { it.owner == R + "SQLiteDatabase" && it.name == "openOrCreateDatabase" }).isTrue() + // requery's factory ignores noBackupDirectory, allowDataLossOnRecovery and the framework's cross-process + // lock, so Room and WorkManager keep androidx's, which runs on requery through the renamed classes + assertThat(go.instructions.filterIsInstance().map { it.desc }) + .containsExactly("androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory") + assertThat( + go.calls().first { it.name == "" }.owner, + ).isEqualTo("androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory") + + val own = node(out["p/MyActivity"]!!).method("own") + assertThat(own.calls().map { it.owner + "." + it.name }).containsExactly("$HELPER.openOrCreateDatabase", "$HELPER.deleteDatabase") + } + + @Test + fun `branchy and looping samples of every rule kind verify, frames included`() { + val out = + transform( + src( + "p.Branchy", + """ + package p; + import android.content.Context; + import android.database.Cursor; + import android.database.DatabaseErrorHandler; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class Branchy implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + // four locals at once make javac write a full frame, which names the parameter's type + int n = 0, a = 1, b = 2, c = 3; + for (int i = 0; i < 3; i++) { + if (f == null) { + n++; + } else if (args == null) { + n--; + } + } + if (n + a + b + c > 1) { + return null; + } + return null; + } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + + Cursor descriptors(SQLiteDatabase db, SQLiteDatabase.CursorFactory f, String[] a, boolean b, CancellationSignal s) { + Cursor c = null; + for (int i = 0; i < 3; i++) { + if (b) { + c = db.rawQuery("x", a); + } else if (s != null) { + c = db.rawQuery("y", a, s); + } else { + c = db.query("t", a, "w", a, null, null, null, "1"); + } + if (c != null && i == 1) { + c = db.rawQueryWithFactory(f, "z", a, "t"); + } + } + return c; + } + + Object redirects(Context ctx, SQLiteDatabase.CursorFactory f, DatabaseErrorHandler h, boolean b, String name) { + SQLiteDatabase db; + if (b) { + db = SQLiteDatabase.openDatabase(name, f, SQLiteDatabase.OPEN_READONLY); + } else { + db = ctx.openOrCreateDatabase(name, 0, f, h); + } + for (int i = 0; i < 2; i++) { + if (ctx.deleteDatabase(name + i)) { + db = SQLiteDatabase.openDatabase(name, f, SQLiteDatabase.CREATE_IF_NECESSARY, h); + } + } + return db; + } + + Object joins(SQLiteDatabase db, boolean b, SQLiteQueryBuilder qb) { + SQLiteClosable c = b ? db : db.compileStatement("x"); + SQLiteProgram p = b ? db.compileStatement("y") : null; + while (c != null && b) { + c.close(); + c = p; + b = false; + } + return qb != null ? c : p; + } + } + """, + ), + ) + assertThat(out["p/Branchy"]).isNotNull() + } + + @Test + fun `the androidx factory class is rewritten like the rest of its package, not excluded`() { + val name = "androidx/sqlite/db/framework/FrameworkSQLiteOpenHelperFactory" + val out = + transform( + src( + "androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory", + """ + package androidx.sqlite.db.framework; + import android.content.Context; + import android.database.sqlite.SQLiteOpenHelper; + public class FrameworkSQLiteOpenHelperFactory { + public SQLiteOpenHelper create(Context c, String n) { return null; } + } + """, + ), + ) + val n = node(out[name]!!) + assertThat(n.name).isEqualTo(name) + assertThat( + n.method("create").desc, + ).isEqualTo("(Landroid/content/Context;Ljava/lang/String;)L${R}SQLiteOpenHelper;") + assertThat(RewritePolicy.isExcluded(name)).isFalse() + } + + @Test + fun `fails with class, method and line for members requery lacks, collecting all of them`() { + val sources = + src( + "p.Bad", + """ + package p; + import android.database.sqlite.*; + public class Bad { + void one(SQLiteDatabase db) { + db.execPerConnectionSQL("x", null); + } + void two(SQLiteDatabase db, SQLiteDatabase.OpenParams params) { + db.setCustomScalarFunction("f", s -> s); + SQLiteDatabase.openDatabase(new java.io.File("f"), params); + new SQLiteDatabase.OpenParams.Builder().build(); + } + void three(MyBuilder b, SQLiteQueryBuilder q) { + b.isDistinct(); + q.setStrictGrammar(true); + } + } + """, + ) + val classes = + Compiler.compile( + listOf( + sources, + src("p.MyBuilder", "package p; public class MyBuilder extends android.database.sqlite.SQLiteQueryBuilder {}"), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Bad"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("p.Bad") + assertThat(message).contains("one(Landroid/database/sqlite/SQLiteDatabase;)V (line 6)") + assertThat(message).contains("SQLiteDatabase.execPerConnectionSQL") + assertThat(message).contains("two(") + assertThat(message).contains("setCustomScalarFunction") + assertThat(message).contains("OpenParams") + assertThat(message).contains("(line 9)") + assertThat(message).contains("(line 10)") + assertThat(message).contains("(line 11)") + // a subclass of SQLiteQueryBuilder is resolved through the hierarchy + assertThat(message).contains("SQLiteQueryBuilder.isDistinct") + assertThat(message).contains("SQLiteQueryBuilder.setStrictGrammar") + } + + @Test + fun `a missing member is found through every renamed ancestor of the owner`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Ancestors", + """ + package p; + import android.database.sqlite.*; + public class Ancestors { + int program(SQLiteStatement stmt, SQLiteQuery query) { return stmt.getUniqueId() + query.getUniqueId(); } + void closable(SQLiteDatabase db) { db.releaseReferenceFromContainer(); } + void subclass(MyBuilder b) { b.isStrict(); b.close(); } + } + """, + ), + src( + "p.MyBuilder", + "package p; public class MyBuilder extends android.database.sqlite.SQLiteQueryBuilder { void close() {} }", + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Ancestors"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("SQLiteProgram.getUniqueId") + assertThat(message).contains("SQLiteClosable.releaseReferenceFromContainer") + assertThat(message).contains("SQLiteQueryBuilder.isStrict") + assertThat(message).doesNotContain("close") + } + + @Test + fun `hierarchy lookups are made once per owner and not for classes that cannot be SQLite subclasses`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Chatty", + """ + package p; + import android.database.sqlite.*; + public class Chatty { + int go(String s, StringBuilder sb, java.util.List l, android.widget.TextView tv, Mine a, Mine b, SQLiteStatement st) { + sb.append(s.length()).append(s.trim()).append(l.size()); + tv.setText(s); tv.setText(s); + a.run(); a.run(); b.run(); + return st.getUniqueId() > 0 ? 1 : 0; + } + } + """, + ), + src("p.Mine", "package p; public class Mine { void run() {} }"), + ), + ) + val asked = mutableListOf() + val counting = + ClassHierarchy { name -> + asked += name + null + } + try { + SqliteRewriter.rewrite(classes["p/Chatty"]!!, counting) + } catch (e: SqliteRewriteException) { + // getUniqueId is reported, which is not what this test is about + } + assertThat(asked.filter { it.startsWith("java/") || it.startsWith("android/") }).isEmpty() + assertThat(asked.groupingBy { it }.eachCount().filterValues { it > 1 }).isEmpty() + // ordinary calls on app classes need no lookup: no rule can match their name or descriptor + assertThat(asked).doesNotContain("p/Mine") + } + + @Test + fun `a call whose name a rule lists, or whose descriptor has a CursorWindow, still looks the owner up`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Named", + """ + package p; + public class Named { + void go(Mine a, Other b, Third c, android.database.CursorWindow w) { + a.getUniqueId(); + a.getUniqueId(); + b.fetch(w); + c.ordinary(); + } + } + """, + ), + src("p.Mine", "package p; public class Mine { int getUniqueId() { return 0; } }"), + src("p.Other", "package p; public class Other { void fetch(android.database.CursorWindow w) {} }"), + src("p.Third", "package p; public class Third { void ordinary() {} }"), + ), + ) + val asked = mutableListOf() + SqliteRewriter.rewrite( + classes["p/Named"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) + assertThat(asked).containsExactly("p/Mine", "p/Other") + } + + @Test + fun `field accesses and constructors of app classes trigger no hierarchy lookup`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Fields", + """ + package p; + public class Fields { + int go(Holder h) { h.count++; return h.count + new Holder().count; } + } + """, + ), + src("p.Holder", "package p; public class Holder { int count; }"), + ), + ) + val asked = mutableListOf() + SqliteRewriter.rewrite( + classes["p/Fields"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) + assertThat(asked).isEmpty() + } + + @Test + fun `DatabaseUtils helpers that take SQLite types are redirected to the runtime`() { + val out = + transform( + src( + "p.Utils", + """ + package p; + import android.database.DatabaseUtils; + import android.database.sqlite.*; + public class Utils { + long go(SQLiteDatabase db, SQLiteStatement st, String[] a, boolean b) { + long n = 0; + if (b) { + n += DatabaseUtils.queryNumEntries(db, "t"); + n += DatabaseUtils.queryNumEntries(db, "t", "x"); + } else { + n += DatabaseUtils.queryNumEntries(db, "t", "x", a); + } + for (int i = 0; i < 2; i++) { + n += DatabaseUtils.longForQuery(db, "q", a) + DatabaseUtils.longForQuery(st, a); + n += DatabaseUtils.stringForQuery(db, "q", a).length() + DatabaseUtils.stringForQuery(st, a).length(); + } + return n; + } + } + """, + ), + )["p/Utils"]!! + val calls = node(out).method("go").calls() + assertThat(calls.filter { it.owner == "android/database/DatabaseUtils" }).isEmpty() + val redirected = calls.filter { it.owner == HELPER } + assertThat(redirected.map { it.name }.groupingBy { it }.eachCount()) + .containsExactly("queryNumEntries", 3, "longForQuery", 2, "stringForQuery", 2) + assertThat(redirected.map { it.opcode }.toSet()).containsExactly(org.objectweb.asm.Opcodes.INVOKESTATIC) + assertThat(redirected.first { it.name == "longForQuery" && it.desc.contains("Statement") }.desc) + .isEqualTo("(L${R}SQLiteStatement;[Ljava/lang/String;)J") + assertThat(redirected.first { it.name == "queryNumEntries" && it.desc.contains("Ljava/lang/String;Ljava/lang/String;)") }.desc) + .isEqualTo("(L${R}SQLiteDatabase;Ljava/lang/String;Ljava/lang/String;)J") + } + + @Test + fun `platform methods with SQLite types that have no requery counterpart fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.BadUtils", + """ + package p; + import android.database.DatabaseUtils; + import android.database.sqlite.*; + public class BadUtils { + void one(SQLiteDatabase db, SQLiteStatement st, String[] a, SQLiteProgram p) throws Exception { + DatabaseUtils.blobFileDescriptorForQuery(db, "q", a); + DatabaseUtils.blobFileDescriptorForQuery(st, a); + DatabaseUtils.bindObjectToProgram(p, 1, "x"); + new DatabaseUtils.InsertHelper(db, "t"); + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/BadUtils"]!!, hierarchy) } + val message = e.message!! + assertThat(message).contains("p.BadUtils") + assertThat(message).contains("DatabaseUtils.blobFileDescriptorForQuery") + assertThat(message).contains("DatabaseUtils.bindObjectToProgram") + assertThat(message).contains("DatabaseUtils\$InsertHelper.") + for (line in 7..10) assertThat(message).contains("(line $line)") + } + + @Test + fun `Context database calls that cannot be redirected fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.SuperCall", + """ + package p; + public class SuperCall extends android.app.Activity { + void go(android.database.sqlite.SQLiteDatabase.CursorFactory f) { + super.openOrCreateDatabase("n", 0, f); + } + boolean del() { + return super.deleteDatabase("n"); + } + } + """, + ), + src( + "p.OverridesBoth", + """ + package p; + import android.database.sqlite.*; + public class OverridesBoth extends android.content.ContextWrapper { + public OverridesBoth() { super(null); } + @Override public SQLiteDatabase openOrCreateDatabase(String n, int m, SQLiteDatabase.CursorFactory f) { return null; } + @Override public boolean deleteDatabase(String n) { return true; } + } + """, + ), + src( + "p.Unknown", + """ + package p; + public class Unknown { + boolean go(q.Lib lib, android.database.sqlite.SQLiteDatabase.CursorFactory f) { + lib.openOrCreateDatabase("n", 0, f); + return lib.deleteDatabase("n"); + } + } + """, + ), + src( + "q.Lib", + """ + package q; + public class Lib { + public android.database.sqlite.SQLiteDatabase openOrCreateDatabase(String n, int m, android.database.sqlite.SQLiteDatabase.CursorFactory f) { return null; } + public boolean deleteDatabase(String n) { return true; } + } + """, + ), + ), + ) + + fun failure(name: String) = + assertThrows(SqliteRewriteException::class.java) { + SqliteRewriter.rewrite(classes[name]!!, hierarchy) + }.message!! + failure("p/SuperCall").let { + assertThat(it).contains("go(") + assertThat(it).contains("del(") + assertThat(it).contains("super") + } + failure("p/OverridesBoth").let { + assertThat(it).contains("overrides Context.openOrCreateDatabase") + assertThat(it).contains("overrides Context.deleteDatabase") + } + failure("p/Unknown").let { + assertThat(it).contains("q/Lib") + assertThat(it).contains("(line 5)") + assertThat(it).contains("(line 6)") + } + } + + @Test + fun `method references get the same rules as calls, or fail asking for a lambda`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Refs", + """ + package p; + import android.content.Context; + import android.database.Cursor; + import android.database.sqlite.*; + public class Refs { + interface Open { SQLiteDatabase open(String n, int m, SQLiteDatabase.CursorFactory f); } + interface Opener { SQLiteDatabase open(String n, SQLiteDatabase.CursorFactory f, int flags); } + Object rawQuery(SQLiteDatabase db) { + java.util.function.BiFunction f = db::rawQuery; + return f; + } + Object delete(Context ctx) { + java.util.function.Predicate p = ctx::deleteDatabase; + return p; + } + Object open(Context ctx) { + Open o = ctx::openOrCreateDatabase; + return o; + } + Object staticOpen() { + Opener o = SQLiteDatabase::openDatabase; + return o; + } + Object missing(SQLiteDatabase db) { + Runnable r = db::releaseReferenceFromContainer; + return r; + } + } + """, + ), + ), + ) + val message = + assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Refs"]!!, hierarchy) }.message!! + for (method in listOf("rawQuery", "delete", "open", "staticOpen", "missing")) { + assertThat(message).contains("$method(") + } + assertThat(message).contains("method reference") + assertThat(message).contains("lambda") + } + + @Test + fun `method references and lambdas that need no rule are left alone`() { + val out = + transform( + src( + "p.FineRefs", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class FineRefs { + Object go(SQLiteDatabase db, java.util.List l) { + Runnable begin = db::beginTransaction; + java.util.function.Function len = String::length; + java.util.function.Supplier s = () -> db.compileStatement("x"); + java.util.function.Function q = a -> db.rawQuery(a, null); + l.forEach(x -> db.execSQL(x)); + return new Object[] {begin, len, s, q}; + } + } + """, + ), + )["p/FineRefs"]!! + val n = node(out) + assertThat( + n.methods + .flatMap { it.calls() } + .filter { it.name == "rawQuery" } + .map { it.desc }, + ).containsExactly("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + } + + @Test + fun `fails for a cast of an SQLite cursor to AbstractWindowedCursor but not for other values`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Casts", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Casts { + Object viaLocal(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return (AbstractWindowedCursor) c; + } + Object viaSqliteCursor(SQLiteCursor c) { + Object o = c; + return (AbstractWindowedCursor) o; + } + } + """, + ), + src( + "p.Fine", + """ + package p; + import android.database.*; + public class Fine { + Object other(Cursor fromContentProvider) { + return (AbstractWindowedCursor) fromContentProvider; + } + boolean guarded(Cursor c) { + return c instanceof AbstractWindowedCursor; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Casts"]!!, hierarchy) } + assertThat(e.message).contains("viaLocal") + assertThat(e.message).contains("viaSqliteCursor") + assertThat(e.message).contains("AbstractWindowedCursor") + SqliteRewriter.rewrite(classes["p/Fine"]!!, hierarchy) + } + + @Test + fun `taint of an SQLite cursor survives a join point`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Joins", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Joins { + Object assignedOnOneBranch(Cursor other, SQLiteDatabase db, boolean b) { + Cursor c = other; + if (b) { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + Object assignedInElse(Cursor other, SQLiteDatabase db, boolean b) { + Cursor c; + if (b) { + c = other; + } else { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + Object assignedInLoop(Cursor other, SQLiteDatabase db, int n) { + Cursor c = other; + for (int i = 0; i < n; i++) { + c = db.rawQuery("x", null); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Joins"]!!, hierarchy) } + assertThat(e.message).contains("assignedOnOneBranch") + assertThat(e.message).contains("assignedInElse") + assertThat(e.message).contains("assignedInLoop") + } + + @Test + fun `casts of an SQLite cursor to AbstractCursor and CrossProcessCursor fail too`() { + val classes = + Compiler.compile( + listOf( + src( + "p.MoreCasts", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class MoreCasts { + Object toAbstract(SQLiteDatabase db) { + return (AbstractCursor) db.rawQuery("x", null); + } + Object toCrossProcess(SQLiteCursor c) { + Object o = c; + return (CrossProcessCursor) o; + } + boolean guarded(SQLiteDatabase db) { + return db.rawQuery("x", null) instanceof CrossProcessCursor; + } + } + """, + ), + src( + "p.FineCasts", + """ + package p; + import android.database.*; + public class FineCasts { + Object other(Cursor fromContentProvider) { + return (AbstractCursor) fromContentProvider; + } + Object cross(Cursor fromContentProvider) { + return (CrossProcessCursor) fromContentProvider; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MoreCasts"]!!, hierarchy) } + assertThat(e.message).contains("toAbstract") + assertThat(e.message).contains("android.database.AbstractCursor") + assertThat(e.message).contains("toCrossProcess") + assertThat(e.message).contains("android.database.CrossProcessCursor") + assertThat(e.message).doesNotContain("guarded") + SqliteRewriter.rewrite(classes["p/FineCasts"]!!, hierarchy) + } + + @Test + fun `a call through CursorWindow of a renamed class fails`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Window", + "package p; public class Window { Object go(android.database.sqlite.SQLiteCursor c) { return c.getWindow(); } }", + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Window"]!!, hierarchy) } + assertThat(e.message).contains("CursorWindow") + } + + @Test + fun `the helper package is renamed but neither redirected nor failed, and framework only skips the fail rule`() { + val classes = + Compiler.compile( + listOf( + src( + "org.appdevforall.sqliterewrite.SqliteRewriteRuntime", + """ + package org.appdevforall.sqliterewrite; + import android.database.sqlite.SQLiteDatabase; + public class SqliteRewriteRuntime { + public static SQLiteDatabase openDatabase(String p, SQLiteDatabase.CursorFactory f, int fl) { + return SQLiteDatabase.openDatabase(p, f, fl); + } + public static void bad(SQLiteDatabase db) { db.execPerConnectionSQL("x", null); } + } + """, + ), + src( + "androidx.sqlite.db.framework.FrameworkSQLiteDatabase", + """ + package androidx.sqlite.db.framework; + import android.database.sqlite.SQLiteDatabase; + public class FrameworkSQLiteDatabase { + void exec(SQLiteDatabase db, String[] a) { + db.execPerConnectionSQL("x", null); + db.rawQuery("x", a); + } + } + """, + ), + ), + ) + val helper = node(SqliteRewriter.rewrite(classes["org/appdevforall/sqliterewrite/SqliteRewriteRuntime"]!!, hierarchy)) + val inner = helper.method("openDatabase").call("openDatabase") + assertThat(inner.owner).isEqualTo(R + "SQLiteDatabase") + assertThat(inner.desc).isEqualTo("(Ljava/lang/String;L${R}SQLiteDatabase\$CursorFactory;I)L${R}SQLiteDatabase;") + + val framework = node(SqliteRewriter.rewrite(classes["androidx/sqlite/db/framework/FrameworkSQLiteDatabase"]!!, hierarchy)) + assertThat(framework.method("exec").call("rawQuery").desc).contains("[Ljava/lang/Object;") + } + + @Test + fun `a type test on OpenParams fails too`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Fields", + "package p; public class Fields { Object go(Object o) { return o instanceof android.database.sqlite.SQLiteDatabase.OpenParams; } }", + ), + ), + ) + assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Fields"]!!, hierarchy) } + } + + @Test + fun `androidx core's SQLiteCursorCompat helper is rewritten but never fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "androidx.core.database.sqlite.SQLiteCursorCompat", + """ + package androidx.core.database.sqlite; + public class SQLiteCursorCompat { + public static void setFillWindowForwardOnly(android.database.sqlite.SQLiteCursor c, boolean b) { + c.setFillWindowForwardOnly(b); + } + } + """, + ), + ), + ) + val out = node(SqliteRewriter.rewrite(classes["androidx/core/database/sqlite/SQLiteCursorCompat"]!!, hierarchy)) + assertThat(out.method("setFillWindowForwardOnly").desc).isEqualTo("(L${R}SQLiteCursor;Z)V") + assertThat(RewritePolicy.forClass("androidx/core/database/sqlite/SQLiteCursorCompat\$Api28Impl")).isEqualTo(RewritePolicy.NO_FAIL) + } + + @Test + fun `a cursor driver that uses its String array parameter verifies and links, with every kind of frame`() { + val out = + transform( + src( + "p.Driver", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public class Driver implements SQLiteCursorDriver { + static int sink(String[] a) { return a.length; } + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + int x = args[0].length(); + if (f == null) { + x += sink(args); + } + int a = 1, b = 2, c = 3, d = 4; + for (int i = 0; i < x; i++) { + if (args.length > i) { + x += args[i].length(); + } else { + x += sink(args); + } + } + return x > a + b + c + d ? null : null; + } + public void cursorDeactivated() {} + public void cursorRequeried(Cursor c) {} + public void cursorClosed() {} + public void setBindArguments(String[] a) {} + } + """, + ), + src( + "p.LoopFirst", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + public abstract class LoopFirst implements SQLiteCursorDriver { + public Cursor query(SQLiteDatabase.CursorFactory f, String[] args) { + for (;;) { + if (args[0].length() > 1) { + return null; + } + Driver.sink(args); + } + } + } + """, + ), + ) + val query = node(out["p/Driver"]!!).method("query") + assertThat(query.desc).isEqualTo("(L${R}SQLiteDatabase\$CursorFactory;[Ljava/lang/Object;)Landroid/database/Cursor;") + val first = query.instructions.filter { it.opcode >= 0 }.take(3) + assertThat(first.map { it.opcode }).containsExactly(25, 192, 58).inOrder() // ALOAD, CHECKCAST, ASTORE + assertThat((first[1] as TypeInsnNode).desc).isEqualTo("[Ljava/lang/String;") + } + + @Test + fun `SQL text with COLLATE UNICODE fails the build, with class, method and line`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Dao", + """ + package p; + public class Dao { + static final String K = "SELECT * FROM t ORDER BY n Collate Unicode"; + String direct() { + return "SELECT * FROM t ORDER BY n COLLATE\n\tUNICODE".trim(); + } + String viaConstant() { return K; } + String fine() { return "SELECT * FROM t ORDER BY n COLLATE NOCASE; collateral unicode"; } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Dao"]!!) } + val message = e.message!! + assertThat(message).contains("p.Dao") + assertThat(message).contains("direct()Ljava/lang/String; (line ") + assertThat(message).contains("viaConstant()Ljava/lang/String; (line ") + assertThat(message).contains("COLLATE UNICODE") + assertThat(message).doesNotContain("fine") + } + + @Test + fun `COLLATE LOCALIZED only warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Sorted", + "package p; public class Sorted { String q() { return \"SELECT * FROM t ORDER BY n collate localized\"; } String n() { return \"x\"; } }", + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Sorted"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Sorted.q") + assertThat(warnings.single()).contains("line ") + assertThat(warnings.single()).contains("byte") + } + + @Test + fun `calls on android's CursorWindow are not reported even when the hierarchy says it extends SQLiteClosable`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Windows", + """ + package p; + import android.database.CursorWindow; + public class Windows { + void go(CursorWindow w) { w.releaseReferenceFromContainer(); w.close(); } + Object parcel(android.os.Parcel p) { return CursorWindow.newFromParcel(p); } + } + """, + ), + ), + ) + val withClosable = + ClassHierarchy { name -> + if (name == "android/database/CursorWindow") listOf(A + "SQLiteClosable", "java/lang/Object") else null + } + SqliteRewriter.rewrite(classes["p/Windows"]!!, withClosable) + } + + @Test + fun `inherited deprecated cursor members that requery lacks fail the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.CursorUser", + """ + package p; + import android.database.sqlite.SQLiteCursor; + public class CursorUser { + boolean types(SQLiteCursor c) { return c.isBlob(0) || c.isFloat(1) || c.isLong(2) || c.isString(3); } + Object uris(SQLiteCursor c) { return c.getNotificationUris(); } + } + """, + ), + src( + "p.MyCursor", + """ + package p; + import android.database.sqlite.*; + public class MyCursor extends SQLiteCursor { + public MyCursor(SQLiteCursorDriver d, String t, SQLiteQuery q) { super(d, t, q); } + boolean updated(int i) { return isFieldUpdated(i) || getUpdatedField(i) != null; } + Object window() { return mWindow; } + boolean fine() { return isClosed() && getCount() > 0; } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/CursorUser"]!!, hierarchy) } + for (name in listOf("isBlob", "isFloat", "isLong", "isString", "getNotificationUris")) { + assertThat(e.message).contains("SQLiteCursor.$name") + } + val viaSubclass = + ClassHierarchy { name -> + if (name == + "p/MyCursor" + ) { + listOf(A + "SQLiteCursor", "android/database/AbstractWindowedCursor", "java/lang/Object") + } else { + null + } + } + val e2 = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MyCursor"]!!, viaSubclass) } + assertThat(e2.message).contains("SQLiteCursor.isFieldUpdated") + assertThat(e2.message).contains("SQLiteCursor.getUpdatedField") + assertThat(e2.message).contains("SQLiteCursor.mWindow") + assertThat(e2.message).doesNotContain("isClosed") + assertThat(e2.message).doesNotContain("getCount") + } + + @Test + fun `a cast guarded by an instanceof of the same type on the same value is not reported`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Guarded", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Guarded { + Object ifBlock(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + AbstractWindowedCursor w = (AbstractWindowedCursor) c; + return w.getWindow(); + } + return null; + } + Object and(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return c instanceof AbstractCursor && ((AbstractCursor) c).getCount() > 0; + } + Object ternary(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + return c instanceof CrossProcessCursor ? (CrossProcessCursor) c : null; + } + Object early(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (!(c instanceof AbstractWindowedCursor)) { + return null; + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + src( + "p.Unguarded", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Unguarded { + Object otherType(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractCursor) { + return (AbstractWindowedCursor) c; + } + return null; + } + Object otherValue(SQLiteDatabase db, Cursor d) { + Cursor c = db.rawQuery("x", null); + if (d instanceof AbstractWindowedCursor) { + return (AbstractWindowedCursor) c; + } + return null; + } + Object reassigned(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + c = db.rawQuery("y", null); + return (AbstractWindowedCursor) c; + } + return null; + } + Object afterTheBlock(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractWindowedCursor) { + c.close(); + } + return (AbstractWindowedCursor) c; + } + Object negatedWithoutExit(SQLiteDatabase db) { + Cursor c = db.rawQuery("x", null); + if (!(c instanceof AbstractWindowedCursor)) { + c.close(); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + ), + ) + SqliteRewriter.rewrite(classes["p/Guarded"]!!, hierarchy) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Unguarded"]!!, hierarchy) } + for (name in listOf("otherType", "otherValue", "reassigned", "afterTheBlock", "negatedWithoutExit")) { + assertThat(e.message).contains(name) + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt new file mode 100644 index 0000000000..fd9e8a91eb --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupport.kt @@ -0,0 +1,278 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.junit.jupiter.api.Assumptions.assumeTrue +import org.objectweb.asm.ClassReader +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import java.io.File +import java.net.URI +import java.nio.file.Files +import java.util.zip.ZipFile +import javax.tools.JavaFileObject +import javax.tools.SimpleJavaFileObject +import javax.tools.ToolProvider + +/** + * Prints one line naming what the environment lacks. Then skips the test locally, where not everyone has + * every jar, but fails it when the environment variable CI is set: a CI run that skips these tests + * looks green while checking nothing. + */ +internal fun requireEnvironment( + present: Boolean, + missing: String, +) { + if (present) return + val onCi = !System.getenv("CI").isNullOrEmpty() + System.err.println("SQLITE REWRITE TESTS: missing $missing (${if (onCi) "failing, CI is set" else "skipping, set CI to fail instead"})") + if (onCi) throw AssertionError("The environment lacks $missing, and CI is set, so the SQLite rewrite tests do not skip") + assumeTrue(false, missing) +} + +/** Jars the tests compile and resolve against. Each lookup skips the test with a message, or fails it on CI. */ +internal object TestJars { + private val home = File(System.getProperty("user.home")) + + val androidJar: File by lazy { + val sdkRoots = + listOfNotNull( + System.getenv("ANDROID_HOME"), + System.getenv("ANDROID_SDK_ROOT"), + generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "local.properties") } + .firstOrNull { it.isFile } + ?.readLines() + ?.firstOrNull { it.startsWith("sdk.dir=") } + ?.substringAfter("="), + File(home, "Android/Sdk").path, + ) + val jar = + sdkRoots + .flatMap { root -> listOf(36, 35, 34, 33).map { File(root, "platforms/android-$it/android.jar") } } + .firstOrNull { it.isFile } + requireEnvironment(jar != null, "No android.jar found: set ANDROID_HOME or sdk.dir in local.properties") + jar!! + } + + private fun gradleCacheAar( + group: String, + artifact: String, + version: String, + file: String, + ): File? = + File(home, ".gradle/caches/modules-2/files-2.1/$group/$artifact/$version") + .walkTopDown() + .firstOrNull { it.name == file } + + private fun classesJarOf( + aar: File, + name: String, + ): File { + val out = Files.createTempDirectory("sqlite-rewrite-test").toFile().also { it.deleteOnExit() } + val jar = File(out, "$name.jar") + ZipFile(aar).use { zip -> + zip.getInputStream(zip.getEntry("classes.jar")).use { input -> jar.outputStream().use { input.copyTo(it) } } + } + return jar + } + + val requeryJar: File by lazy { + val aar = + System.getenv("REQUERY_AAR")?.let(::File)?.takeIf { it.isFile } + ?: gradleCacheAar("com.github.requery", "sqlite-android", "3.49.0", "sqlite-android-3.49.0.aar") + requireEnvironment(aar != null, "requery 3.49.0 AAR not found: set REQUERY_AAR or resolve it into the Gradle cache") + classesJarOf(aar!!, "requery") + } + + val androidxSqliteJar: File by lazy { + val aar = gradleCacheAar("androidx.sqlite", "sqlite-android", "2.6.2", "sqlite.aar") + requireEnvironment(aar != null, "androidx.sqlite 2.6.2 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-sqlite") + } + + val androidxSqliteFrameworkJar: File by lazy { + val aar = gradleCacheAar("androidx.sqlite", "sqlite-framework-android", "2.6.2", "sqlite-framework.aar") + requireEnvironment(aar != null, "androidx.sqlite framework 2.6.2 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-sqlite-framework") + } + + /** The Kotlin classes the framework classes call; null when the Gradle cache has none. */ + val kotlinStdlibJar: File? by lazy { + File(home, ".gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib") + .walkTopDown() + .filter { it.name.startsWith("kotlin-stdlib-") && it.name.endsWith(".jar") && "sources" !in it.name } + .maxByOrNull { it.name } + } + + val androidxCoreJar: File by lazy { + val aar = gradleCacheAar("androidx.core", "core", "1.13.1", "core-1.13.1.aar") + requireEnvironment(aar != null, "androidx.core 1.13.1 AAR not found in the Gradle cache") + classesJarOf(aar!!, "androidx-core") + } +} + +internal class JavaSource( + val className: String, + val code: String, +) : SimpleJavaFileObject(URI.create("string:///${className.replace('.', '/')}.java"), JavaFileObject.Kind.SOURCE) { + override fun getCharContent(ignoreEncodingErrors: Boolean) = code +} + +internal object Compiler { + /** Class bytes by internal name, compiled with debug info (line numbers) against [classpath]. */ + fun compile( + sources: List, + classpath: List = listOf(TestJars.androidJar), + ): Map { + val compiler = ToolProvider.getSystemJavaCompiler() ?: error("No system Java compiler: run the tests on a JDK") + val out = Files.createTempDirectory("sqlite-rewrite-classes").toFile().also { it.deleteOnExit() } + val diagnostics = StringBuilder() + val task = + compiler.getTask( + null, + null, + { diagnostics.appendLine(it.toString()) }, + listOf( + "-g", + "-proc:none", + "-source", + "8", + "-target", + "8", + "-Xlint:-options", + "-d", + out.path, + "-classpath", + classpath.joinToString(File.pathSeparator), + ), + null, + sources, + ) + check(task.call()) { "Sample failed to compile:\n$diagnostics" } + return out + .walkTopDown() + .filter { it.extension == "class" } + .associate { it.relativeTo(out).path.removeSuffix(".class") to it.readBytes() } + } +} + +/** Class members of jars, for resolving a reference the way the JVM does. */ +internal class ClassIndex( + jars: List, +) { + private val classes = HashMap() + + init { + for (jar in jars) { + ZipFile(jar).use { zip -> + for (entry in zip.entries()) { + if (entry.name.endsWith(".class")) { + classes[entry.name.removeSuffix(".class")] = + ClassNode().also { ClassReader(zip.getInputStream(entry)).accept(it, ClassReader.SKIP_CODE) } + } + } + } + } + } + + fun addClass(bytes: ByteArray) { + val n = ClassNode().also { ClassReader(bytes).accept(it, ClassReader.SKIP_CODE) } + classes[n.name] = n + } + + private fun load(name: String): ClassNode? = + classes[name] + ?: ClassLoader.getSystemResourceAsStream("$name.class")?.use { s -> + ClassNode().also { ClassReader(s).accept(it, ClassReader.SKIP_CODE) } + } + + fun knows(name: String) = load(name) != null + + fun superClass(name: String): String? = load(name)?.superName + + fun superTypes(name: String): List { + val result = mutableListOf() + val pending = ArrayDeque(listOf(name)) + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + val c = load(n) ?: continue + if (n != name) result += n + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return result + } + + private fun packageOf(name: String) = name.substringBeforeLast('/', "") + + private fun accessible( + access: Int, + declaring: String, + caller: String?, + protectedOk: Boolean, + ): Boolean = + when { + access and Opcodes.ACC_PUBLIC != 0 -> { + true + } + + access and Opcodes.ACC_PRIVATE != 0 -> { + caller == declaring + } + + access and Opcodes.ACC_PROTECTED != 0 -> { + protectedOk || ( + caller != null && ( + caller == declaring || declaring in + superTypes( + caller, + ) || packageOf(caller) == packageOf(declaring) + ) + ) + } + + else -> { + caller != null && packageOf(caller) == packageOf(declaring) + } + } + + /** + * The class declaring the member, or null when the JVM would not link the reference: the member is + * missing, static where the call is not (or the reverse), or not accessible from [caller], a class + * in [classes] (null: an unrelated class in another package). [protectedOk] accepts protected members + * for a caller that is not a subclass, as a probe that stands in for one. + */ + fun resolve( + owner: String, + name: String, + desc: String, + field: Boolean, + isStatic: Boolean? = null, + caller: String? = null, + protectedOk: Boolean = false, + ): String? { + val ownerNode = load(owner) ?: return null + if (!accessible(ownerNode.access, owner, caller, false)) return null + val pending = ArrayDeque(listOf(owner)) + val seen = HashSet() + while (pending.isNotEmpty()) { + val n = pending.removeFirst() + if (!seen.add(n)) continue + val c = load(n) ?: continue + val access = + if (field) { + c.fields.firstOrNull { it.name == name && it.desc == desc }?.access + } else { + c.methods.firstOrNull { it.name == name && it.desc == desc }?.access + } + if (access != null) { + if (isStatic != null && (access and Opcodes.ACC_STATIC != 0) != isStatic) return null + return if (accessible(access, n, caller, protectedOk)) n else null + } + c.superName?.let(pending::add) + pending.addAll(c.interfaces) + } + return null + } + + fun isInterface(name: String) = load(name)?.let { it.access and Opcodes.ACC_INTERFACE != 0 } ?: false +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index cdc27caaf2..45225bc68f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -291,6 +291,9 @@ composeRuntimeDaemonTests = { module = "androidx.compose.runtime:runtime-android # Only :quickbuild:daemon uses this, and it takes the jar from the on-device Gradle # distribution rather than the APK - so the version is the distribution's, not ours. ow2-asm = { module = "org.ow2.asm:asm", version = "9.9" } +ow2-asmAnalysis = { module = "org.ow2.asm:asm-analysis", version = "9.9" } +ow2-asmCommons = { module = "org.ow2.asm:asm-commons", version = "9.9" } +ow2-asmTree = { module = "org.ow2.asm:asm-tree", version = "9.9" } # Tests tests-junit = { module = "junit:junit", version = "4.13.2" } From 0c28568934e8a9df2cbe23953f754cafeb4dcdfb Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 21:51:37 -0800 Subject: [PATCH 03/12] ADFA-7138 Code on the Go injects the rewrite runtime AAR and checks the classpath The rewrite is useful only when the helper AAR is on the app's classpath and requery is present. Code on the Go bundles the helper in its assets (release AAR, no v7/v8 flavors), extracts it at startup, and passes its path to Gradle as -Pandroidide.sqliteRewriteRuntime.aar, always, like logsender: the plugin reads it only for an opted-in project and then names the missing file instead of a missing property. SqliteRewritePlugin registers the class transform on every variant and adds the helper to every variant's runtime classpath. checkSqliteRewriteClasspath fails the build with a clear message when requery or androidx.sqlite is missing from the app: requery comes from the project's libs/, as in ADFA-7082, and the build says so. withRuntimeConfiguration moves from LogSenderPlugin to a shared internal function so the rewrite and the debugger plugins use the same one. GradlePluginConfig names the opt-in property cogo.sqliteDebugger. Tests: the plugin and the check task run in a real Gradle build with the sample project, in the commit that applies the plugin. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- app/build.gradle.kts | 2 + .../services/builder/GradleBuildService.kt | 4 + .../androidide/managers/ToolsManager.java | 14 +++ .../itsaky/androidide/utils/Environment.java | 5 + .../main/java/org/adfa/constants/constants.kt | 4 + .../plugins/AndroidIDEAssetsPlugin.kt | 18 ++++ .../tooling/api/GradlePluginConfig.java | 10 ++ .../androidide/gradle/LogSenderPlugin.kt | 16 ++-- .../sqlite/CheckSqliteRewriteClasspathTask.kt | 82 ++++++++++++++++ .../gradle/sqlite/SqliteRewritePlugin.kt | 94 +++++++++++++++++++ 10 files changed, 243 insertions(+), 6 deletions(-) create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 55c5fdec0f..0e99bc11cc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -523,6 +523,8 @@ dependencies { // (QuickBuildArtifactStager). evaluationDependsOn(":quickbuild:runtime") evaluationDependsOn(":quickbuild:daemon") +// The assets plugin bundles its release AAR (ADFA-7131). +evaluationDependsOn(":subprojects:sqlite-rewrite-runtime") // ADFA-4931: jars the daemon loads from the on-device Gradle distribution instead of shipping its // own copy, since `gradle-dists/` and `quickbuild/` both sit under . diff --git a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt index 88452a86ad..8064b11c7e 100644 --- a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt +++ b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt @@ -53,6 +53,7 @@ import com.itsaky.androidide.tooling.api.ForwardingToolingApiClient import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_JDWP_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_AAR import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR import com.itsaky.androidide.tooling.api.IToolingApiClient import com.itsaky.androidide.tooling.api.IToolingApiServer import com.itsaky.androidide.tooling.api.messages.BuildId @@ -726,6 +727,9 @@ class GradleBuildService : extraArgs.add("-P${PROPERTY_JDWP_ENABLED}=$enableJdwp") extraArgs.add("-P${PROPERTY_LOG_SENDER_ENABLED}=$enableLogSender") extraArgs.add("-P${PROPERTY_LOG_SENDER_AAR}=${Environment.LOGSENDER_AAR.absolutePath}") + // Always passed, like logsender's: the plugin reads it only for a project that opted in, and + // then names the missing file instead of a missing property. + extraArgs.add("-P${PROPERTY_SQLITE_REWRITE_RUNTIME_AAR}=${Environment.SQLITE_REWRITE_RUNTIME_AAR.absolutePath}") extraArgs.addAll(relocatedBuildOutputArgs(isSync)) diff --git a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java index 3977d3fc94..3c0cd978c0 100755 --- a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java +++ b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java @@ -129,6 +129,7 @@ public static void init(@NonNull BaseApplication app, Runnable onFinish) { ensureToolingJar(app); extractLogSender(app); + extractSqliteRewriteRuntime(); writeNoMediaFile(); extractCogoPlugin(); @@ -261,6 +262,19 @@ private static void extractLogSender(BaseApplication app) { Environment.LOGSENDER_AAR.getAbsolutePath()); } + @WorkerThread + private static void extractSqliteRewriteRuntime() { + if (Environment.SQLITE_REWRITE_RUNTIME_AAR.exists()) { + FileUtils.delete(Environment.SQLITE_REWRITE_RUNTIME_AAR); + } + + Environment.mkdirIfNotExists(Environment.SQLITE_REWRITE_RUNTIME_DIR); + + // Java only, so one AAR for both ABIs + ResourceUtils.copyFileFromAssets(getCommonAsset("sqlite-rewrite-runtime-release.aar"), + Environment.SQLITE_REWRITE_RUNTIME_AAR.getAbsolutePath()); + } + private static void generateKeystore() { try { String keystorePath = Environment.KEYSTORE_RELEASE.getPath(); diff --git a/common/src/main/java/com/itsaky/androidide/utils/Environment.java b/common/src/main/java/com/itsaky/androidide/utils/Environment.java index ce5e1f0db9..07cec9b4cb 100755 --- a/common/src/main/java/com/itsaky/androidide/utils/Environment.java +++ b/common/src/main/java/com/itsaky/androidide/utils/Environment.java @@ -18,6 +18,7 @@ import static org.adfa.constants.ConstantsKt.GRADLE_DISTRIBUTION_VERSION; import static org.adfa.constants.ConstantsKt.LOGSENDER_AAR_NAME; +import static org.adfa.constants.ConstantsKt.SQLITE_REWRITE_RUNTIME_AAR_NAME; import android.annotation.SuppressLint; import android.content.Context; @@ -66,6 +67,8 @@ public final class Environment { public static File OPT_DIR; public static File LOGSENDER_DIR; public static File LOGSENDER_AAR; + public static File SQLITE_REWRITE_RUNTIME_DIR; + public static File SQLITE_REWRITE_RUNTIME_AAR; public static File PROJECTS_DIR; // split assets vars @@ -154,6 +157,8 @@ public static void init(Context context) { OPT_DIR = mkdirIfNotExists(new File(PREFIX, "opt")); LOGSENDER_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "logsender")); LOGSENDER_AAR = mkdirIfNotExists(new File(LOGSENDER_DIR, LOGSENDER_AAR_NAME)); + SQLITE_REWRITE_RUNTIME_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "sqlite-rewrite-runtime")); + SQLITE_REWRITE_RUNTIME_AAR = new File(SQLITE_REWRITE_RUNTIME_DIR, SQLITE_REWRITE_RUNTIME_AAR_NAME); PROJECTS_DIR = mkdirIfNotExists(new File(FileUtil.getExternalStorageDir(), PROJECTS_FOLDER)); // NOTE: change location of android.jar from ANDROIDIDE_HOME to inside android-sdk // and don't create the dir if it doesn't exist diff --git a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt index cf9d02b859..b50688e819 100644 --- a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt +++ b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt @@ -83,6 +83,10 @@ const val DOCUMENTATION_DB = "documentation.db" const val LOGSENDER_AAR_NAME = "logsender.aar" +// The helpers the build-time android.database.sqlite -> requery rewrite redirects to (ADFA-7138), +// injected into every variant of apps built with CoGo that opted in +const val SQLITE_REWRITE_RUNTIME_AAR_NAME = "sqlite-rewrite-runtime.aar" + // Generated Gradle Api Jar const val GRADLE_API_NAME_JAR = "gradle-api-$GRADLE_DISTRIBUTION_VERSION.jar" const val GRADLE_API_NAME_JAR_ZIP = "${GRADLE_API_NAME_JAR}.zip" diff --git a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt index fa69885a18..2ff3f922e0 100644 --- a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt +++ b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt @@ -71,6 +71,9 @@ class AndroidIDEAssetsPlugin : Plugin { // Add logsender.aar registerLogSenderAarCopierTask(variant, variantNameCapitalized) + // Add sqlite-rewrite-runtime-release.aar + registerSqliteRewriteRuntimeAarCopierTask(variant, variantNameCapitalized) + // Add plugin-artifacts.zip registerPluginArtifactsCopierTask(variant, variantNameCapitalized) @@ -216,6 +219,21 @@ class AndroidIDEAssetsPlugin : Plugin { } } + /** Java only, no v7/v8 flavors: every variant bundles the same file. The release build type, as above. */ + private fun Project.registerSqliteRewriteRuntimeAarCopierTask( + variant: Variant, + variantName: String, + ) { + addProjectArtifactToAssets( + variant = variant, + taskName = "copy${variantName}SqliteRewriteRuntimeAar", + projectPath = ":subprojects:sqlite-rewrite-runtime", + projectTask = "assembleRelease", + ) { project -> + project.layout.buildDirectory.file("outputs/aar/sqlite-rewrite-runtime-release.aar") + } + } + private inline fun Project.addProjectArtifactToAssets( variant: Variant, taskName: String, diff --git a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java index 4bd6180f1e..03c72716af 100644 --- a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java +++ b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java @@ -64,6 +64,16 @@ public final class GradlePluginConfig { */ public static final String PROPERTY_LOG_SENDER_AAR = "androidide.logsender.aar"; + /** + * Property, normally in the project's gradle.properties, that opts the project into the SQLite debugger (ADFA-7131). The debugger is added to debuggable variants only, and only when the value is true. + */ + public static final String PROPERTY_SQLITE_DEBUGGER_ENABLED = "cogo.sqliteDebugger"; + + /** + * The path to the AAR with the helpers the SQLite rewrite redirects to (ADFA-7138), passed by Code on the Go like {@link #PROPERTY_SQLITE_DEBUGGER_AAR}. + */ + public static final String PROPERTY_SQLITE_REWRITE_RUNTIME_AAR = "androidide.sqliteRewriteRuntime.aar"; + /** * Property that is set in tests to indicate that the plugin is being applied in a test environment. *

diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt index 4819ca0dd9..99416b3d7d 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt @@ -85,12 +85,16 @@ class LogSenderPlugin : Plugin { } } } +} - private fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { - if (this is ApplicationVariantImpl) { - variantDependencies.runtimeClasspath.action() - } else if (this is AnalyticsEnabledApplicationVariant) { - delegate.withRuntimeConfiguration(action) - } +/** + * Runs [action] on the variant's runtime classpath configuration. Shared with + * [SqliteDebuggerPlugin]; does nothing for a variant type it does not recognize. + */ +internal fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { + if (this is ApplicationVariantImpl) { + variantDependencies.runtimeClasspath.action() + } else if (this is AnalyticsEnabledApplicationVariant) { + delegate.withRuntimeConfiguration(action) } } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt new file mode 100644 index 0000000000..39422df3fb --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/CheckSqliteRewriteClasspathTask.kt @@ -0,0 +1,82 @@ +package com.itsaky.androidide.gradle.sqlite + +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Classpath +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import java.io.File +import java.util.zip.ZipFile + +/** + * Fails the build, before anything is compiled or rewritten, when the variant's runtime classpath + * lacks what the rewritten classes load: requery itself and androidx.sqlite. requery reaches an app + * as a file dependency from `libs/`, which brings none of its own dependencies. + */ +abstract class CheckSqliteRewriteClasspathTask : DefaultTask() { + @get:Input + abstract val variantName: Property + + @get:Classpath + abstract val runtimeClasspath: ConfigurableFileCollection + + /** Written on success, so an unchanged classpath is not scanned again. */ + @get:OutputFile + abstract val stamp: RegularFileProperty + + @TaskAction + fun check() { + val files = runtimeClasspath.files + val missing = REQUIRED.filter { (_, entry) -> files.none { contains(it, entry) } } + if (missing.isNotEmpty()) { + throw GradleException(message(variantName.get(), missing.map { it.first })) + } + stamp.get().asFile.apply { + parentFile.mkdirs() + writeText("ok") + } + } + + private fun contains( + file: File, + entry: String, + ): Boolean = + when { + file.isDirectory -> File(file, entry).isFile + file.isFile -> runCatching { ZipFile(file).use { it.getEntry(entry) != null } }.getOrDefault(false) + else -> false + } + + companion object { + internal const val REQUERY_ENTRY = "io/requery/android/database/sqlite/SQLiteDatabase.class" + internal const val ANDROIDX_SQLITE_ENTRY = "androidx/sqlite/db/SupportSQLiteDatabase.class" + + private val REQUIRED = + listOf( + "requery's SQLiteDatabase ($REQUERY_ENTRY)" to REQUERY_ENTRY, + "androidx.sqlite's SupportSQLiteDatabase ($ANDROIDX_SQLITE_ENTRY)" to ANDROIDX_SQLITE_ENTRY, + ) + + internal fun message( + variant: String, + missing: List, + ) = "cogo.sqliteDebugger=true moves the app to requery, but the runtime classpath of variant '$variant' has no " + + missing.joinToString(" and no ") + + ". requery's AAR comes from the project's libs/ directory as a file dependency, which brings none of its own " + + "dependencies: add requery's AAR to libs/ (implementation(files(\"libs/sqlite-android.aar\"))) and androidx.sqlite " + + "(implementation(\"androidx.sqlite:sqlite:\"), or a library that depends on it, such as Room or WorkManager). " + + "Remove cogo.sqliteDebugger from gradle.properties to build without them." + + /** The tasks of a variant that must not start before the check: compiling and the rewrite itself. */ + internal fun guardedTaskNames(capitalizedVariant: String) = + setOf( + "compile${capitalizedVariant}JavaWithJavac", + "compile${capitalizedVariant}Kotlin", + "transform${capitalizedVariant}ClassesWithAsm", + ) + } +} diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt new file mode 100644 index 0000000000..a3b621b501 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt @@ -0,0 +1,94 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle.sqlite + +import com.android.build.api.instrumentation.InstrumentationScope +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.itsaky.androidide.gradle.APP_PLUGIN +import com.itsaky.androidide.gradle.withRuntimeConfiguration +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.gradle.api.GradleException +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.type.ArtifactTypeDefinition +import org.gradle.api.logging.Logging +import java.io.File + +/** + * Registers the android.database.sqlite -> requery class rewrite on every variant (ADFA-7138), adds the + * helpers the rewritten classes call to every variant's runtime classpath, and checks that requery and + * androidx.sqlite are there too. + * + * Applied only when [PROPERTY_SQLITE_DEBUGGER_ENABLED] is "true": the rewritten classes need requery in + * the app, and it reaches apps only through the template's libs/ (ADFA-7082), so a project that + * did not opt in would crash at class load. The helper AAR is Code on the Go's, passed in like the + * debugger's; the build fails when it is missing, because the project opted in. + */ +class SqliteRewritePlugin : Plugin { + companion object { + private val logger = Logging.getLogger(SqliteRewritePlugin::class.java) + + /** AGP's artifact-type attribute for a dependency's classes as a jar; AGP-internal, so the raw string. */ + internal const val CLASSES_JAR_ARTIFACT_TYPE = "android-classes-jar" + } + + override fun apply(target: Project) { + if (!target.plugins.hasPlugin(APP_PLUGIN)) { + return + } + logger.info("Applying SqliteRewritePlugin to project '${target.path}'") + + val runtimeAar = + target.findProperty(PROPERTY_SQLITE_REWRITE_RUNTIME_AAR)?.let { File(it.toString()) } + ?: throw GradleException( + "'$PROPERTY_SQLITE_DEBUGGER_ENABLED' is true but no property '$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR' is set. " + + "Remove the line from gradle.properties to build without the SQLite rewrite.", + ) + if (!runtimeAar.isFile) { + throw GradleException( + "SQLite rewrite runtime AAR not found at '${runtimeAar.absolutePath}'. " + + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it.", + ) + } + + target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).onVariants { variant -> + variant.instrumentation.transformClassesWith( + SqliteRewriteVisitorFactory::class.java, + InstrumentationScope.ALL, + ) {} + // Frames are copied; the rewriter keeps the one method whose descriptor changes valid itself. + variant.instrumentation.setAsmFramesComputationMode(SqliteRewriter.FRAMES_MODE) + + val capitalized = variant.name.replaceFirstChar { it.uppercase() } + variant.withRuntimeConfiguration { + logger.lifecycle("Adding SQLite rewrite runtime dependency to variant '{}' of project '{}'", variant.name, target.path) + dependencies.add(target.dependencies.create(target.fileTree(runtimeAar))) + + val check = + target.tasks.register( + "check${capitalized}SqliteRewriteClasspath", + CheckSqliteRewriteClasspathTask::class.java, + ) { task -> + task.variantName.set(variant.name) + task.runtimeClasspath.from( + incoming + .artifactView { view -> + view.attributes { + it.attribute( + ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, + CLASSES_JAR_ARTIFACT_TYPE, + ) + } + view.setLenient(true) + }.files, + ) + task.stamp.set(target.layout.buildDirectory.file("intermediates/sqlite_rewrite_classpath/${variant.name}/ok")) + } + target.tasks + .matching { it.name in CheckSqliteRewriteClasspathTask.guardedTaskNames(capitalized) } + .configureEach { it.dependsOn(check) } + } + } + } +} From b65e5936d0fa6ff7d23b1f4c55bdaaa42031f7c6 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:06:02 -0800 Subject: [PATCH 04/12] ADFA-7138 Add the SQLite debugger AAR to debuggable variants when opted in Code on the Go bundles the sqlite-debugger release AAR (one AAR carries both ABIs), extracts it at startup and passes its path to Gradle as -Pandroidide.sqliteDebugger.aar, like logsender. SqliteDebuggerPlugin adds it and kotlin-stdlib to the runtime classpath of debuggable variants only: it reads Variant.debuggable in onVariants. A release build never carries the debugger. Tests: SqliteDebuggerPluginTest, with the commit that applies the plugin. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- app/build.gradle.kts | 1 + .../services/builder/GradleBuildService.kt | 2 + .../androidide/managers/ToolsManager.java | 14 +++ .../itsaky/androidide/utils/Environment.java | 5 + .../main/java/org/adfa/constants/constants.kt | 3 + .../plugins/AndroidIDEAssetsPlugin.kt | 21 ++++ .../tooling/api/GradlePluginConfig.java | 5 + gradle-plugin/build.gradle.kts | 8 ++ .../androidide/gradle/SqliteDebuggerPlugin.kt | 98 +++++++++++++++++++ .../androidide/gradle/SqliteBuildSupport.kt | 68 +++++++++++++ 10 files changed, 225 insertions(+) create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 0e99bc11cc..8164293585 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -524,6 +524,7 @@ dependencies { evaluationDependsOn(":quickbuild:runtime") evaluationDependsOn(":quickbuild:daemon") // The assets plugin bundles its release AAR (ADFA-7131). +evaluationDependsOn(":subprojects:sqlite-debugger") evaluationDependsOn(":subprojects:sqlite-rewrite-runtime") // ADFA-4931: jars the daemon loads from the on-device Gradle distribution instead of shipping its diff --git a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt index 8064b11c7e..640054e6c9 100644 --- a/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt +++ b/app/src/main/java/com/itsaky/androidide/services/builder/GradleBuildService.kt @@ -53,6 +53,7 @@ import com.itsaky.androidide.tooling.api.ForwardingToolingApiClient import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_JDWP_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_AAR import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR import com.itsaky.androidide.tooling.api.IToolingApiClient import com.itsaky.androidide.tooling.api.IToolingApiServer @@ -729,6 +730,7 @@ class GradleBuildService : extraArgs.add("-P${PROPERTY_LOG_SENDER_AAR}=${Environment.LOGSENDER_AAR.absolutePath}") // Always passed, like logsender's: the plugin reads it only for a project that opted in, and // then names the missing file instead of a missing property. + extraArgs.add("-P${PROPERTY_SQLITE_DEBUGGER_AAR}=${Environment.SQLITE_DEBUGGER_AAR.absolutePath}") extraArgs.add("-P${PROPERTY_SQLITE_REWRITE_RUNTIME_AAR}=${Environment.SQLITE_REWRITE_RUNTIME_AAR.absolutePath}") extraArgs.addAll(relocatedBuildOutputArgs(isSync)) diff --git a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java index 3c0cd978c0..b097f8e08c 100755 --- a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java +++ b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java @@ -129,6 +129,7 @@ public static void init(@NonNull BaseApplication app, Runnable onFinish) { ensureToolingJar(app); extractLogSender(app); + extractSqliteDebugger(); extractSqliteRewriteRuntime(); writeNoMediaFile(); @@ -262,6 +263,19 @@ private static void extractLogSender(BaseApplication app) { Environment.LOGSENDER_AAR.getAbsolutePath()); } + @WorkerThread + private static void extractSqliteDebugger() { + if (Environment.SQLITE_DEBUGGER_AAR.exists()) { + FileUtils.delete(Environment.SQLITE_DEBUGGER_AAR); + } + + Environment.mkdirIfNotExists(Environment.SQLITE_DEBUGGER_DIR); + + // One AAR with both ABIs, unlike logsender's per-flavor AARs + ResourceUtils.copyFileFromAssets(getCommonAsset("sqlite-debugger-release.aar"), + Environment.SQLITE_DEBUGGER_AAR.getAbsolutePath()); + } + @WorkerThread private static void extractSqliteRewriteRuntime() { if (Environment.SQLITE_REWRITE_RUNTIME_AAR.exists()) { diff --git a/common/src/main/java/com/itsaky/androidide/utils/Environment.java b/common/src/main/java/com/itsaky/androidide/utils/Environment.java index 07cec9b4cb..44c1585bed 100755 --- a/common/src/main/java/com/itsaky/androidide/utils/Environment.java +++ b/common/src/main/java/com/itsaky/androidide/utils/Environment.java @@ -18,6 +18,7 @@ import static org.adfa.constants.ConstantsKt.GRADLE_DISTRIBUTION_VERSION; import static org.adfa.constants.ConstantsKt.LOGSENDER_AAR_NAME; +import static org.adfa.constants.ConstantsKt.SQLITE_DEBUGGER_AAR_NAME; import static org.adfa.constants.ConstantsKt.SQLITE_REWRITE_RUNTIME_AAR_NAME; import android.annotation.SuppressLint; @@ -67,6 +68,8 @@ public final class Environment { public static File OPT_DIR; public static File LOGSENDER_DIR; public static File LOGSENDER_AAR; + public static File SQLITE_DEBUGGER_DIR; + public static File SQLITE_DEBUGGER_AAR; public static File SQLITE_REWRITE_RUNTIME_DIR; public static File SQLITE_REWRITE_RUNTIME_AAR; public static File PROJECTS_DIR; @@ -157,6 +160,8 @@ public static void init(Context context) { OPT_DIR = mkdirIfNotExists(new File(PREFIX, "opt")); LOGSENDER_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "logsender")); LOGSENDER_AAR = mkdirIfNotExists(new File(LOGSENDER_DIR, LOGSENDER_AAR_NAME)); + SQLITE_DEBUGGER_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "sqlite-debugger")); + SQLITE_DEBUGGER_AAR = new File(SQLITE_DEBUGGER_DIR, SQLITE_DEBUGGER_AAR_NAME); SQLITE_REWRITE_RUNTIME_DIR = mkdirIfNotExists(new File(ANDROIDIDE_HOME, "sqlite-rewrite-runtime")); SQLITE_REWRITE_RUNTIME_AAR = new File(SQLITE_REWRITE_RUNTIME_DIR, SQLITE_REWRITE_RUNTIME_AAR_NAME); PROJECTS_DIR = mkdirIfNotExists(new File(FileUtil.getExternalStorageDir(), PROJECTS_FOLDER)); diff --git a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt index b50688e819..88d6c608a0 100644 --- a/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt +++ b/composite-builds/build-deps-common/constants/src/main/java/org/adfa/constants/constants.kt @@ -83,6 +83,9 @@ const val DOCUMENTATION_DB = "documentation.db" const val LOGSENDER_AAR_NAME = "logsender.aar" +// The SQLite debugger AAR (ADFA-7131), injected into debug builds of apps built with CoGo +const val SQLITE_DEBUGGER_AAR_NAME = "sqlite-debugger.aar" + // The helpers the build-time android.database.sqlite -> requery rewrite redirects to (ADFA-7138), // injected into every variant of apps built with CoGo that opted in const val SQLITE_REWRITE_RUNTIME_AAR_NAME = "sqlite-rewrite-runtime.aar" diff --git a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt index 2ff3f922e0..28dad8c713 100644 --- a/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt +++ b/composite-builds/build-logic/plugins/src/main/java/com/itsaky/androidide/plugins/AndroidIDEAssetsPlugin.kt @@ -71,6 +71,9 @@ class AndroidIDEAssetsPlugin : Plugin { // Add logsender.aar registerLogSenderAarCopierTask(variant, variantNameCapitalized) + // Add sqlite-debugger-release.aar + registerSqliteDebuggerAarCopierTask(variant, variantNameCapitalized) + // Add sqlite-rewrite-runtime-release.aar registerSqliteRewriteRuntimeAarCopierTask(variant, variantNameCapitalized) @@ -219,6 +222,24 @@ class AndroidIDEAssetsPlugin : Plugin { } } + /** + * The debugger module has no v7/v8 flavors: one AAR carries both ABIs, so every variant bundles + * the same file. The release build type, as for logsender. + */ + private fun Project.registerSqliteDebuggerAarCopierTask( + variant: Variant, + variantName: String, + ) { + addProjectArtifactToAssets( + variant = variant, + taskName = "copy${variantName}SqliteDebuggerAar", + projectPath = ":subprojects:sqlite-debugger", + projectTask = "assembleRelease", + ) { project -> + project.layout.buildDirectory.file("outputs/aar/sqlite-debugger-release.aar") + } + } + /** Java only, no v7/v8 flavors: every variant bundles the same file. The release build type, as above. */ private fun Project.registerSqliteRewriteRuntimeAarCopierTask( variant: Variant, diff --git a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java index 03c72716af..705f174240 100644 --- a/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java +++ b/gradle-plugin-config/src/main/java/com/itsaky/androidide/tooling/api/GradlePluginConfig.java @@ -69,6 +69,11 @@ public final class GradlePluginConfig { */ public static final String PROPERTY_SQLITE_DEBUGGER_ENABLED = "cogo.sqliteDebugger"; + /** + * The path to the SQLite debugger AAR file, passed by Code on the Go like {@link #PROPERTY_LOG_SENDER_AAR}. + */ + public static final String PROPERTY_SQLITE_DEBUGGER_AAR = "androidide.sqliteDebugger.aar"; + /** * The path to the AAR with the helpers the SQLite rewrite redirects to (ADFA-7138), passed by Code on the Go like {@link #PROPERTY_SQLITE_DEBUGGER_AAR}. */ diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index d631c8404a..ac6d85a7c2 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -176,6 +176,14 @@ gradlePlugin { tags.set(setOf("androidide", "logsender")) } + create("sqliteDebuggerPlugin") { + id = "${BuildConfig.PACKAGE_NAME}.sqlitedebugger" + implementationClass = "${BuildConfig.PACKAGE_NAME}.gradle.SqliteDebuggerPlugin" + displayName = "AndroidIDE SQLite Debugger Gradle Plugin" + description = + "Gradle plugin for adding the SQLite debugger to debuggable variants of projects that are built with AndroidIDE" + tags.set(setOf("androidide", "sqlite")) + } } } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt new file mode 100644 index 0000000000..bb37d6a7b1 --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt @@ -0,0 +1,98 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.api.variant.ApplicationVariant +import com.android.build.api.variant.Component +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import org.adfa.constants.KOTLIN_VERSION +import org.gradle.api.GradleException +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.logging.Logging +import java.io.File +import java.lang.reflect.Method + +/** + * Adds the SQLite debugger (ADFA-7131) to the debuggable variants of an Android application, as + * [LogSenderPlugin] does for LogSender. [AndroidIDEGradlePlugin] applies it only when the project + * sets [PROPERTY_SQLITE_DEBUGGER_ENABLED] to `true`. + * + * Fails the build when the AAR is not there: the project opted in, and a build that quietly drops + * the debugger leaves the user wondering why no database shows up. + */ +class SqliteDebuggerPlugin : Plugin { + companion object { + private val logger = Logging.getLogger(SqliteDebuggerPlugin::class.java) + + /** + * The debugger is Kotlin, and the AAR is a file dependency, which brings no POM and so no + * kotlin-stdlib. KOTLIN_VERSION is the version CoGo's local Maven repository always holds + * (new projects are generated with it), so this resolves offline. Gradle keeps the highest + * version of a module, so an app on a newer Kotlin is not downgraded. + */ + internal const val KOTLIN_STDLIB_COORDINATES = "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION" + + /** + * `Component.debuggable`, which AGP added after the oldest version this plugin supports, so it is + * reached by reflection: minAgpCheck compiles this file against that oldest version. Null there. + */ + private val componentDebuggable: Method? = runCatching { Component::class.java.getMethod("getDebuggable") }.getOrNull() + + /** Null when this AGP has no `Variant.debuggable`. */ + internal fun readDebuggable(variant: ApplicationVariant): Boolean? = componentDebuggable?.invoke(variant) as Boolean? + } + + override fun apply(target: Project) { + if (!target.plugins.hasPlugin(APP_PLUGIN)) { + return + } + + logger.info("Applying {} to project '${target.path}'", SqliteDebuggerPlugin::class.simpleName) + + if (target.isTestEnv) { + logger.lifecycle("Applying {} to project '{}'", javaClass.simpleName, target.path) + } + + val aar = + target.findProperty(PROPERTY_SQLITE_DEBUGGER_AAR)?.let { File(it.toString()) } + ?: throw GradleException( + "'$PROPERTY_SQLITE_DEBUGGER_ENABLED' is true but no property '$PROPERTY_SQLITE_DEBUGGER_AAR' is set. " + + "Remove the line from gradle.properties to build without the SQLite debugger.", + ) + + if (!aar.isFile) { + throw GradleException( + "SQLite debugger AAR not found at '${aar.absolutePath}'. " + + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it.", + ) + } + + target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).apply { + val addDebugger = { variant: ApplicationVariant -> + variant.withRuntimeConfiguration { + logger.lifecycle( + "Adding SQLite debugger dependency to variant '{}' of project '{}'", + variant.name, + target.path, + ) + + dependencies.add(target.dependencies.create(target.fileTree(aar))) + dependencies.add(target.dependencies.create(KOTLIN_STDLIB_COORDINATES)) + } + } + // onDebuggableVariants reads variantBuilder.debuggable in beforeVariants, which AGP 8.11 rejects + // when a plugin is applied from CoGo's init script (see QuickBuildPlugin). Variant.debuggable in + // onVariants is the sanctioned read; AGPs that predate it still allow the builder's. + if (componentDebuggable != null) { + onVariants { variant -> + if (readDebuggable(variant) == true) { + addDebugger(variant) + } + } + } else { + onDebuggableVariants(addDebugger) + } + } + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt new file mode 100644 index 0000000000..4024a65278 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteBuildSupport.kt @@ -0,0 +1,68 @@ +package com.itsaky.androidide.gradle + +import java.io.File +import java.nio.file.Path +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** Prints what each variant's runtime classpath declares, without resolving anything. */ +internal fun printDepsInitScript(dir: File): File = + File(dir, "print-deps.init.gradle").apply { + writeText( + """ + allprojects { p -> + p.afterEvaluate { + if (p.plugins.hasPlugin('com.android.application')) { + p.tasks.register('printRuntimeDeps') { + doLast { + ['demoDebug', 'fullDebug', 'demoRelease', 'fullRelease'].each { v -> + p.configurations.getByName(v + 'RuntimeClasspath').allDependencies.each { d -> + if (d instanceof org.gradle.api.artifacts.FileCollectionDependency) { + d.files.each { println "RTDEP ${'$'}v file ${'$'}{it.name}" } + } else { + println "RTDEP ${'$'}v ${'$'}{d.group}:${'$'}{d.name}:${'$'}{d.version}" + } + } + } + } + } + } + } + } + """.trimIndent(), + ) + } + +/** A jar or AAR file with the given entries, for stand-ins whose class bytes nobody reads. */ +internal fun standIn( + file: File, + vararg entries: Pair, +): File { + file.parentFile.mkdirs() + ZipOutputStream(file.outputStream()).use { zip -> + for ((name, bytes) in entries) { + zip.putNextEntry(ZipEntry(name)) + zip.write(bytes) + zip.closeEntry() + } + } + return file +} + +/** A copy of the sample project in [into], without build output, with [extraBuildScript] appended to the app's script. */ +internal fun copySampleProject( + into: File, + extraBuildScript: String, +): Path { + val source = openProject().toFile() + val dest = File(into, "project") + source + .walkTopDown() + .onEnter { it.name != "build" && it.name != ".gradle" } + .forEach { file -> + val target = File(dest, file.relativeTo(source).path) + if (file.isDirectory) target.mkdirs() else file.copyTo(target, overwrite = true) + } + File(dest, "app/build.gradle.kts").appendText("\n" + extraBuildScript + "\n") + return dest.toPath() +} From 2a5cb6d9368f7649f78d6067ed98b46d5cbde887 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:06:02 -0800 Subject: [PATCH 05/12] ADFA-7138 Apply the rewrite and the debugger from one opt-in property cogo.sqliteDebugger=true in gradle.properties applies both plugins: the rewrite moves the app to requery in every variant, and the debugger, which needs requery, goes into the debuggable variants only. Without the property neither is applied. Tests: a dry run shows the class transform task for a project with the property and none without; the rewrite plugin and the classpath check task run in real Gradle builds with the sample project; a transform run compiles an app and checks the rewritten classes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/AndroidIDEGradlePlugin.kt | 10 + .../gradle/SqliteRewriteBuildTest.kt | 51 +++++ .../gradle/SqliteRewritePluginTest.kt | 172 +++++++++++++++++ .../gradle/SqliteRewriteTransformTest.kt | 175 ++++++++++++++++++ 4 files changed, 408 insertions(+) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt index 0a880dfeff..598b4d9259 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt @@ -16,10 +16,12 @@ */ package com.itsaky.androidide.gradle +import com.itsaky.androidide.gradle.sqlite.SqliteRewritePlugin import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_JDWP_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_PROFILEABLE_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project @@ -55,6 +57,14 @@ class AndroidIDEGradlePlugin : Plugin { pluginManager.apply(LogSenderPlugin::class.java) } + // One opt-in for both (D13): the rewrite moves the app to requery in every variant, the + // debugger needs requery and goes into the debuggable variants only. + val isSqliteDebuggerEnabled = findProperty(PROPERTY_SQLITE_DEBUGGER_ENABLED) == "true" + if (isSqliteDebuggerEnabled) { + pluginManager.apply(SqliteRewritePlugin::class.java) + pluginManager.apply(SqliteDebuggerPlugin::class.java) + } + val isJdwpEnabled = findProperty(PROPERTY_JDWP_ENABLED) == "true" if (isJdwpEnabled) { pluginManager.apply(JdwpPlugin::class.java) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt new file mode 100644 index 0000000000..e3915e03e1 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteBuildTest.kt @@ -0,0 +1,51 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +/** + * The class rewrite (ADFA-7138) is registered only for projects that opt in: without requery in + * the app the rewritten classes cannot load. AGP schedules an ASM transform task per variant + * exactly when a class visitor is registered, so `--dry-run` tells the two cases apart without + * compiling the sample app. + */ +class SqliteRewriteBuildTest { + @Test + fun `a project with the property gets the class transform and one without does not`( + @TempDir dir: File, + ) { + // The same property also adds the debugger, which needs its AAR; --dry-run never reads it. + val aar = File(dir, "sqlite-debugger-release.aar").apply { writeText("aar") } + val runtimeAar = File(dir, "sqlite-rewrite-runtime-release.aar").apply { writeText("aar") } + val enabled = + buildProject( + task = ":app:assembleDemoDebug", + configureArgs = { + it.add("-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true") + it.add("-P$PROPERTY_SQLITE_DEBUGGER_AAR=${aar.absolutePath}") + it.add("-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}") + it.add("--dry-run") + }, + ) + val disabled = + buildProject( + task = ":app:assembleDemoDebug", + configureArgs = { it.add("--dry-run") }, + ) + + assertThat(enabled.output).contains("transformDemoDebugClassesWithAsm") + // the classpath check runs before the compile and the rewrite of its variant + val lines = enabled.output.lines() + val check = lines.indexOfFirst { it.contains("checkDemoDebugSqliteRewriteClasspath") } + assertThat(check).isAtLeast(0) + assertThat(check).isLessThan(lines.indexOfFirst { it.contains("transformDemoDebugClassesWithAsm") }) + assertThat(check).isLessThan(lines.indexOfFirst { it.contains("compileDemoDebugJavaWithJavac") }) + assertThat(disabled.output).doesNotContain("ClassesWithAsm") + assertThat(disabled.output).doesNotContain("SqliteRewriteClasspath") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt new file mode 100644 index 0000000000..03924efb01 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt @@ -0,0 +1,172 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.gradle.sqlite.CheckSqliteRewriteClasspathTask +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.TaskOutcome +import org.gradle.testkit.runner.UnexpectedBuildResultException +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.nio.file.Path + +/** + * The helpers the rewritten classes call (ADFA-7138) are injected by Code on the Go into every variant of a + * project that opted in, and a per-variant task checks that requery and androidx.sqlite are on the + * runtime classpath before anything is compiled. + */ +class SqliteRewritePluginTest { + private val runtimeAarName = "sqlite-rewrite-runtime-release.aar" + private val allVariants = listOf("demoDebug", "fullDebug", "demoRelease", "fullRelease") + private val requeryEntry = CheckSqliteRewriteClasspathTask.REQUERY_ENTRY + private val androidxSqliteEntry = CheckSqliteRewriteClasspathTask.ANDROIDX_SQLITE_ENTRY + + private fun build( + dir: File, + task: String, + enabled: String? = "true", + runtimeAar: File? = File(dir, runtimeAarName).apply { writeText("aar") }, + project: Path? = null, + extraArgs: List = emptyList(), + ): BuildResult { + val debuggerAar = File(dir, "sqlite-debugger-release.aar").apply { writeText("aar") } + return buildProject(task = task, projectDir = project, configureArgs = { + if (task == ":app:printRuntimeDeps") { + it += "--init-script" + it += printDepsInitScript(dir).absolutePath + } + if (enabled != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=$enabled" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + if (runtimeAar != null) it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + it += extraArgs + }) + } + + private fun BuildResult.declared(variant: String) = output.lines().filter { it.startsWith("RTDEP $variant ") } + + private fun failureOf(block: () -> BuildResult): String { + val failure = runCatching(block).exceptionOrNull() + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + return failure!!.message!! + } + + @Test + fun `the helper AAR is added to every variant when opted in, not only the debuggable ones`( + @TempDir dir: File, + ) { + val result = build(dir, ":app:printRuntimeDeps") + + for (variant in allVariants) { + assertThat(result.declared(variant)).contains("RTDEP $variant file $runtimeAarName") + assertThat(result.output).contains("Adding SQLite rewrite runtime dependency to variant '$variant' of project ':app'") + } + } + + @Test + fun `a project that did not opt in gets no helper AAR and no check`( + @TempDir dir: File, + ) { + for (enabled in listOf(null, "false")) { + val result = build(dir, ":app:printRuntimeDeps", enabled) + + for (variant in allVariants) { + assertThat(result.declared(variant).filter { it.contains(runtimeAarName) }).isEmpty() + } + assertThat(result.output).doesNotContain("Adding SQLite rewrite runtime dependency") + } + } + + @Test + fun `opted in without the helper AAR property fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val message = failureOf { build(dir, ":app:printRuntimeDeps", runtimeAar = null) } + + assertThat(message).contains("no property '$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR' is set") + assertThat(message).contains("Remove the line from gradle.properties") + } + + @Test + fun `opted in with a missing helper AAR file fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val message = failureOf { build(dir, ":app:printRuntimeDeps", runtimeAar = File(dir, "absent.aar")) } + + assertThat(message).contains("SQLite rewrite runtime AAR not found") + assertThat(message).contains("Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties") + } + + private fun jar( + dir: File, + name: String, + vararg entries: String, + ) = standIn(File(dir, "libs/$name"), *entries.map { it to ByteArray(0) }.toTypedArray()) + + private fun project( + dir: File, + script: String, + ) = copySampleProject(dir, script) + + @Test + fun `the check fails without requery on the runtime classpath and says how to add it`( + @TempDir dir: File, + ) { + val project = project(dir, "") + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains("variant 'demoDebug'") + assertThat(message).contains(requeryEntry) + assertThat(message).doesNotContain(androidxSqliteEntry) + assertThat(message).contains("file dependency") + assertThat(message).contains("brings none of its own dependencies") + } + + @Test + fun `the check fails without androidx sqlite, which requery's file dependency does not bring`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val project = + project( + dir, + """ + configurations.all { exclude(group = "androidx.sqlite") } + dependencies { implementation(files("${requery.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains(androidxSqliteEntry) + assertThat(message).doesNotContain("requery's SQLiteDatabase") + } + + @Test + fun `the check passes with stand-in jars that contain the two class entries, and is reused from the configuration cache`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val sqlite = jar(dir, "sqlite.jar", androidxSqliteEntry) + val project = + project( + dir, + """ + configurations.all { exclude(group = "androidx.sqlite") } + dependencies { + implementation(files("${requery.invariantSeparatorsPath}")) + implementation(files("${sqlite.invariantSeparatorsPath}")) + } + """.trimIndent(), + ) + val first = + build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project, extraArgs = listOf("--configuration-cache")) + assertThat(first.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + + val second = + build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project, extraArgs = listOf("--configuration-cache")) + assertThat(second.output).contains("Reusing configuration cache") + assertThat(second.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.UP_TO_DATE) + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt new file mode 100644 index 0000000000..b392f5da73 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt @@ -0,0 +1,175 @@ +@file:Suppress("ktlint:standard:max-line-length") + +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.gradle.sqlite.Compiler +import com.itsaky.androidide.gradle.sqlite.JavaSource +import com.itsaky.androidide.gradle.sqlite.TestJars +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import com.itsaky.androidide.utils.FileProvider +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.objectweb.asm.ClassReader +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodInsnNode +import java.io.ByteArrayOutputStream +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipFile +import java.util.zip.ZipOutputStream + +/** + * A real build, not a dry run: AGP runs the ASM transform of a variant over a class in a local AAR, and the + * transformed class refers to requery. The check of the classpath passes because stand-in jars hold the class + * entries it looks for; nothing loads them. + */ +class SqliteRewriteTransformTest { + private fun classesJar(classes: Map): ByteArray { + val bytes = ByteArrayOutputStream() + ZipOutputStream(bytes).use { zip -> + zip.putNextEntry(ZipEntry("META-INF/stand-in")) + zip.closeEntry() + for ((name, content) in classes) { + zip.putNextEntry(ZipEntry("$name.class")) + zip.write(content) + zip.closeEntry() + } + } + return bytes.toByteArray() + } + + private fun aar( + file: File, + namespace: String, + classes: Map, + ): File = + standIn( + file, + "AndroidManifest.xml" to + "".toByteArray(), + "R.txt" to ByteArray(0), + "classes.jar" to classesJar(classes), + ) + + /** + * AGP instruments the classes of dependencies with artifact transforms, which the dex builder triggers, not + * in the variant's transform task, so the output is in Gradle's transform cache: the jars that hold [entry]. The class of this run + * has a name no earlier run used, so a hit is this run's. + */ + private fun transformedJarsWith(entry: String): List { + val cache = FileProvider.projectRoot().resolve("gradle-plugin/build/tmp/test/work/.gradle-test-kit/caches").toFile() + return cache + .walkTopDown() + .filter { it.extension == "jar" && "/transforms/" in it.path } + .mapNotNull { jar -> + runCatching { ZipFile(jar).use { zip -> zip.getEntry(entry)?.let { zip.getInputStream(it).readBytes() } } }.getOrNull() + }.toList() + } + + @Test + fun `AGP's transform rewrites a class of a local AAR to call requery with Object arrays`( + @TempDir dir: File, + ) { + val daoName = "Dao${System.nanoTime()}" + val library = + Compiler.compile( + listOf( + JavaSource( + "x.lib.$daoName", + "package x.lib; public class $daoName { public Object q(android.database.sqlite.SQLiteDatabase db, String[] a) { return db.rawQuery(\"select 1\", a); } }", + ), + ), + ) + val helperSource = + FileProvider + .projectRoot() + .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") + .toFile() + val helper = + Compiler.compile( + listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf(TestJars.androidJar, TestJars.androidxCoreJar), + ) + val libAar = aar(File(dir, "libs/x.aar"), "x.lib", library) + val runtimeAar = aar(File(dir, "sqlite-rewrite-runtime-release.aar"), "org.appdevforall.sqliterewrite", helper) + // real, empty classes: the dex builder reads them + val requery = + standIn( + File(dir, "libs/requery.jar"), + *Compiler + .compile( + listOf( + JavaSource( + "io.requery.android.database.sqlite.SQLiteDatabase", + "package io.requery.android.database.sqlite; public class SQLiteDatabase {}", + ), + ), + ).map { (name, bytes) -> "$name.class" to bytes } + .toTypedArray(), + ) + // the debugger goes into the debug variants with the rewrite, and AGP reads its manifest + val debuggerAar = aar(File(dir, "sqlite-debugger-release.aar"), "org.appdevforall.sqlitedebugger", emptyMap()) + val project = + copySampleProject( + dir, + """ + // the sample compiles against 33, which AGP's AAR metadata check rejects for its own view binding + android { compileSdk = 36 } + dependencies { + implementation(files("${libAar.invariantSeparatorsPath}")) + implementation(files("${requery.invariantSeparatorsPath}")) + } + """.trimIndent(), + ) + + val result = + buildProject( + task = ":app:dexBuilderDemoDebug", + projectDir = project, + configureArgs = { + it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + }, + ) + + assertThat(result.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:transformDemoDebugClassesWithAsm")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + + // the unmodified copy from extracting the AAR is in the cache too; the rewritten one is the other + val daos = transformedJarsWith("x/lib/$daoName.class").map { bytes -> ClassNode().also { ClassReader(bytes).accept(it, 0) } } + assertThat(daos).isNotEmpty() + val rewritten = + daos.filter { dao -> + dao.methods + .first { it.name == "q" } + .instructions + .filterIsInstance() + .any { it.owner.startsWith("io/requery/") } + } + assertThat(rewritten).isNotEmpty() + val q = rewritten.first().methods.first { it.name == "q" } + val call = q.instructions.filterIsInstance().first { it.name == "rawQuery" } + assertThat(call.owner).isEqualTo("io/requery/android/database/sqlite/SQLiteDatabase") + assertThat(call.desc).isEqualTo("(Ljava/lang/String;[Ljava/lang/Object;)Landroid/database/Cursor;") + assertThat(q.desc).isEqualTo("(Lio/requery/android/database/sqlite/SQLiteDatabase;[Ljava/lang/String;)Ljava/lang/Object;") + + // the injected helper AAR goes through the same transform: its class is rewritten to requery too + val helpers = + transformedJarsWith("org/appdevforall/sqliterewrite/SqliteRewriteRuntime.class") + .map { bytes -> ClassNode().also { ClassReader(bytes).accept(it, 0) } } + assertThat(helpers).isNotEmpty() + assertThat( + helpers.any { helper -> + helper.methods.any { method -> + "io/requery/" in method.desc || + method.instructions.filterIsInstance().any { it.owner.startsWith("io/requery/") } + } + }, + ).isTrue() + } +} From 9dc56f06f686f807b229ec46d837369543d84aca Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:06:02 -0800 Subject: [PATCH 06/12] ADFA-7138 Do not apply or provision Quick Build for a project that opted into the debugger Quick Build ships edited classes as a separate payload, compiled by its own daemon from the project's source, so it never passes through the SQLite rewrite. Mixed with rewritten baseline classes it would hand android.database.sqlite objects to requery ones. - AndroidIDEGradlePlugin does not apply Quick Build to an opted-in project and logs why. - GradleQuickBuildProvisioner refuses before staging, which writes into the project, and the IDE shows a string saying why, instead of ending in "variant setup missing". - ComponentProxiabilityResolver keeps DebuggerStartup in its by-name list as a guard only. Tests: SqliteDebuggerPluginTest checks which variants get the debugger AAR, with the property absent and set, and that Quick Build is not applied; QuickBuildProjectSupport decides the opt-in from gradle.properties as the Gradle plugin does (root file read, missing file is no opt-in); the provisioner refuses before staging with its own message. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 16 +- .../quickbuild/QuickBuildProjectSupport.kt | 44 ++++++ ...dleQuickBuildProvisionerFailureArmsTest.kt | 20 +++ .../QuickBuildProjectSupportTest.kt | 34 +++++ .../gradle/AndroidIDEGradlePlugin.kt | 14 +- .../ComponentProxiabilityResolver.kt | 4 + .../gradle/SqliteDebuggerPluginTest.kt | 142 ++++++++++++++++++ .../ComponentProxiabilityResolverTest.kt | 2 + resources/src/main/res/values/strings.xml | 1 + 9 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt index 7b616cfc23..ae0c963fd9 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -340,11 +340,23 @@ class GradleQuickBuildProvisioner( log.info("A Gradle build is already in progress; not staging for the Quick Build proxy app build") return ProxyAppBuildResult.SlotBusy } - stage(context, paths) - val projectManager = lookupProjectManager() val projectRoot = File(projectManager.projectDirPath) + // Before staging, which writes into the project: the Gradle plugin never applies Quick Build + // to an opted-in project, so building would only end in "variant setup missing". + QuickBuildProjectSupport + .sqliteDebuggerMessage(QuickBuildProjectSupport.isSqliteDebuggerEnabled(projectRoot)) + ?.let { refusal -> + log.info( + "Quick Build is not provisioned: {} is true in {}/gradle.properties", + GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, + projectRoot, + ) + return ProxyAppBuildResult.Failed(context.getString(refusal)) + } + stage(context, paths) + // The project model only exists once CoGo's Gradle sync has populated it, and a tap // during sync is common (the user opens a project and reaches straight for Quick // Build). Queue behind the sync rather than failing: the session is already in diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt index bdef298fd4..89728f2f6c 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt @@ -2,6 +2,10 @@ package com.itsaky.androidide.quickbuild import androidx.annotation.StringRes import com.itsaky.androidide.resources.R +import com.itsaky.androidide.tooling.api.GradlePluginConfig +import java.io.File +import java.io.StringReader +import java.util.Properties /** * The reasons Quick Build refuses a project up front, as string resources. @@ -61,4 +65,44 @@ object QuickBuildProjectSupport { } else { null } + + /** + * A project with `cogo.sqliteDebugger=true` is moved to requery by the Gradle plugin, which + * never applies Quick Build to it (its payload would compile the source without the move). The + * IDE must not provision it either: the build would succeed and then end in a "variant setup + * missing" message that blames the variant. + * + * @param sqliteDebuggerEnabled see [isSqliteDebuggerEnabled]. + * @return the refusal message, or null when Quick Build may run. + */ + @StringRes + fun sqliteDebuggerMessage(sqliteDebuggerEnabled: Boolean): Int? = + if (sqliteDebuggerEnabled) { + R.string.quick_build_disabled_sqlite_debugger + } else { + null + } + + /** + * Whether the root `gradle.properties` text opts in, decided as the Gradle plugin does: the + * value is exactly `true`. + * + * @param gradleProperties the file's text, or null when the project has none. + */ + fun isSqliteDebuggerEnabled(gradleProperties: String?): Boolean { + if (gradleProperties == null) return false + val properties = Properties() + return try { + properties.load(StringReader(gradleProperties)) + properties.getProperty(GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED) == "true" + } catch (e: IllegalArgumentException) { + false + } + } + + /** [isSqliteDebuggerEnabled] for the `gradle.properties` in [projectRoot]; false when it cannot be read. */ + fun isSqliteDebuggerEnabled(projectRoot: File): Boolean = + isSqliteDebuggerEnabled( + runCatching { File(projectRoot, "gradle.properties").takeIf { it.isFile }?.readText() }.getOrNull(), + ) } diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt index 168ca584de..dbb526cced 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -69,6 +69,26 @@ class GradleQuickBuildProvisionerFailureArmsTest { assertThat(gradle.executedTasks).isNull() } + @Test + fun `a project that opted into the SQLite debugger is refused before staging, with its own message`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + var staged = 0 + val gradle = FakeBuildService() + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + stage = { _, _ -> staged++ }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(staged).isEqualTo(0) + assertThat(gradle.executedTasks).isNull() + } + @Test fun `a slot that goes busy between the staging and the build still reads as a busy slot`() = runTest { diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt index 0c3af40b4b..ae57986533 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt @@ -76,4 +76,38 @@ class QuickBuildProjectSupportTest { assertThat(QuickBuildProjectSupport.nonDebuggableVariantMessage("staging")).isNull() assertThat(QuickBuildProjectSupport.nonDebuggableVariantMessage("demoStaging")).isNull() } + + @Test + fun `an opted-in SQLite debugger project is refused with its own message`() { + assertThat(QuickBuildProjectSupport.sqliteDebuggerMessage(sqliteDebuggerEnabled = true)) + .isEqualTo(R.string.quick_build_disabled_sqlite_debugger) + assertThat(QuickBuildProjectSupport.sqliteDebuggerMessage(sqliteDebuggerEnabled = false)).isNull() + } + + @Test + fun `gradle properties decide the opt-in as the Gradle plugin does`() { + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("org.gradle.jvmargs=-Xmx2g\ncogo.sqliteDebugger=true\n")).isTrue() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger = true")).isTrue() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("#cogo.sqliteDebugger=true")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger=false")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebugger=TRUE")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("cogo.sqliteDebuggerX=true")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled("")).isFalse() + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(null as String?)).isFalse() + } + + @Test + fun `the root gradle properties file is read, a missing one is not an opt-in`() { + val dir = + java.nio.file.Files + .createTempDirectory("qb-sqlite") + .toFile() + try { + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(dir)).isFalse() + java.io.File(dir, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(dir)).isTrue() + } finally { + dir.deleteRecursively() + } + } } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt index 598b4d9259..d82dce3507 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt @@ -44,6 +44,16 @@ class AndroidIDEGradlePlugin : Plugin { * `QuickBuildPluginTest`. */ internal const val QUICK_BUILD_PLUGIN_CLASS = "com.itsaky.androidide.gradle.QuickBuildPlugin" + + /** + * Quick Build ships edited classes as a separate payload, compiled by its own daemon from the + * project's source and so never passed through the SQLite rewrite. Mixed with rewritten + * baseline classes it would hand android.database.sqlite objects to requery ones. + */ + internal const val QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE = + "Quick Build is not applied: cogo.sqliteDebugger=true moves the app to requery at build time, " + + "and Quick Build's hot payload is compiled separately and would still use android.database.sqlite. " + + "Building normally; use a Standard Run, or remove the property to use Quick Build." } override fun apply(target: Project) { @@ -76,7 +86,9 @@ class AndroidIDEGradlePlugin : Plugin { } val isQuickBuildEnabled = findProperty(PROPERTY_QUICK_BUILD_ENABLED) == "true" - if (isQuickBuildEnabled) { + if (isQuickBuildEnabled && isSqliteDebuggerEnabled) { + logger.lifecycle(QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + } else if (isQuickBuildEnabled) { // By name, not ::class: Quick Build classes load (and touch newer AGP APIs) // only when the property enables them - see QUICK_BUILD_PLUGIN_CLASS. pluginManager.apply(Class.forName(QUICK_BUILD_PLUGIN_CLASS)) diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt index ba9e0201f2..b8f22e4967 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolver.kt @@ -120,6 +120,10 @@ class ComponentProxiabilityResolver( "com.itsaky.androidide.quickbuild.runtime.QuickBuildKeepAliveService" to "the quick-build runtime's own keep-alive, not user code; skipping keeps it out " + "of the component list and the deploy policy's restart rule", + // Guard only: AndroidIDEGradlePlugin never applies Quick Build to a project that opted into the SQLite debugger. + "org.appdevforall.sqlitedebugger.DebuggerStartup" to + "CoGo's injected SQLite debugger provider (ADFA-7131); it runs once from the base APK dex, " + + "and skipping keeps it out of the component list and the deploy policy's restart rule", "com.google.firebase.components.ComponentDiscoveryService" to "Firebase plumbing whose class never travels in the payload; skipping keeps it out " + "of the component list and the deploy policy's restart rule", diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt new file mode 100644 index 0000000000..edb4bc1cc1 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt @@ -0,0 +1,142 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationVariant +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_QUICK_BUILD_RUNTIME_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_AAR +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR +import org.adfa.constants.KOTLIN_VERSION +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.UnexpectedBuildResultException +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.lang.reflect.Proxy + +/** + * The SQLite debugger (ADFA-7131) reaches the debuggable variants' runtime classpath, and only + * when the project opts in. The fixture project has the debuggable `demoDebug`/`fullDebug` and the + * non-debuggable `demoRelease`/`fullRelease` variants. + */ +class SqliteDebuggerPluginTest { + private val aarName = "sqlite-debugger-release.aar" + private val stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION" + private val debugVariants = listOf("demoDebug", "fullDebug") + private val releaseVariants = listOf("demoRelease", "fullRelease") + + private fun build( + dir: File, + enabled: String?, + aar: File?, + quickBuildAar: File? = null, + runtimeAar: File? = File(dir, "sqlite-rewrite-runtime-release.aar").apply { writeText("aar") }, + ): BuildResult = + buildProject(task = ":app:printRuntimeDeps", configureArgs = { + it += "--init-script" + it += printDepsInitScript(dir).absolutePath + if (enabled != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=$enabled" + if (aar != null) it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${aar.absolutePath}" + if (runtimeAar != null) it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${runtimeAar.absolutePath}" + if (quickBuildAar != null) { + it += "-P$PROPERTY_QUICK_BUILD_ENABLED=true" + it += "-P$PROPERTY_QUICK_BUILD_RUNTIME_AAR=${quickBuildAar.absolutePath}" + } + }) + + private fun BuildResult.declared(variant: String) = output.lines().filter { it.startsWith("RTDEP $variant ") } + + @Test + fun `enabled adds the AAR and kotlin-stdlib to debuggable variants only`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + val result = build(dir, "true", aar) + + assertThat(result.output).contains("Applying SqliteDebuggerPlugin to project ':app'") + for (variant in debugVariants) { + assertThat(result.declared(variant)).contains("RTDEP $variant file $aarName") + assertThat(result.declared(variant)).contains("RTDEP $variant $stdlib") + assertThat(result.output) + .contains("Adding SQLite debugger dependency to variant '$variant' of project ':app'") + } + for (variant in releaseVariants) { + // The fixture's own libraries declare other kotlin-stdlib versions in every variant + assertThat(result.declared(variant).filter { it.contains(aarName) || it.endsWith(stdlib) }) + .isEmpty() + assertThat(result.output) + .doesNotContain("Adding SQLite debugger dependency to variant '$variant' of project ':app'") + } + } + + @Test + fun `debuggable is read from the variant itself, not from a builder`() { + fun variant(debuggable: Boolean): ApplicationVariant = + Proxy.newProxyInstance( + ApplicationVariant::class.java.classLoader, + arrayOf(ApplicationVariant::class.java), + ) { _, method, _ -> + when (method.name) { + "getDebuggable" -> debuggable + else -> throw UnsupportedOperationException(method.name) + } + } as ApplicationVariant + + assertThat(SqliteDebuggerPlugin.readDebuggable(variant(true))).isTrue() + assertThat(SqliteDebuggerPlugin.readDebuggable(variant(false))).isFalse() + } + + @Test + fun `quick build is not applied to a project that opted into the debugger`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + val runtime = File(dir, "quickbuild-runtime.aar").apply { writeText("aar") } + + val withDebugger = build(dir, "true", aar, runtime) + assertThat(withDebugger.output).contains(AndroidIDEGradlePlugin.QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + assertThat(withDebugger.output).doesNotContain("Configuring Quick Build for variant") + assertThat(withDebugger.output).contains("Adding SQLite debugger dependency to variant 'demoDebug'") + + // the control: without the opt-in the same invocation configures Quick Build + val without = build(dir, null, aar, runtime) + assertThat(without.output).doesNotContain(AndroidIDEGradlePlugin.QUICK_BUILD_SQLITE_DEBUGGER_MESSAGE) + assertThat(without.output).contains("Configuring Quick Build for variant") + } + + @Test + fun `a property that is not true adds nothing`( + @TempDir dir: File, + ) { + val aar = File(dir, aarName).apply { writeText("aar") } + for (enabled in listOf(null, "false")) { + val result = build(dir, enabled, aar) + + assertThat(result.output).doesNotContain("Applying SqliteDebuggerPlugin") + for (variant in debugVariants + releaseVariants) { + assertThat(result.declared(variant).filter { it.contains(aarName) }).isEmpty() + } + } + } + + @Test + fun `enabled without the AAR property fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val failure = runCatching { build(dir, "true", null) }.exceptionOrNull() + + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + assertThat(failure!!.message).contains("no property '$PROPERTY_SQLITE_DEBUGGER_AAR' is set") + } + + @Test + fun `enabled with a missing AAR file fails the build and says how to fix it`( + @TempDir dir: File, + ) { + val failure = runCatching { build(dir, "true", File(dir, "absent.aar")) }.exceptionOrNull() + + assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) + assertThat(failure!!.message).contains("SQLite debugger AAR not found") + } +} diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt index f8e066361b..382f522096 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/quickbuild/ComponentProxiabilityResolverTest.kt @@ -78,6 +78,8 @@ class ComponentProxiabilityResolverTest { // library plumbing that must stay out of the component list and the restart rule. assertThat(resolver.resolve("com.google.firebase.components.ComponentDiscoveryService")) .isInstanceOf(ComponentProxiabilityResolver.Resolution.Skip::class.java) + assertThat(resolver.resolve("org.appdevforall.sqlitedebugger.DebuggerStartup")) + .isInstanceOf(ComponentProxiabilityResolver.Resolution.Skip::class.java) } @Test diff --git a/resources/src/main/res/values/strings.xml b/resources/src/main/res/values/strings.xml index 49f1ed7302..ba8a7c439b 100644 --- a/resources/src/main/res/values/strings.xml +++ b/resources/src/main/res/values/strings.xml @@ -1233,6 +1233,7 @@ Quick Build needs an Android app module, and this project has none. Quick Build needs a debuggable build variant, and \"%1$s\" is a release variant. Open Build Variants in the sidebar and select a debug variant. Quick Build could not set up \"%1$s\". If that variant is not debuggable, open Build Variants in the sidebar and select a debug variant. + Quick Build is off while the SQLite debugger is on (cogo.sqliteDebugger=true in gradle.properties), because it would compile your source without the move to requery. Use Run to build the app, or remove that line to use Quick Build. Quick Build setup failed. Check the Build Output for what went wrong. Another build is running. Wait for it to finish, then start Quick Build again. Quick Build could not rebuild your app. Check the Build Output for what went wrong. From dd570d45f5d616049a47ee6fde7ea03b3f1904f6 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:06:03 -0800 Subject: [PATCH 07/12] ADFA-7138 Add a script that checks an APK carries no SQLite debugger scripts/check-release-apk-no-sqlite-debugger.sh fails when an APK holds libsqlitedbg.so, the DebuggerStartup provider, the entry for Code on the Go, or the debugger's class prefix in any dex, so a release build can be checked by script (acceptance criteria 2 and 3). INTERNET is only a warning line: the app may declare it for itself. It runs with pipefail and reads the APK with unzip and aapt2. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../check-release-apk-no-sqlite-debugger.sh | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 scripts/check-release-apk-no-sqlite-debugger.sh diff --git a/scripts/check-release-apk-no-sqlite-debugger.sh b/scripts/check-release-apk-no-sqlite-debugger.sh new file mode 100755 index 0000000000..5e25938e5e --- /dev/null +++ b/scripts/check-release-apk-no-sqlite-debugger.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +# Usage: check-release-apk-no-sqlite-debugger.sh +# +# Fails (exit 1, one line per finding) when an APK carries the SQLite debugger (ADFA-7131): +# - a lib//libsqlitedbg.so entry, +# - the provider org.appdevforall.sqlitedebugger.DebuggerStartup in the manifest, +# - a package com.itsaky.androidide in the manifest, +# - the class descriptor prefix Lorg/appdevforall/sqlitedebugger/ in any classes*.dex. +# INTERNET is only a WARNING line, not a failure: the app may declare it for itself, and the +# manifest cannot say which declaration it came from. Without the provider above, treat the +# warning as the app's own. +# Exit 2 on bad usage or when aapt2, unzip or the APK cannot be found. +# +# aapt2 comes from $AAPT2, else the newest build-tools under $ANDROID_HOME / $ANDROID_SDK_ROOT +# / ~/Android/Sdk. + +set -u +set -o pipefail + +if [ $# -ne 1 ] || [ ! -f "$1" ]; then + echo "usage: $0 " >&2 + exit 2 +fi +apk=$1 + +if ! command -v unzip >/dev/null 2>&1; then + echo "unzip not found: install it to inspect the APK" >&2 + exit 2 +fi + +aapt2=${AAPT2:-} +if [ -z "$aapt2" ]; then + for sdk in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk"; do + [ -n "$sdk" ] || continue + candidate=$(ls -d "$sdk"/build-tools/*/aapt2 2>/dev/null | sort -V | tail -n 1) + if [ -n "$candidate" ]; then + aapt2=$candidate + break + fi + done +fi +if [ -z "$aapt2" ] || [ ! -x "$aapt2" ]; then + echo "aapt2 not found: set AAPT2 or ANDROID_HOME" >&2 + exit 2 +fi + +manifest=$("$aapt2" dump xmltree --file AndroidManifest.xml "$apk" 2>&1) || { + echo "cannot read the manifest of $apk: $manifest" >&2 + exit 2 +} + +failed=0 +fail() { + echo "FAIL: $1" + failed=1 +} + +libs=$(unzip -Z1 "$apk" | grep -E '^lib/[^/]+/libsqlitedbg\.so$' || true) +for entry in $libs; do + fail "native library $entry" +done + +if grep -q 'org\.appdevforall\.sqlitedebugger\.DebuggerStartup' <<<"$manifest"; then + fail "manifest declares provider org.appdevforall.sqlitedebugger.DebuggerStartup" +fi + +# xmltree indents children deeper than their parent: collect the lines under , then look +# at the name of its children. +if awk ' + { match($0, /^ */); indent = RLENGTH } + /E: queries / { inq = 1; qindent = indent; next } + inq && indent <= qindent { inq = 0 } + inq && /E: package / { pkg = 1; next } + inq && /E: / { pkg = 0 } + inq && pkg && /:name\(.*"com\.itsaky\.androidide"/ { found = 1 } + END { exit !found } +' <<<"$manifest"; then + fail "manifest lists package com.itsaky.androidide" +fi + +# A dex stores type descriptors as plain strings, so a debugger class that got in shows up here even when +# the manifest is clean (a library that registers nothing). +# grep -c reads all its input: with pipefail, grep -q exiting early would make unzip die of SIGPIPE +# and the pipeline report failure. +dex_hits=$(unzip -p "$apk" 'classes*.dex' 2>/dev/null | grep -ac 'Lorg/appdevforall/sqlitedebugger/' || true) +if [ "${dex_hits:-0}" -gt 0 ]; then + fail "a classes*.dex contains org.appdevforall.sqlitedebugger classes" +fi + +if grep -q 'android.permission.INTERNET' <<<"$manifest"; then + echo "WARN: manifest has android.permission.INTERNET. It comes from the debugger only if the provider DebuggerStartup is also present (that is a FAIL above); otherwise it is the app's own." +fi + +if [ "$failed" -eq 0 ]; then + echo "OK: no SQLite debugger found in $apk" +fi +exit "$failed" From 4a9bc950b5228c03f7b0ce3e0199d060915c7003 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:06:03 -0800 Subject: [PATCH 08/12] ADFA-7138 Docs: describe the SQLite rewrite and the Gradle wiring gradle-plugin/docs/sqlite-rewrite.md explains the rewrite: what it renames and redirects, what it rejects, the Spike 0 survey result, the helper and debugger AAR wiring, the Quick Build exclusion and the APK check. The sqlite-debugger README, CLAUDE.md and ARCHITECTURE.md list the new module and point to it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- ARCHITECTURE.md | 5 +- CLAUDE.md | 4 +- gradle-plugin/docs/sqlite-rewrite.md | 171 ++++++++++++++++++++++++++ subprojects/sqlite-debugger/README.md | 2 +- 4 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 gradle-plugin/docs/sqlite-rewrite.md diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index e8247b2966..7009784bce 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -95,7 +95,7 @@ Strategy: **layer-and-subsystem based**, not feature-by-feature. The Gradle buil - **`app` depends inward; libraries never depend on `app`.** Subsystems are consumed by `app`, not vice versa. - **Vendored forks are substituted, not imported ad hoc.** `composite-builds/build-deps` and `build-deps-common` provide forked `javac`/`jdt`/`layoutlib`/etc.; `settings.gradle.kts` substitutes them in for `com.itsaky.androidide.build:*`. Don't add a Maven coordinate for something already substituted. Pebble is likewise built from source, as `subprojects:pebble`, for a lexer fix Android needs (its `NOTICE` says what changed); depend on `projects.subprojects.pebble`, not `io.pebbletemplates:pebble`. -- **All module config flows through `composite-builds/build-logic`.** Every Android module gets the `v7`/`v8` ABI flavors centrally (`AndroidModuleConf.kt`) — there is no flavorless `assembleDebug`. `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger`, libraries for other projects that need one artifact, are intentionally excluded from flavors. +- **All module config flows through `composite-builds/build-logic`.** Every Android module gets the `v7`/`v8` ABI flavors centrally (`AndroidModuleConf.kt`) — there is no flavorless `assembleDebug`. `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime`, libraries for other projects that need one artifact, are intentionally excluded from flavors. - **`RepositoriesMode.FAIL_ON_PROJECT_REPOS`** — repositories are declared once in `settings.gradle.kts`; modules must not declare their own. - **Avoid new dependencies.** Check `gradle/libs.versions.toml` first; the build almost certainly already has it. @@ -104,11 +104,12 @@ Strategy: **layer-and-subsystem based**, not feature-by-feature. The Gradle buil These structural facts shape every module. Day-to-day build *commands* live in `CLAUDE.md`; the rules that produce them live here. - **Centralized convention logic.** All Android module setup flows through `composite-builds/build-logic` (`conf/AndroidModuleConf.kt`). Modules stay thin and share configuration, so understanding any module's setup starts here. -- **ABI product flavors.** Every Android module *except* `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` gets two flavors on the `abi` dimension — `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) — defined centrally. There is no flavorless variant; tasks are `assembleV8Debug`, `assembleV7Release`, etc. +- **ABI product flavors.** Every Android module *except* `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` gets two flavors on the `abi` dimension — `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) — defined centrally. There is no flavorless variant; tasks are `assembleV8Debug`, `assembleV7Release`, etc. - **SDK levels** (`build-logic/.../build/config/BuildConfig.kt`): `COMPILE_SDK=36`, `MIN_SDK=28`, `TARGET_SDK=28`. **`TARGET_SDK` is deliberately pinned at 28:** higher targets enforce W^X (write-xor-execute), which blocks executing code from app-writable files. That is fatal for an on-device IDE that compiles and runs code (Gradle, `javac`, Termux binaries), so it is a hard requirement, not tech debt. `MIN_SDK_FOR_APPS_BUILT_WITH_COGO=16` is the floor for the apps a *user* builds with CoGo — distinct from CoGo's own `MIN_SDK`. - **Native asset bundling.** The on-device LLM (`llama-impl`) ships as a per-flavor native AAR, wired through the root `build.gradle.kts` (`bundleLlamaV8Assets` / `assembleV8Assets`, …); prebuilt per-flavor assets live under `assets/release/v7/` and `assets/release/v8/`. - **sqlite-js for apps built with CoGo** (ADFA-7082). `subprojects:sqlite-js` builds the AAR that the JavaScript-in-SQLite project template depends on: sqlite-js vendored with our patches (see its README), for arm64-v8a and armeabi-v7a, minSdk 26, without core-library desugaring. Nothing in CoGo depends on it. Its default entry point runs no code stored in a database; loading stored functions is a separate entry point, only for databases the app made, since loading runs their code. `test-sqlite-js.yml` runs its host tests on pull requests. The manual `release-sqlite-js.yml` publishes it to the moving `sqlite-js-latest` release, from which dev-assets fetches it into the template, as it fetches `plugin-api-latest` for the plugin template. - **SQLite debugger for apps built with CoGo** (ADFA-7131). `subprojects:sqlite-debugger` builds an AAR with a debug-only SQLite debugger and MCP server for apps built with CoGo: a native extension on requery's SQLite plus a loopback HTTP server. Like sqlite-js it has no ABI flavors, and nothing in CoGo depends on it; see its README. +- **AARs bundled for apps built with CoGo.** `:logsender`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` are built as AARs and bundled into the IDE's assets (`AndroidIDEAssetsPlugin`). Code on the Go extracts them and passes their paths to Gradle as properties, and its Gradle plugin injects each into the app: logsender and the debugger into debuggable variants, the rewrite runtime into every variant of a project with `cogo.sqliteDebugger=true`. `:subprojects:sqlite-rewrite-runtime` is Java-only, has no ABI flavors and holds the helpers the build-time rewrite to requery redirects to (ADFA-7138); see `gradle-plugin/docs/sqlite-rewrite.md`. - **Native lib compression** (ADFA-2306, ADFA-4729). The app manifest hard-codes `android:extractNativeLibs="true"` (required: the installer must materialize libs in `nativeLibraryDir`, e.g. `libshizuku.so` is an executable the adb shell runs from there). That attribute overrides the `jniLibs.useLegacyPackaging` DSL, so AGP packages `lib//*.so` deflate-compressed in **every** APK — ~5.9 MB smaller (`libtree-sitter-kotlin.so` alone is 4.18 MB → 339 kB). The trap is the `recompressApk` post-step (release always, debug in CI only): its no-compress lists in `app/build.gradle.kts` must NOT contain `"so"`, or it silently re-stores the libs and undoes the saving — which is what ADFA-2306 fixed for release and ADFA-4729 for CI debug. Locally built debug APKs (including the e2e farm's) never run that step and were always fine. - **Quick Build shares the Gradle distribution's toolchain** (ADFA-4931). `:app`'s `quickBuildDaemonZip` leaves the Kotlin compiler and eight other jars out of the daemon zip, because the on-device Gradle distribution already carries them; `QuickBuildArtifactStager` links them in at provision time. That makes the daemon's Kotlin version the distribution's, so `:quickbuild:daemon` takes its Kotlin artifacts at `org.adfa.constants.GRADLE_DISTRIBUTION_KOTLIN_VERSION`, and `:app:quickBuildDistJarCheck` fails the build when the distribution it packages lacks a linked jar. See [ADR 0016](docs/adr/0016-quick-build-compiles-outside-gradle.md). - **`app` package layout is by concern, not feature:** `activities`, `fragments`, `services`, `di`, `agent`, `viewmodel(s)`, `repositories`, `roomData`, `localWebServer`, `preferences`, `ui` (Compose screens live under `ui/compose`), `templates/manager` (the Manager screen's `.cgt`-parsing data layer, with direct filesystem access to `Environment.TEMPLATES_DIR` — distinct from the plugin-facing `IdeTemplateService` in `plugin-api`/`plugin-manager`), `utils`, …. diff --git a/CLAUDE.md b/CLAUDE.md index 7334588a8c..1d9f9d7499 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -15,7 +15,7 @@ flox activate -d flox/local -- ./gradlew ``` - **Debug APK (arm64, the usual target):** `flox activate -d flox/local -- ./gradlew :app:assembleV8Debug --parallel --max-workers=6` -- **Module unit tests:** `flox activate -d flox/local -- ./gradlew :editor:testV7DebugUnitTest` - unit tests are per-flavor, there is no plain `test` task. `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` are the modules without flavors: `:plugin-api:testDebugUnitTest`. +- **Module unit tests:** `flox activate -d flox/local -- ./gradlew :editor:testV7DebugUnitTest` - unit tests are per-flavor, there is no plain `test` task. `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` are the modules without flavors: `:plugin-api:testDebugUnitTest`. - **Single unit test:** append `--tests "com.itsaky.androidide.SomeTest"` to the flavored task above. - **Fast iteration:** during multi-file/multi-module changes, verify with targeted `:module:compileV8DebugKotlin`/`compileV8DebugJavaWithJavac` invocations (batch several modules into one Gradle call) rather than a full assemble. Reserve `:app:assembleV8Debug` for final end-to-end verification — it's slow (multi-minute) in this multi-module project, and running it after every small change adds up. - **Long-running commands: narrate them, and background only the read-only ones.** Anything that can exceed ~60s — `assembleV8Debug`, a full test sweep, a cold Gradle invocation of any kind (daemon start plus configuring this many modules is ~20s before any task runs, and far worse under memory pressure), and `git push`, which runs Spotless through the hook — gets a line before it starts saying what is running and roughly how long it takes, and a status line every couple of minutes while it runs: elapsed time, the last output line, whether it is still progressing. A silent terminal is indistinguishable from a hang, and saying which it is is your job, not the user's to ask. @@ -26,7 +26,7 @@ When the user names a CI/CD job ("the sonar job", "the analyze workflow"), read ### ABI product flavors (v7 / v8) -Every module except `:plugin-api`, `:subprojects:sqlite-js` and `:subprojects:sqlite-debugger` carries `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) flavors, so build tasks are `assembleV8Debug`, `assembleV7Release`, etc. — there is **no** flavorless `assembleDebug`. Those three build one artifact for other projects. See [ARCHITECTURE.md](ARCHITECTURE.md) (Build & Module Configuration) for flavors, native asset bundling, and SDK levels. +Every module except `:plugin-api`, `:subprojects:sqlite-js`, `:subprojects:sqlite-debugger` and `:subprojects:sqlite-rewrite-runtime` carries `v7` (`armeabi-v7a`) and `v8` (`arm64-v8a`) flavors, so build tasks are `assembleV8Debug`, `assembleV7Release`, etc. — there is **no** flavorless `assembleDebug`. Those four build one artifact for other projects. See [ARCHITECTURE.md](ARCHITECTURE.md) (Build & Module Configuration) for flavors, native asset bundling, and SDK levels. ### Emulator / device diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md new file mode 100644 index 0000000000..2b6cf2186e --- /dev/null +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -0,0 +1,171 @@ +# SQLite rewrite (ADFA-7138) + +When `cogo.sqliteDebugger=true`, the Gradle plugin registers an AGP class transform +(`com.itsaky.androidide.gradle.sqlite.SqliteRewriteVisitorFactory`, scope ALL, every variant) that moves +the app's and its libraries' classes from `android.database.sqlite` to requery 3.49.0, so debug and release +run the same SQLite. User source does not change. + +## Turning it on + +An existing project needs: + +1. `cogo.sqliteDebugger=true` in `gradle.properties`. +2. requery's AAR (`sqlite-android.aar`) in the app's `libs/`, as a file dependency + (`implementation(files("libs/sqlite-android.aar"))`), as the ADFA-7082 template does. +3. androidx.sqlite on the runtime classpath (`androidx.sqlite:sqlite`, which Room, WorkManager and most androidx + libraries bring). A file dependency brings none of the dependencies of the AAR, and requery implements + androidx.sqlite's interfaces. It also calls `androidx.core.os.CancellationSignal`, which AppCompat's + androidx.core provides. + +The helper classes the rewrite redirects to (`SqliteRewriteRuntime`) are not in `libs/`: Code on the Go bundles +`subprojects/sqlite-rewrite-runtime`'s release AAR, extracts it with the other tools and passes its path as +`androidide.sqliteRewriteRuntime.aar`, as it does for the debugger AAR. `SqliteRewritePlugin` adds it to the +runtime classpath of every variant of a project that opted in, and fails the build when the file is missing. + +A task `checkSqliteRewriteClasspath` runs before the compile and the transform of each variant. It fails +with an actionable message when the variant's runtime classpath has no +`io/requery/android/database/sqlite/SQLiteDatabase.class` or `androidx/sqlite/db/SupportSQLiteDatabase.class`. + +The property is required because requery reaches apps only through `libs/`. Without it the rewritten classes +cannot load, so the transform runs only on projects that opted in. Commenting the line out turns it off. + +## Rules + +- Rename: the 10 classes requery shares (`SQLiteClosable`, `SQLiteCursor`, `SQLiteCursorDriver`, + `SQLiteDatabase`, `SQLiteDatabase$CursorFactory`, `SQLiteOpenHelper`, `SQLiteProgram`, `SQLiteQuery`, + `SQLiteQueryBuilder`, `SQLiteStatement`) and `android.database.DatabaseErrorHandler` and + `DefaultDatabaseErrorHandler`. androidx's `FrameworkSQLiteOpenHelperFactory` is not renamed; see the design + change below. Kept: the exceptions, `SQLiteTransactionListener`, `android.database.Cursor`, `CursorWindow`. + Overrides count: a `SQLiteOpenHelper` subclass or a `SQLiteCursorDriver`/`CursorFactory` implementation + ends up with requery's descriptors. +- Descriptor: `String[]` becomes `Object[]` (selectionArgs) for `query`, `queryWithFactory`, `rawQuery`, + `rawQueryWithFactory` and `SQLiteCursorDriver.query`, in calls and in the override declaration. +- Redirect to `org.appdevforall.sqliterewrite.SqliteRewriteRuntime`: `SQLiteDatabase.openDatabase` with + flags (Android's flag values differ from requery's; the flags are compiled into the app), + `Context.openOrCreateDatabase` / `deleteDatabase` (INVOKEVIRTUAL on `Context` or a subclass; subclasses + are found with AGP's class loader, plus a fixed list of the framework ones), and an + `android.os.CancellationSignal` argument of a requery-bound call, converted right before the call (it + is always the last argument), and `android.database.DatabaseUtils.queryNumEntries`, `longForQuery` and + `stringForQuery` (every overload; requery has no `DatabaseUtils`, so the runtime carries Android's own + implementations, written against Android's types). +- Fail the build, naming class, method and line: a call to a member requery lacks (for example + `execPerConnectionSQL`, `setCustomScalarFunction`, `SQLiteQueryBuilder.isStrict`; also the deprecated cursor + members `SQLiteCursor` inherits from android's `AbstractCursor` and `AbstractWindowedCursor` that requery's + copies lack: `isBlob`, `isFloat`, `isLong`, `isString`, `getUpdatedField`, `isFieldUpdated`, + `getNotificationUris`, `setNotificationUris`, and the protected field `mWindow`; `SqliteRewriteResolutionTest` probes the inherited members of every + renamed class against requery), SQL text with `COLLATE UNICODE` (any case or whitespace, in any string + constant, which covers Room's generated `_Impl` classes: requery's native SQLite has no ICU, so the collation + does not exist), any use of + `SQLiteDatabase.OpenParams`, a call that passes android's `CursorWindow` to a renamed class (calls on + `CursorWindow` itself are fine), and a cast of + an SQLite cursor to `android.database.AbstractWindowedCursor`, `AbstractCursor` or `CrossProcessCursor` + (requery's cursors extend its own copies of the first two, and implement only `android.database.Cursor`). + A cast guarded by `instanceof` of the same type on the same local (`if (c instanceof X) ((X) c)`, `&&`, `?:`, + or an early return when it is false) is not reported. `COLLATE LOCALIZED` only logs a warning: it works, but + compares bytes (no ICU) instead of sorting by locale. All violations of a class are reported together. A platform method outside the renamed set whose + descriptor mentions a renamed class (found with javap over android.jar: the other `DatabaseUtils` methods + `blobFileDescriptorForQuery` and `bindObjectToProgram`, `DatabaseUtils$InsertHelper`, `OpenParams` + accessors) cannot link once the descriptor is renamed, so a call to one that is not redirected above fails + the build. `SqliteRewriteResolutionTest` probes every such member in android.jar. + A `Context.openOrCreateDatabase` / `deleteDatabase` call that cannot be redirected also fails the build: + `super.` calls (INVOKESPECIAL), calls whose owner class cannot be resolved (it might be a Context), and + a method with one of those signatures that overrides it in a Context subclass (the redirect would skip + the override). +- Method references: a handle in an invokedynamic (`db::rawQuery`, `ctx::deleteDatabase`) that any rule + above would change, redirect or reject fails the build, naming class, method and line, with the advice to + write a lambda (its body is an ordinary call, which the rules handle). The rules are not applied to the + handle itself: it cannot be redirected in place, and its descriptor is fixed by the call site. +- Frames: `COPY_FRAMES`, AGP's default. The one change that breaks them is a retyped + `SQLiteCursorDriver.query` implementation, whose `String[]` parameter becomes `Object[]` while its body, and + the full frames copied from it, still say `String[]`. For each non-abstract method whose descriptor is retyped + that way, the rewriter inserts `ALOAD n; CHECKCAST [Ljava/lang/String;; ASTORE n` at method entry, so the body + stays valid, and writes every frame of that method in full (compressed frames are relative to the implicit first + frame, which now has the wide type). A driver that is handed an `Object[]` with an element that is not a + `String` throws `ClassCastException` on entry; requery's own drivers pass strings. + +Excluded: `io.requery.**`. The helper's package is renamed only (a redirect would call itself). +`androidx.sqlite.db.framework.**` and `androidx.core.database.**` are rewritten but exempt from the fail rule: see +below, and androidx.core's `SQLiteCursorCompat$Api28Impl` calls `SQLiteCursor.setFillWindowForwardOnly`, which requery +lacks, so failing the build for it would fail every app that depends on androidx.core (found by the transform test +on androidx.core 1.9.0). Calling `SQLiteCursorCompat.setFillWindowForwardOnly` on requery's cursor throws +`NoSuchMethodError`. + +## Survey: what reaches Android's SQLite (androidx.sqlite framework 2.6.2, Room 2.8.4, WorkManager 2.10.0) + +| Where | Reference | Handling | +| --- | --- | --- | +| `RoomDatabase$Builder.build`, `PrePackagedCopyOpenHelper`, WorkManager's `WorkDatabase$Companion` | `new FrameworkSQLiteOpenHelperFactory` | kept: androidx's factory runs on requery through the renamed classes (design change below) | +| `FrameworkSQLiteDatabase`, `FrameworkSQLiteOpenHelper$OpenHelper`, `FrameworkSQLiteProgram`/`Statement` | every member of the renamed classes they use | all resolve in requery after the rules; `SqliteRewriteResolutionTest` transforms the real 2.6.2 framework classes, verifies them and resolves every reference (access and static-ness included) | +| `FrameworkSQLiteDatabase$Api30Impl` | `execPerConnectionSQL` (NO in requery's own class) | the only reference to a member requery lacks; it resolves to `SupportSQLiteDatabase`'s default method, which throws `UnsupportedOperationException` | +| `FrameworkSQLiteDatabase.query(.., CancellationSignal)` | `rawQueryWithFactory` with android's signal | signal converted | +| `FrameworkSQLiteOpenHelper$OpenHelper`, `PrePackagedCopyOpenHelper` | `Context.deleteDatabase`, `getDatabasePath` | redirect (`deleteDatabase`); `getDatabasePath` needs none | +| Room `DBUtil.query` | `instanceof` and cast to `AbstractWindowedCursor`, `CursorWindow.getNumRows` | untouched: the value comes from `SupportSQLiteDatabase.query`, not an SQLite API, and the cast is guarded by `instanceof`, which is false for requery's cursor (Room skips its copy optimisation) | +| Room `checkUniquenessException` | `SQLiteConstraintException` | kept class | +| `androidx.sqlite.driver.AndroidSQLiteDriver.open` | `SQLiteDatabase.openOrCreateDatabase(String, null)` and `new FrameworkSQLiteDatabase(SQLiteDatabase)` | works only if `FrameworkSQLiteDatabase` is rewritten too | + +### Design change (2026-11-13): androidx's factory stays + +The first version renamed `new FrameworkSQLiteOpenHelperFactory()` to requery's `RequerySQLiteOpenHelperFactory` +and excluded the factory class. That is dropped. requery's factory reads only the context, the name and the +callback of the `SupportSQLiteOpenHelper.Configuration` (checked with javap): it ignores `noBackupDirectory` and +`allowDataLossOnRecovery`, and has none of the framework's cross-process lock. WorkManager 2.10 sets the first +two, so its database would be recreated empty after `WorkDatabasePathHelper.migrateDatabase` on each start. + +Instead the androidx.sqlite framework classes (`androidx.sqlite.db.framework.*`, rewritten with the fail rule +off) run on requery through the normal rename and descriptor rules: `FrameworkSQLiteOpenHelper`, its inner +`OpenHelper` (a subclass of the renamed `SQLiteOpenHelper`), `FrameworkSQLiteDatabase`, `-Statement` and +`-Program` keep their behaviour, including the no-backup directory and the recovery flags, and call requery's +classes. They are not excluded, which also keeps `androidx.sqlite.driver.AndroidSQLiteDriver` working: it builds a +`FrameworkSQLiteDatabase` from a plain `SQLiteDatabase`. + +Every reference of these classes resolves in requery. The one member requery's own `SQLiteDatabase` lacks is +`execPerConnectionSQL`, which `FrameworkSQLiteDatabase$Api30Impl` calls; it resolves to the default method of +`SupportSQLiteDatabase` (checked with javap), which throws `UnsupportedOperationException`. So +`execPerConnectionSQL` through the `SupportSQLiteDatabase` interface stays unsupported, and the fail rule is off for +this package, so the build does not report it. + +## Checking a release APK + +`scripts/check-release-apk-no-sqlite-debugger.sh ` fails when an APK carries the debugger (native library, +provider, `` entry or classes). QA runs it on the release APK of an opted-in project: the release build must never contain the debugger. + +## Known limits + +- Room's `BundledSQLiteDriver` is not covered (it brings its own SQLite). +- requery's native library is built without ICU: there is no `UNICODE` collator (a `COLLATE UNICODE` string + constant fails the build, see above) and `LOCALIZED` compares bytes (a warning). +- `SupportSQLiteDatabase.execPerConnectionSQL` is unsupported (see the design change): no build failure, an + `UnsupportedOperationException` when called. +- Passing an Android `CursorWindow` where a (renamed) `SQLiteClosable` is expected cannot work: android's + `CursorWindow` extends android's `SQLiteClosable`, which stays the platform's class. Calls that name a + `CursorWindow` in a renamed class's descriptor fail the build; calls on a `CursorWindow` are left alone. +- The items below were reported by review and not verified here: `ATTACH` on a WAL database (requery runs it on + one connection only; Android turns write-ahead logging off on the first `ATTACH`); a cancelled query throws + `androidx.core.os.OperationCanceledException`, not `android.os.OperationCanceledException`; page size and + `wal_autocheckpoint` defaults can differ from the platform's; there is no compatibility WAL mode; the cursor + window is a fixed 2 MB, and a row larger than it throws `IllegalStateException`; `beginTransactionReadOnly` + is not in requery (it fails the build) and its read-only semantics have no counterpart; requery has no + `StrictMode` hooks, so StrictMode does not report database access on the main thread. +- Quick Build is not applied to a project with `cogo.sqliteDebugger=true`: its hot payload is compiled by + the Quick Build daemon from the project's source (and its baseline payload is diverted out of the classes + pipeline), so those classes would still use `android.database.sqlite` next to rewritten baseline classes. + The plugin logs why and the build is a normal one. The IDE does not provision Quick Build for such a project + either (`QuickBuildProjectSupport.sqliteDebuggerMessage`): it shows `quick_build_disabled_sqlite_debugger`, which + says Quick Build is off while the SQLite debugger is on. +- Classes loaded by name (`Class.forName`, reflection, strings naming `android.database.sqlite.*`) are not + rewritten. +- The `AbstractWindowedCursor` check follows the cursor through locals and the stack only. A cast of a + cursor that reached the method through a field, a parameter or another call is not seen (the app throws + `ClassCastException` at run time), and a cast of a cursor from an SQLite API to a type that is not an + SQLite cursor cannot be a false positive. +- Compile-time constants are copied into callers, so uses of `NO_LOCALIZED_COLLATORS`, `JOURNAL_MODE_*` or `SYNC_MODE_*` are + not visible to the fail rule. `NO_LOCALIZED_COLLATORS` is dropped by the flag translation. +- The file permission bits of the `mode` of `Context.openOrCreateDatabase` are not applied. +- A direct subclass of `SQLiteQueryBuilder` or `SQLiteCursor` that overrides a member requery lacks is not + reported. +- `instanceof` is not rewritten: `cursor instanceof AbstractWindowedCursor`, `AbstractCursor` or + `CrossProcessCursor` is silently false for requery's cursors (no build failure), which is what Room's + `DBUtil.query` relies on. +- Android's `CancellationSignal` holds one listener: the conversion replaces any listener the caller set. +- Which `SQLiteException` subclass requery throws for an error code (for example `SQLiteConstraintException`, which + Room catches) is decided by its native layer; this rewrite does not change it and the survey did not check it. diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 55b60427e7..08f92e3e6d 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -22,6 +22,7 @@ MCP client --HTTP, 127.0.0.1--> McpHttp -> McpProtocol -> DatabaseTools -> Debug - `engine/`: `DebugEngine` is what the server needs from the extension, `NativeDebugEngine` implements it over JNI, and `EventDecoder` turns the event buffer's bytes into `Event`s. - `mcp/`: `McpHttp` is the HTTP/1.1 listener, `McpProtocol` is JSON-RPC and the MCP session handling, `DatabaseTools` is the tools: `list_databases`, `list_connections`, `get_schema`, `query`, `explain_query_plan`, `get_stats`, `integrity_check`, `get_events`. - `DebuggerStartup` is a `ContentProvider` with the highest `initOrder`, declared in the AAR's manifest. It runs before the app's own providers and `Application.onCreate`, so the extension is registered before the app opens a database. It installs the extension synchronously in `onCreate` (so an app that opens its database in `Application.onCreate` is still seen) and starts the server on a background thread. Everything after the debuggable check is wrapped in `catch (Throwable)`: a missing requery library (`UnsatisfiedLinkError`) means the debugger is disabled and logged, never a crash of the host app. +- **Gradle plugin wiring (ADFA-7138).** An app opts in with `cogo.sqliteDebugger=true` in its `gradle.properties`; commenting the line out turns everything off. With it, Code on the Go's Gradle plugin (`AndroidIDEGradlePlugin`) applies two plugins. `SqliteRewritePlugin` registers a class transform on every variant that moves the app's and its libraries' classes from `android.database.sqlite` to requery (see `gradle-plugin/docs/sqlite-rewrite.md`) and adds the rewrite's helper AAR (`:subprojects:sqlite-rewrite-runtime`) to every variant. `SqliteDebuggerPlugin` adds the debugger AAR and kotlin-stdlib to debuggable variants only. Code on the Go passes both AAR paths to Gradle as properties. Quick Build is not applied to an opted-in project, because its payload would compile the project's source without the rewrite. The apps' minSdk must be at least 21, the floor of requery, the helper AAR and the debugger. ### The event record @@ -49,6 +50,5 @@ No workflow in `.github/workflows` runs either yet. ## Not done yet - Broadcasting the port and token to Code on the Go (ADFA-7137). `DebuggerStartup` only keeps them in `DebuggerStartup.port` and `endpointToken` and logs the port, never the token. -- Gradle plugin wiring and the build-time rewrite that adds the library to debug builds (ADFA-7138). - The AI-Agent-MCP side that connects to the server (ADFA-7139). - Verification on a device. The C tests run on the host and the Kotlin tests on the JVM; nothing has run in an app on a device or emulator. From 2c78e0af9b4fff8a49fb3db5aaf6a1cf2391a490 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:47:07 -0800 Subject: [PATCH 09/12] ADFA-7138 Quick Build opt-in check reads the sources Gradle's findProperty sees The plugin decides the opt-in with findProperty, which sees the Gradle user home and the app module gradle.properties as well as the root one. The IDE read only the root file, so such a project was staged and then refused with the misleading variant-setup message, and a read failure counted as not opted in without a log line. The check now reads user home, app module and root (first defining one wins) and logs a read failure. Tests: opt-in in root, in user home, in the module, false overriding true, and an unreadable file reported and treated as not set. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 13 ++-- .../quickbuild/QuickBuildProjectSupport.kt | 68 ++++++++++++++----- .../QuickBuildProjectSupportTest.kt | 63 +++++++++++++++-- gradle-plugin/docs/sqlite-rewrite.md | 6 +- 4 files changed, 122 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt index ae0c963fd9..28bc92cec4 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -15,6 +15,7 @@ import com.itsaky.androidide.tooling.api.messages.BuildRunType import com.itsaky.androidide.tooling.api.messages.GradleBuildParams import com.itsaky.androidide.tooling.api.messages.TaskExecutionMessage import com.itsaky.androidide.tooling.api.messages.result.TaskExecutionResult +import com.itsaky.androidide.utils.Environment import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -346,12 +347,16 @@ class GradleQuickBuildProvisioner( // Before staging, which writes into the project: the Gradle plugin never applies Quick Build // to an opted-in project, so building would only end in "variant setup missing". QuickBuildProjectSupport - .sqliteDebuggerMessage(QuickBuildProjectSupport.isSqliteDebuggerEnabled(projectRoot)) - ?.let { refusal -> + .sqliteDebuggerMessage( + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + projectRoot, + Environment.GRADLE_USER_HOME, + projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, + ), + )?.let { refusal -> log.info( - "Quick Build is not provisioned: {} is true in {}/gradle.properties", + "Quick Build is not provisioned: {} is true in the project's or the Gradle user home's gradle.properties", GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, - projectRoot, ) return ProxyAppBuildResult.Failed(context.getString(refusal)) } diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt index 89728f2f6c..58fe82ebff 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt @@ -3,7 +3,9 @@ package com.itsaky.androidide.quickbuild import androidx.annotation.StringRes import com.itsaky.androidide.resources.R import com.itsaky.androidide.tooling.api.GradlePluginConfig +import org.slf4j.LoggerFactory import java.io.File +import java.io.IOException import java.io.StringReader import java.util.Properties @@ -84,25 +86,59 @@ object QuickBuildProjectSupport { } /** - * Whether the root `gradle.properties` text opts in, decided as the Gradle plugin does: the - * value is exactly `true`. + * Whether one `gradle.properties` text opts in, decided as the Gradle plugin does: the value is + * exactly `true`. * - * @param gradleProperties the file's text, or null when the project has none. + * @param gradleProperties the file's text, or null when there is none. */ - fun isSqliteDebuggerEnabled(gradleProperties: String?): Boolean { - if (gradleProperties == null) return false - val properties = Properties() - return try { - properties.load(StringReader(gradleProperties)) - properties.getProperty(GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED) == "true" - } catch (e: IllegalArgumentException) { - false + fun isSqliteDebuggerEnabled(gradleProperties: String?): Boolean = gradleProperties != null && propertyValue(gradleProperties) == "true" + + /** + * Whether the project opts in, from the same files the Gradle plugin's `findProperty` sees in + * Code on the Go's builds: the Gradle user home `gradle.properties` (`GRADLE_USER_HOME`, which + * overrides the project files), the app module's, and the root's. Gradle loads a + * `gradle.properties` from every project directory into that project, and a subproject also + * inherits its parent's properties, so the first of these that defines the property wins. + * `-P` arguments are not read: Code on the Go passes none for this property. + * + * A file that cannot be read is reported to [onReadFailure] and counts as not defining it. + * + * @param moduleDirs the directories of the app modules the plugin is applied to. + */ + fun isSqliteDebuggerEnabled( + projectRoot: File, + gradleUserHome: File?, + moduleDirs: List = emptyList(), + onReadFailure: (File, Throwable) -> Unit = { file, error -> + log.warn("Cannot read {}; treating it as not defining the SQLite debugger opt-in", file, error) + }, + readText: (File) -> String = File::readText, + ): Boolean { + val files = + listOfNotNull(gradleUserHome) + moduleDirs + projectRoot + for (entry in files) { + val file = File(entry, "gradle.properties") + val text = + try { + file.takeIf { it.isFile }?.let(readText) + } catch (e: IOException) { + onReadFailure(file, e) + null + } ?: continue + val value = propertyValue(text) ?: continue + return value == "true" } + return false } - /** [isSqliteDebuggerEnabled] for the `gradle.properties` in [projectRoot]; false when it cannot be read. */ - fun isSqliteDebuggerEnabled(projectRoot: File): Boolean = - isSqliteDebuggerEnabled( - runCatching { File(projectRoot, "gradle.properties").takeIf { it.isFile }?.readText() }.getOrNull(), - ) + private fun propertyValue(gradleProperties: String): String? = + try { + Properties() + .apply { load(StringReader(gradleProperties)) } + .getProperty(GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED) + } catch (e: IllegalArgumentException) { + null + } + + private val log = LoggerFactory.getLogger("QB-Support") } diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt index ae57986533..ceb3e13999 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt @@ -96,18 +96,67 @@ class QuickBuildProjectSupportTest { assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(null as String?)).isFalse() } - @Test - fun `the root gradle properties file is read, a missing one is not an opt-in`() { - val dir = + private fun withDirs(block: (root: java.io.File, home: java.io.File, module: java.io.File) -> Unit) { + val base = java.nio.file.Files .createTempDirectory("qb-sqlite") .toFile() try { - assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(dir)).isFalse() - java.io.File(dir, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") - assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(dir)).isTrue() + block( + java.io.File(base, "root").apply { mkdirs() }, + java.io.File(base, "home").apply { mkdirs() }, + java.io.File(base, "root/app").apply { mkdirs() }, + ) } finally { - dir.deleteRecursively() + base.deleteRecursively() } } + + private fun java.io.File.props(text: String) = java.io.File(this, "gradle.properties").writeText(text) + + @Test + fun `the root gradle properties file is read, a missing one is not an opt-in`() = + withDirs { root, home, _ -> + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home)).isFalse() + root.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home)).isTrue() + } + + @Test + fun `the Gradle user home gradle properties opts in`() = + withDirs { root, home, module -> + home.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isTrue() + } + + @Test + fun `the app module gradle properties opts in`() = + withDirs { root, home, module -> + module.props("cogo.sqliteDebugger=true\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isTrue() + } + + @Test + fun `false in a higher-priority file overrides true in the root`() = + withDirs { root, home, module -> + root.props("cogo.sqliteDebugger=true\n") + home.props("cogo.sqliteDebugger=false\n") + assertThat(QuickBuildProjectSupport.isSqliteDebuggerEnabled(root, home, listOf(module))).isFalse() + } + + @Test + fun `an unreadable file is logged and treated as not set`() = + withDirs { root, home, _ -> + home.props("cogo.sqliteDebugger=true\n") + val failures = mutableListOf() + val enabled = + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + root, + home, + onReadFailure = { file, _ -> failures += file }, + readText = { throw java.io.IOException("denied") }, + ) + assertThat(enabled).isFalse() + assertThat(failures).containsExactly(java.io.File(home, "gradle.properties")) + } } diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 2b6cf2186e..85310aa101 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -9,7 +9,11 @@ run the same SQLite. User source does not change. An existing project needs: -1. `cogo.sqliteDebugger=true` in `gradle.properties`. +1. `cogo.sqliteDebugger=true` in `gradle.properties`. The plugin reads it with Gradle's `findProperty`, so the + project's root file, the app module's own `gradle.properties` and the Gradle user home's + (`GRADLE_USER_HOME`, `~/.gradle` under Code on the Go's home) all work, the user home first. The IDE's Quick + Build check (`QuickBuildProjectSupport.isSqliteDebuggerEnabled`) reads those same three files, in that order, + and logs a file it cannot read; it does not see `-P` arguments, which Code on the Go does not pass for this. 2. requery's AAR (`sqlite-android.aar`) in the app's `libs/`, as a file dependency (`implementation(files("libs/sqlite-android.aar"))`), as the ADFA-7082 template does. 3. androidx.sqlite on the runtime classpath (`androidx.sqlite:sqlite`, which Room, WorkManager and most androidx From 4e4718f4cd5dbf609e0fdacf70de2253c2f87b50 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:47:07 -0800 Subject: [PATCH 10/12] ADFA-7138 Fail the build when a variant's runtime classpath cannot be reached SqliteRewritePlugin registers the class rewrite before it adds the helper AAR and the classpath check, and withRuntimeConfiguration silently skipped a variant type it did not know, so the app would build and throw NoClassDefFoundError on the device. The two SQLite plugins now use requireRuntimeConfiguration, which throws naming the variant and its class; LogSender keeps the skipping helper. Test: a stand-in variant (TestKit cannot produce an unknown variant type) makes the decision function throw, while withRuntimeConfiguration still skips it. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 5 ++- .../androidide/gradle/LogSenderPlugin.kt | 38 +++++++++++++---- .../androidide/gradle/SqliteDebuggerPlugin.kt | 2 +- .../gradle/sqlite/SqliteRewritePlugin.kt | 4 +- .../gradle/RuntimeConfigurationTest.kt | 42 +++++++++++++++++++ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 85310aa101..2472d1d548 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -24,7 +24,10 @@ An existing project needs: The helper classes the rewrite redirects to (`SqliteRewriteRuntime`) are not in `libs/`: Code on the Go bundles `subprojects/sqlite-rewrite-runtime`'s release AAR, extracts it with the other tools and passes its path as `androidide.sqliteRewriteRuntime.aar`, as it does for the debugger AAR. `SqliteRewritePlugin` adds it to the -runtime classpath of every variant of a project that opted in, and fails the build when the file is missing. +runtime classpath of every variant of a project that opted in, and fails the build when the file is missing or +when a variant's runtime classpath cannot be reached (a variant type the plugin does not know, named in the message): +the rewrite is already registered for it, so going on would give an app that throws `NoClassDefFoundError`. +`SqliteDebuggerPlugin` fails the same way for the variants it adds the debugger to. A task `checkSqliteRewriteClasspath` runs before the compile and the transform of each variant. It fails with an actionable message when the variant's runtime classpath has no diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt index 99416b3d7d..4cb348c462 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt @@ -88,13 +88,37 @@ class LogSenderPlugin : Plugin { } /** - * Runs [action] on the variant's runtime classpath configuration. Shared with - * [SqliteDebuggerPlugin]; does nothing for a variant type it does not recognize. + * The variant's runtime classpath configuration, or null for a variant type it does not recognize + * (a wrapper a future AGP may add). */ -internal fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { - if (this is ApplicationVariantImpl) { - variantDependencies.runtimeClasspath.action() - } else if (this is AnalyticsEnabledApplicationVariant) { - delegate.withRuntimeConfiguration(action) +internal fun ApplicationVariant.runtimeConfigurationOrNull(): Configuration? = + when (this) { + is ApplicationVariantImpl -> variantDependencies.runtimeClasspath + is AnalyticsEnabledApplicationVariant -> delegate.runtimeConfigurationOrNull() + else -> null } + +/** + * Runs [action] on the variant's runtime classpath configuration. Used by [LogSenderPlugin], and + * does nothing for a variant type it does not recognize; the SQLite plugins use + * [requireRuntimeConfiguration] instead. + */ +internal fun ApplicationVariant.withRuntimeConfiguration(action: Configuration.() -> Unit) { + runtimeConfigurationOrNull()?.action() +} + +/** + * Like [withRuntimeConfiguration], but fails the build when the configuration cannot be reached. For + * plugins that have already registered something that needs what [action] adds: skipping it would + * build an app that throws NoClassDefFoundError on the device. + */ +internal fun ApplicationVariant.requireRuntimeConfiguration(action: Configuration.() -> Unit) { + val configuration = + runtimeConfigurationOrNull() + ?: throw GradleException( + "Cannot reach the runtime classpath of variant '$name': its type ${javaClass.name} is not one this " + + "plugin knows. The SQLite debugger cannot add its runtime dependencies to it, so the app would " + + "fail at run time. Remove 'cogo.sqliteDebugger' from gradle.properties to build without it.", + ) + configuration.action() } diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt index bb37d6a7b1..72e2b3985d 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteDebuggerPlugin.kt @@ -70,7 +70,7 @@ class SqliteDebuggerPlugin : Plugin { target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).apply { val addDebugger = { variant: ApplicationVariant -> - variant.withRuntimeConfiguration { + variant.requireRuntimeConfiguration { logger.lifecycle( "Adding SQLite debugger dependency to variant '{}' of project '{}'", variant.name, diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt index a3b621b501..3f80201b35 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewritePlugin.kt @@ -5,7 +5,7 @@ package com.itsaky.androidide.gradle.sqlite import com.android.build.api.instrumentation.InstrumentationScope import com.android.build.api.variant.ApplicationAndroidComponentsExtension import com.itsaky.androidide.gradle.APP_PLUGIN -import com.itsaky.androidide.gradle.withRuntimeConfiguration +import com.itsaky.androidide.gradle.requireRuntimeConfiguration import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_REWRITE_RUNTIME_AAR import org.gradle.api.GradleException @@ -61,7 +61,7 @@ class SqliteRewritePlugin : Plugin { variant.instrumentation.setAsmFramesComputationMode(SqliteRewriter.FRAMES_MODE) val capitalized = variant.name.replaceFirstChar { it.uppercase() } - variant.withRuntimeConfiguration { + variant.requireRuntimeConfiguration { logger.lifecycle("Adding SQLite rewrite runtime dependency to variant '{}' of project '{}'", variant.name, target.path) dependencies.add(target.dependencies.create(target.fileTree(runtimeAar))) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt new file mode 100644 index 0000000000..506e1b8ddb --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/RuntimeConfigurationTest.kt @@ -0,0 +1,42 @@ +package com.itsaky.androidide.gradle + +import com.android.build.api.variant.ApplicationVariant +import com.google.common.truth.Truth.assertThat +import org.gradle.api.GradleException +import org.junit.jupiter.api.Test +import java.lang.reflect.Proxy + +/** + * A variant type the helper does not know cannot be given the SQLite runtime dependencies, and a + * build that went on would crash on the device (NoClassDefFoundError). A TestKit build cannot produce + * such a variant, so this tests the decision function with a stand-in variant. + */ +class RuntimeConfigurationTest { + private val unknownVariant = + Proxy.newProxyInstance( + javaClass.classLoader, + arrayOf(ApplicationVariant::class.java), + ) { _, method, _ -> + if (method.name == "getName") "stagingDebug" else null + } as ApplicationVariant + + @Test + fun `requireRuntimeConfiguration fails naming the variant and its class`() { + var ran = false + val failure = runCatching { unknownVariant.requireRuntimeConfiguration { ran = true } }.exceptionOrNull() + + assertThat(failure).isInstanceOf(GradleException::class.java) + assertThat(failure!!.message).contains("variant 'stagingDebug'") + assertThat(failure.message).contains(unknownVariant.javaClass.name) + assertThat(ran).isFalse() + } + + @Test + fun `withRuntimeConfiguration still skips an unknown variant for LogSender`() { + var ran = false + unknownVariant.withRuntimeConfiguration { ran = true } + + assertThat(ran).isFalse() + assertThat(unknownVariant.runtimeConfigurationOrNull()).isNull() + } +} From 627aa94c61882f735a0a739e1eb30d53dbbe07c4 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:45:36 -0800 Subject: [PATCH 11/12] ADFA-7138: Say how to turn the SQLite debugger on in an app Adds a how-to for existing projects to the debugger's README (ADFA-7138, item 5): the gradle.properties line, requery's AAR in libs/, the androidx libraries requery needs, and what to expect in a debug build. It says what the line changes (every variant moves to requery, Quick Build is off), that commenting it out turns it off, and that Room's BundledSQLiteDriver is not covered. The details stay in gradle-plugin/docs/sqlite-rewrite.md, which it links to. The template line itself is in dev-assets, on sqlite-debugger/ADFA-7138, stacked on dev-assets#79. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- subprojects/sqlite-debugger/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 08f92e3e6d..36a5d61128 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -4,6 +4,19 @@ A debug-only SQLite debugger and MCP server that runs inside apps built with Cod The module builds an AAR with `libsqlitedbg.so` for arm64-v8a and armeabi-v7a in one artifact, so it has no v7/v8 flavors (`AndroidModuleConf`). Its minSdk is 21, requery's, because the debugger runs on requery's SQLite. The unit test task is `:subprojects:sqlite-debugger:testDebugUnitTest`. +## Turning it on in an app + +A project made from the JavaScript in SQLite template has it on already. For any other project: + +1. Add `cogo.sqliteDebugger=true` to the project's `gradle.properties` ([where else it can go](../../gradle-plugin/docs/sqlite-rewrite.md#turning-it-on)). +2. Copy `app/libs/sqlite-android.aar` (requery) from a project made from that template into the app's `libs/`, and add `implementation(files("libs/sqlite-android.aar"))`. +3. Check that the app depends on androidx.sqlite (Room and WorkManager bring it) and androidx.core (AppCompat brings it). requery needs both, and a file dependency brings neither. +4. Build and run the debug variant. The app starts the server and sends its port and token to Code on the Go (ADFA-7137), and AI-Agent-MCP connects to it with no setup (ADFA-7139). In phase 1 the Agent asks before each call (ADFA-7147). + +The app's code does not change: the build moves the app and its libraries from Android's SQLite to requery in every variant, release included, so debug and release run the same SQLite. Release builds never contain the debugger. While the line is on, Quick Build is off for the project. Commenting the line out turns all of this off. + +Not covered: Room's `BundledSQLiteDriver`, which brings its own SQLite. When the app or one of its libraries uses something requery lacks, the build fails and names the class, method and line; the list is under [Rules](../../gradle-plugin/docs/sqlite-rewrite.md#rules). + ## How it fits together ``` From 2394a80e8dada7c41246c2263e5672862b4b7f44 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:59:54 -0800 Subject: [PATCH 12/12] ADFA-7138: Say how to build when the rewrite fails on a library class The rewrite fails the build on a class that uses something requery lacks, naming class, method and line. For the app's own code that is enough; for a library class the user cannot change, the message gave no way out. It now ends with one: comment out cogo.sqliteDebugger=true in gradle.properties to build without the SQLite debugger. The transform cannot tell an app class from a library class, so the line covers both. The fail rule itself stays (decided 2026-11-16): the rewrite runs in release builds too, so a warning would let a released app ship a call that crashes when it runs. The new test fails without the change (its "in a library" check); the rewrite tests pass with it. The rewrite doc and the debugger README say the message now carries the way out. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 3 ++- .../gradle/sqlite/SqliteRewriter.kt | 8 ++++++- .../gradle/sqlite/SqliteRewriterTest.kt | 24 +++++++++++++++++++ subprojects/sqlite-debugger/README.md | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 2472d1d548..92c4da1c2d 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -69,7 +69,8 @@ cannot load, so the transform runs only on projects that opted in. Commenting th (requery's cursors extend its own copies of the first two, and implement only `android.database.Cursor`). A cast guarded by `instanceof` of the same type on the same local (`if (c instanceof X) ((X) c)`, `&&`, `?:`, or an early return when it is false) is not reported. `COLLATE LOCALIZED` only logs a warning: it works, but - compares bytes (no ICU) instead of sorting by locale. All violations of a class are reported together. A platform method outside the renamed set whose + compares bytes (no ICU) instead of sorting by locale. All violations of a class are reported together, with + the way out for a library class, which the user cannot change: comment out `cogo.sqliteDebugger=true`. A platform method outside the renamed set whose descriptor mentions a renamed class (found with javap over android.jar: the other `DatabaseUtils` methods `blobFileDescriptorForQuery` and `bindObjectToProgram`, `DatabaseUtils$InsertHelper`, `OpenParams` accessors) cannot link once the descriptor is renamed, so a call to one that is not redirected above fails diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt index f356a6e93f..18553c7e58 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriter.kt @@ -75,6 +75,11 @@ enum class RewritePolicy( } object SqliteRewriter { + /** The transform cannot tell an app class from a library class, so the message covers both. */ + private const val WAY_OUT = + "If this class is the app's, change the code above. If it is in a library, the library cannot run on " + + "requery: comment out cogo.sqliteDebugger=true in gradle.properties to build without the SQLite debugger." + private val remapper = object : Remapper() { override fun map(internalName: String): String = Rules.RENAMES[internalName] ?: internalName @@ -158,7 +163,8 @@ object SqliteRewriter { if (violations.isNotEmpty()) { throw SqliteRewriteException( "Code on the Go cannot move '${cn.name.replace('/', '.')}' from android.database.sqlite to " + - "requery (cogo.sqliteDebugger=true):\n" + violations.joinToString("\n") { " - $it" }, + "requery (cogo.sqliteDebugger=true):\n" + violations.joinToString("\n") { " - $it" } + + "\n" + WAY_OUT, ) } } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt index 36bfbf40fd..70e7e6e7b2 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriterTest.kt @@ -415,6 +415,30 @@ class SqliteRewriterTest { assertThat(message).contains("SQLiteQueryBuilder.setStrictGrammar") } + @Test + fun `the failure says how to build when the class is in a library`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Lib", + """ + package p; + public class Lib { + void one(android.database.sqlite.SQLiteDatabase db) { + db.execPerConnectionSQL("x", null); + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Lib"]!!, hierarchy) } + val lastLine = e.message!!.lines().last() + assertThat(lastLine).contains("in a library") + assertThat(lastLine).contains("comment out cogo.sqliteDebugger=true in gradle.properties") + } + @Test fun `a missing member is found through every renamed ancestor of the owner`() { val classes = diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 36a5d61128..a23365bc11 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -15,7 +15,7 @@ A project made from the JavaScript in SQLite template has it on already. For any The app's code does not change: the build moves the app and its libraries from Android's SQLite to requery in every variant, release included, so debug and release run the same SQLite. Release builds never contain the debugger. While the line is on, Quick Build is off for the project. Commenting the line out turns all of this off. -Not covered: Room's `BundledSQLiteDriver`, which brings its own SQLite. When the app or one of its libraries uses something requery lacks, the build fails and names the class, method and line; the list is under [Rules](../../gradle-plugin/docs/sqlite-rewrite.md#rules). +Not covered: Room's `BundledSQLiteDriver`, which brings its own SQLite. When the app or one of its libraries uses something requery lacks, the build fails, names the class, method and line, and says how to build without the debugger; the list is under [Rules](../../gradle-plugin/docs/sqlite-rewrite.md#rules). ## How it fits together