From e6b2e2632671812ac41b2b9b725f80bce04c04e4 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 21:36:24 -0800 Subject: [PATCH 01/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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/56] 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 From efc07f76ed980228cdb064726999a00d41bf705a Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:27:47 -0800 Subject: [PATCH 13/56] ADFA-7138: Stub the module directory in the Quick Build provisioner fakes 2c78e0af9 made the opt-in check read each app module's projectDirPath, but the shared test double androidModule() did not stub it, so mockk threw, runProxyAppBuild caught it as a build failure, and 21 of the 43 GradleQuickBuildProvisioner* tests failed with the generic failure (FailureArms 9, Signing 7, Baseline 3, BuildDir 2). None of the PR's checks runs :app's unit tests. androidModule() now takes a projectDir (default /fake-project/app), and testProvisioner's default module sits under the test's project root. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisionerFakes.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt index b46b121d99..fd4f00882d 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt @@ -144,15 +144,19 @@ internal fun unsyncedProjectManager(projectRoot: File): FakeProjectManager = Fak * @param variantName the selected variant, or null for the mid-sync read the provisioner * falls back to the default variant on. * @param buildDir the module's build directory as the synced model reports it. + * @param projectDir the module's directory, where the SQLite debugger opt-in check reads its + * `gradle.properties`. */ internal fun androidModule( path: String = ":app", variantName: String? = "debug", buildDir: File = File("/fake-project/app/build"), + projectDir: File = File("/fake-project/app"), ): AndroidModule = mockk().also { module -> every { module.path } returns path every { module.buildDirPath } returns buildDir.path + every { module.projectDirPath } returns projectDir.path every { module.getSelectedVariant() } returns variantName?.let { AndroidModels.AndroidVariant @@ -257,7 +261,10 @@ internal fun testProvisioner( projectRoot: File, buildService: BuildService? = FakeBuildService(), projectManager: IProjectManager = - FakeProjectManager(projectRoot, appModules = listOf(androidModule(buildDir = File(projectRoot, "app/build")))), + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(projectRoot, "app/build"), projectDir = File(projectRoot, "app"))), + ), installer: ProxyAppInstaller = recordingInstaller(), packages: InstalledPackages = FakePackages(), apkCertSha256: (File) -> String? = { null }, From 1407eae8c09ddac08c0c135a5d0d35937ea3a099 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:27:57 -0800 Subject: [PATCH 14/56] ADFA-7138: Check a module's SQLite debugger opt-in after the sync too The Quick Build opt-in check ran before awaitProjectModel. While the Gradle sync has not published the model, getAndroidAppModules() is empty, so a tap during the sync read only the user home's and the root's gradle.properties. A project with cogo.sqliteDebugger=true in its app module's own file was staged and built, and ended in "variant setup missing". The check still runs first, so a root or user home opt-in is refused at once as before. When the model was not ready then, it runs again after the wait, with the modules. In that case staging (into Code on the Go's own quickbuild directory, not the project) has already run, which a refused build can afford. A model that is ready at the tap sees no change. Test: GradleQuickBuildProvisionerFailureArmsTest, an opt-in in the app module's gradle.properties with the modules published during staging is refused with quick_build_disabled_sqlite_debugger and runs no build. Without the fix it gets quick_build_variant_setup_missing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 49 +++++++++++++------ ...dleQuickBuildProvisionerFailureArmsTest.kt | 29 +++++++++++ .../GradleQuickBuildProvisionerFakes.kt | 17 +++++++ 3 files changed, 79 insertions(+), 16 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 28bc92cec4..635fa875ed 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -344,22 +344,12 @@ class GradleQuickBuildProvisioner( 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, - Environment.GRADLE_USER_HOME, - projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, - ), - )?.let { refusal -> - log.info( - "Quick Build is not provisioned: {} is true in the project's or the Gradle user home's gradle.properties", - GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, - ) - return ProxyAppBuildResult.Failed(context.getString(refusal)) - } + // Before staging, which a refused build need not pay for: the Gradle plugin never applies Quick + // Build to an opted-in project, so building would only end in "variant setup missing". During + // a sync the model has no app modules yet, so an opt-in in a module's own gradle.properties is + // seen only by the second check, after the wait for the model. + val modulesKnown = projectManager.workspace != null + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } stage(context, paths) // The project model only exists once CoGo's Gradle sync has populated it, and a tap @@ -372,6 +362,9 @@ class GradleQuickBuildProvisioner( context.getString(R.string.quick_build_waiting_for_sync), ) } + if (!modulesKnown) { + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } + } val module = quickBuildModule(projectManager) @@ -543,6 +536,30 @@ class GradleQuickBuildProvisioner( } } + /** + * The refusal for a project with `cogo.sqliteDebugger=true`, or null. It reads the app modules' + * own `gradle.properties` only for the modules [projectManager] knows, which is none before the + * sync publishes the model. + */ + private fun sqliteDebuggerRefusal( + projectManager: IProjectManager, + projectRoot: File, + ): ProxyAppBuildResult.Failed? { + val refusal = + QuickBuildProjectSupport.sqliteDebuggerMessage( + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + projectRoot, + Environment.GRADLE_USER_HOME, + projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, + ), + ) ?: return null + log.info( + "Quick Build is not provisioned: {} is true in the Gradle user home's, an app module's or the project's gradle.properties", + GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, + ) + return ProxyAppBuildResult.Failed(context.getString(refusal)) + } + /** `:app` -> `/app`; nested paths (`:feature:home`) map to nested dirs. */ private fun moduleDir( projectRoot: File, 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 dbb526cced..01913f067d 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -89,6 +89,35 @@ class GradleQuickBuildProvisionerFailureArmsTest { assertThat(gradle.executedTasks).isNull() } + @Test + fun `an opt-in in the app module's gradle properties is refused when the tap comes during the sync`() = + runTest { + val moduleDir = moduleDir() + File(moduleDir, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val gradle = FakeBuildService() + val project = + SyncingProjectManager( + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(moduleDir, "build"), projectDir = moduleDir)), + ), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + // The sync publishes the modules while Quick Build stages, so only a check made after + // the wait for the model can read the module's gradle.properties. + stage = { _, _ -> project.published = true }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + 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/GradleQuickBuildProvisionerFakes.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt index fd4f00882d..eb6e4c9022 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt @@ -133,6 +133,23 @@ internal class FakeProjectManager( */ internal fun unsyncedProjectManager(projectRoot: File): FakeProjectManager = FakeProjectManager(projectRoot, workspace = null) +/** + * A project model that has no workspace and no modules until [published] is set, when it answers + * as [synced] does: a tap during the sync, and the sync finishing while Quick Build works. + */ +internal class SyncingProjectManager( + private val synced: FakeProjectManager, +) : IProjectManager by synced { + var published = false + + override val workspace: Workspace? + get() = if (published) synced.workspace else null + + override fun getAndroidModules(): List = if (published) synced.getAndroidModules() else emptyList() + + override fun getAndroidAppModules(): List = if (published) synced.getAndroidAppModules() else emptyList() +} + /** * An Android module as the Build Variants sidebar presents it. * From 7a227633b96164f52b59c6e91bfd8cb043d33f50 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:27:57 -0800 Subject: [PATCH 15/56] ADFA-7138: Require androidx.core in the rewrite's classpath check checkSqliteRewriteClasspath required requery's and androidx.sqlite's classes but not androidx.core's, which requery 3.49.0 needs too. jdeps and javap over the classes.jar of the template's sqlite-android.aar: 12 requery classes reference androidx.core.os.CancellationSignal; SQLiteSession calls its throwIfCanceled, SQLiteConnection and SQLiteConnectionPool call setOnCancelListener, SQLiteDatabase.query(SupportSQLiteQuery, android.os.CancellationSignal) constructs one, and SQLiteConnectionPool constructs androidx.core.os.OperationCanceledException. The rewrite's own SqliteRewriteRuntime.toCancellationSignal constructs one for every converted signal. An app with androidx.sqlite but without androidx.core passed the check and fails with NoClassDefFoundError on the first query that passes a cancellation signal. The check now also requires androidx/core/os/CancellationSignal.class (present in androidx.core 1.9.0 through 1.16.0), and the message says how to add androidx.core. The rewrite doc lists what requery takes from each library, including androidx.collection's LruCache, which requery's statement cache extends and which androidx.core brings. Tests: SqliteRewritePluginTest, a new case excludes androidx.core and expects its entry and coordinates in the message; the requery and androidx.sqlite cases assert the real androidx.core AAR satisfies the check; the passing case uses a stand-in jar for androidx.core too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 13 ++++--- .../sqlite/CheckSqliteRewriteClasspathTask.kt | 13 +++++-- .../gradle/SqliteRewritePluginTest.kt | 38 +++++++++++++++++-- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 92c4da1c2d..ff317cac29 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -16,10 +16,12 @@ An existing project needs: 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 - 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. +3. androidx.sqlite and androidx.core on the runtime classpath (`androidx.sqlite:sqlite`, which Room, WorkManager + and most androidx libraries bring, and `androidx.core:core`, which AppCompat brings). A file dependency brings + none of the dependencies of the AAR: requery implements androidx.sqlite's interfaces, takes and calls + androidx.core's `CancellationSignal` (the rewrite converts android's signal to it) and throws its + `OperationCanceledException`, and its statement cache extends androidx.collection's `LruCache`, which + androidx.core brings. 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 @@ -31,7 +33,8 @@ the rewrite is already registered for it, so going on would give an app that thr 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`. +`io/requery/android/database/sqlite/SQLiteDatabase.class`, `androidx/sqlite/db/SupportSQLiteDatabase.class` or +`androidx/core/os/CancellationSignal.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. 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 index 39422df3fb..38ed312638 100644 --- 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 @@ -14,8 +14,10 @@ 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. + * lacks what the rewritten classes load: requery itself, androidx.sqlite (whose interfaces requery + * implements) and androidx.core (whose CancellationSignal requery's API takes, and the rewrite + * converts android's signal to). requery reaches an app as a file dependency from `libs/`, which + * brings none of its own dependencies. */ abstract class CheckSqliteRewriteClasspathTask : DefaultTask() { @get:Input @@ -54,11 +56,13 @@ abstract class CheckSqliteRewriteClasspathTask : DefaultTask() { 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" + internal const val ANDROIDX_CORE_ENTRY = "${SqliteRewriteRules.CORE_SIGNAL}.class" private val REQUIRED = listOf( "requery's SQLiteDatabase ($REQUERY_ENTRY)" to REQUERY_ENTRY, "androidx.sqlite's SupportSQLiteDatabase ($ANDROIDX_SQLITE_ENTRY)" to ANDROIDX_SQLITE_ENTRY, + "androidx.core's CancellationSignal ($ANDROIDX_CORE_ENTRY)" to ANDROIDX_CORE_ENTRY, ) internal fun message( @@ -67,8 +71,9 @@ abstract class CheckSqliteRewriteClasspathTask : DefaultTask() { ) = "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). " + + "dependencies: add requery's AAR to libs/ (implementation(files(\"libs/sqlite-android.aar\"))), androidx.sqlite " + + "(implementation(\"androidx.sqlite:sqlite:\"), or a library that depends on it, such as Room or WorkManager) " + + "and androidx.core (implementation(\"androidx.core:core:\"), or a library that depends on it, such as AppCompat). " + "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. */ 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 index 03924efb01..93514157a2 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt @@ -15,14 +15,15 @@ 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. + * project that opted in, and a per-variant task checks that requery, androidx.sqlite and androidx.core 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 val androidxCoreEntry = CheckSqliteRewriteClasspathTask.ANDROIDX_CORE_ENTRY private fun build( dir: File, @@ -119,7 +120,9 @@ class SqliteRewritePluginTest { assertThat(message).contains("variant 'demoDebug'") assertThat(message).contains(requeryEntry) + // the sample project's Room and AppCompat bring the real AARs, which the check must find assertThat(message).doesNotContain(androidxSqliteEntry) + assertThat(message).doesNotContain(androidxCoreEntry) assertThat(message).contains("file dependency") assertThat(message).contains("brings none of its own dependencies") } @@ -141,22 +144,49 @@ class SqliteRewritePluginTest { assertThat(message).contains(androidxSqliteEntry) assertThat(message).doesNotContain("requery's SQLiteDatabase") + assertThat(message).doesNotContain(androidxCoreEntry) } @Test - fun `the check passes with stand-in jars that contain the two class entries, and is reused from the configuration cache`( + fun `the check fails without androidx core, whose CancellationSignal requery's API takes`( + @TempDir dir: File, + ) { + val requery = jar(dir, "requery.jar", requeryEntry) + val project = + project( + dir, + """ + configurations.all { exclude(group = "androidx.core") } + dependencies { implementation(files("${requery.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + val message = failureOf { build(dir, ":app:checkDemoDebugSqliteRewriteClasspath", project = project) } + + assertThat(message).contains(androidxCoreEntry) + assertThat(message).contains("androidx.core:core") + assertThat(message).doesNotContain("requery's SQLiteDatabase") + assertThat(message).doesNotContain(androidxSqliteEntry) + } + + @Test + fun `the check passes with stand-in jars that contain the three 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 core = jar(dir, "core.jar", androidxCoreEntry) val project = project( dir, """ - configurations.all { exclude(group = "androidx.sqlite") } + configurations.all { + exclude(group = "androidx.sqlite") + exclude(group = "androidx.core") + } dependencies { implementation(files("${requery.invariantSeparatorsPath}")) implementation(files("${sqlite.invariantSeparatorsPath}")) + implementation(files("${core.invariantSeparatorsPath}")) } """.trimIndent(), ) From 157387859673e06c062c66de94475fb2f221ecf8 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:27:57 -0800 Subject: [PATCH 16/56] ADFA-7138: Make every missing-member entry name its descriptors MISSING_MEMBERS' builder no() defaulted desc to "", and its KDoc promised an empty set for "every descriptor". The two readers disagreed on that sentinel: SqliteRewriter.decide matched an empty set, fieldProblem and MISSING_FIELD_NAMES matched "". A method entry written without a descriptor would have been stored as {""} and matched no call, so the unsupported call would have passed the rewrite and thrown NoSuchMethodError on the device. No entry used the default, so nothing misbehaved. The descriptor is now required, and the "every descriptor" meaning is gone: decide and fieldProblem match the listed descriptors only, and SqliteRewriteResolutionTest no longer treats a trailing ":" as a wildcard. Requiring it is clearer than an empty-set sentinel in a builder that adds to a set, where a member listed once without and once with a descriptor would end up narrowed to the one. Test: SqliteRewriteRulesTest checks that every entry lists at least one descriptor and that each parses as a JVM descriptor (no blank). It cannot fail on the old code, whose entries all had descriptors, and a call without one no longer compiles; with the old default and one such call added, it fails on that entry's '' descriptor. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/sqlite/SqliteRewriteRules.kt | 8 ++++---- .../gradle/sqlite/SqliteRewriter.kt | 4 ++-- .../sqlite/SqliteRewriteResolutionTest.kt | 3 +-- .../gradle/sqlite/SqliteRewriteRulesTest.kt | 20 +++++++++++++++++++ 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt 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 index 56281d05e7..b238f290b9 100644 --- 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 @@ -83,15 +83,15 @@ internal object SqliteRewriteRules { ) /** - * Members requery lacks, or has with another meaning. Key: "owner.name" -> descriptors, or an - * empty set for every descriptor (fields and the like). + * Members requery lacks, or has with another meaning. Key: "owner.name" -> the descriptors it lacks; + * other overloads are not matched, so every entry names its descriptors. */ val MISSING_MEMBERS: Map> = buildMap> { fun no( owner: String, name: String, - desc: String = "", + desc: String, ) { getOrPut("$SQLITE_PKG$owner.$name") { mutableSetOf() }.add(desc) } @@ -174,7 +174,7 @@ internal object SqliteRewriteRules { .filter { it != "" } .toSet() - /** Names of the fields the missing-member rule can match: entries for every descriptor, or with a field descriptor. */ + /** Names of the fields the missing-member rule can match: entries with a field descriptor. */ val MISSING_FIELD_NAMES: Set = MISSING_MEMBERS .filterValues { descs -> descs.any { !it.startsWith("(") } } 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 18553c7e58..96628ae14b 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 @@ -383,7 +383,7 @@ object SqliteRewriter { // 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 } + .firstOrNull { Rules.MISSING_MEMBERS["$it.$name"]?.contains(desc) == true } ?.let { "${simple(it)}.$name does not exist in requery" } } @@ -483,7 +483,7 @@ object SqliteRewriter { return Action.None } for (c in ancestors) { - if (Rules.MISSING_MEMBERS["$c.$name"]?.let { it.isEmpty() || desc in it } == true) { + if (Rules.MISSING_MEMBERS["$c.$name"]?.contains(desc) == true) { return Action.Fail("${simple(c)}.$name$desc does not exist in requery") } } 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 index 0661826724..01bab6fa4a 100644 --- 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 @@ -178,8 +178,7 @@ class SqliteRewriteResolutionTest { } val unexpected = failed.filterNot { f -> - expected.contains(f) || expected.any { it.endsWith(":") && f.startsWith(it) } || - f.contains("OpenParams") || f.contains("android/database/CursorWindow") + expected.contains(f) || f.contains("OpenParams") || f.contains("android/database/CursorWindow") } assertThat(unexpected).isEmpty() } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt new file mode 100644 index 0000000000..e9e416aa6f --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt @@ -0,0 +1,20 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertWithMessage +import org.junit.jupiter.api.Test +import org.objectweb.asm.Type + +class SqliteRewriteRulesTest { + @Test + fun `every missing member lists the exact descriptors requery lacks, each a well-formed descriptor`() { + // The rewriter fails a call or a field access only when its descriptor is in the entry's set, so an + // entry with no descriptor, or a blank one, would match nothing. + for ((member, descs) in SqliteRewriteRules.MISSING_MEMBERS) { + assertWithMessage(member).that(descs).isNotEmpty() + for (desc in descs) { + val parsed = runCatching { if (desc.startsWith("(")) Type.getMethodType(desc) else Type.getType(desc) }.getOrNull() + assertWithMessage("$member descriptor '$desc'").that(parsed?.descriptor).isEqualTo(desc) + } + } + } +} From acab92691cb896f9f768d06e29d7b6eca2f2e61e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 23:35:51 -0800 Subject: [PATCH 17/56] ADFA-7138: Quick Build status line says "off", not "setup failed", for the SQLite debugger refusal With cogo.sqliteDebugger=true, Quick Build refuses on purpose, but the refusal went through the failed-start path, so the status line read "Quick Build: setup failed - tap to retry" although nothing failed and a retry cannot help. The provisioner now reports the refusal as its own message (QuickBuildMessage.DisabledBySqliteDebugger), the reducer records it as startRefused beside lastStartFailed, and the status line, Build Output and flashes treat it as a separate StartRefused transition. The status line reads "Quick Build: off while the SQLite debugger is on - tap for details"; a tap re-runs the cheap check and shows the full refusal again. Other failure paths are unchanged. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 34 +++++++++++++------ .../quickbuild/QuickBuildFlashes.kt | 1 + .../quickbuild/QuickBuildMessages.kt | 4 +++ .../quickbuild/QuickBuildOutputLines.kt | 6 ++++ .../quickbuild/QuickBuildStatusBar.kt | 6 ++++ .../quickbuild/QuickBuildTransitions.kt | 11 +++++- ...dleQuickBuildProvisionerFailureArmsTest.kt | 15 ++++++++ .../quickbuild/QuickBuildStatusBarTest.kt | 29 ++++++++++++++++ .../domain/session/QuickBuildMessage.kt | 7 ++++ .../domain/session/QuickBuildSessionState.kt | 7 ++++ .../domain/session/QuickBuildStatus.kt | 7 ++-- .../domain/session/SessionReducer.kt | 24 ++++++++++--- resources/src/main/res/values/strings.xml | 1 + 13 files changed, 134 insertions(+), 18 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 635fa875ed..0d2eda903a 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -112,6 +112,10 @@ class GradleQuickBuildProvisioner( built } + ProxyAppBuildResult.Refused -> { + return ProvisionOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) + } + is ProxyAppBuildResult.Failed -> { return ProvisionOutcome.Failure( built.message?.let(QuickBuildMessage::Literal) @@ -202,6 +206,10 @@ class GradleQuickBuildProvisioner( return ProxyAppRebuildOutcome.BuildSlotBusy } + ProxyAppBuildResult.Refused -> { + return ProxyAppRebuildOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) + } + is ProxyAppBuildResult.Failed -> { return ProxyAppRebuildOutcome.Failure( built.message?.let(QuickBuildMessage::Literal) @@ -313,6 +321,13 @@ class GradleQuickBuildProvisioner( /** Another Gradle build owns the single slot, so nothing ran. */ data object SlotBusy : ProxyAppBuildResult + /** + * Quick Build is deliberately off for this project (the SQLite debugger is on), so nothing + * ran and nothing failed. Split from [Failed] so the session can say so rather than offer a + * retry. + */ + data object Refused : ProxyAppBuildResult + /** * [message] replaces the caller's generic wording when the cause is one the user can * act on. Null keeps the generic "proxy app build failed" for genuine build failures. @@ -544,20 +559,19 @@ class GradleQuickBuildProvisioner( private fun sqliteDebuggerRefusal( projectManager: IProjectManager, projectRoot: File, - ): ProxyAppBuildResult.Failed? { - val refusal = - QuickBuildProjectSupport.sqliteDebuggerMessage( - QuickBuildProjectSupport.isSqliteDebuggerEnabled( - projectRoot, - Environment.GRADLE_USER_HOME, - projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, - ), - ) ?: return null + ): ProxyAppBuildResult.Refused? { + QuickBuildProjectSupport.sqliteDebuggerMessage( + QuickBuildProjectSupport.isSqliteDebuggerEnabled( + projectRoot, + Environment.GRADLE_USER_HOME, + projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, + ), + ) ?: return null log.info( "Quick Build is not provisioned: {} is true in the Gradle user home's, an app module's or the project's gradle.properties", GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, ) - return ProxyAppBuildResult.Failed(context.getString(refusal)) + return ProxyAppBuildResult.Refused } /** `:app` -> `/app`; nested paths (`:feature:home`) map to nested dirs. */ diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt index 30294662df..1b0167691c 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildFlashes.kt @@ -77,6 +77,7 @@ class QuickBuildFlashes { // nothing here either. QuickBuildTransition.SessionStopped, QuickBuildTransition.StartFailed, + QuickBuildTransition.StartRefused, -> { flashedFailure = null null diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt index 6f5b065d98..94e43e1469 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildMessages.kt @@ -78,6 +78,10 @@ fun QuickBuildMessage.resolve(context: Context): String = context.getString(R.string.quick_build_scratch_dir_unavailable, path) } + QuickBuildMessage.DisabledBySqliteDebugger -> { + context.getString(R.string.quick_build_disabled_sqlite_debugger) + } + QuickBuildMessage.DaemonRejectedConfiguration -> { context.getString(R.string.quick_build_daemon_rejected_config) } diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt index 3b8d9ef50a..4cf21345b6 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildOutputLines.kt @@ -48,6 +48,12 @@ fun quickBuildOutputLines( listOf("could not start - tap Quick Build to retry.") } + // The refusal itself went to the flash and the log line that made it; a refusal is not a + // failure, so this only says what the tap does. + QuickBuildTransition.StartRefused -> { + listOf("off while the SQLite debugger is on - tap Quick Build to see why.") + } + is QuickBuildTransition.ProvisioningStarted -> { when (val kind = transition.kind) { is ProvisioningKind.Rebaseline -> { diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt index 47f9f10694..fc75a5546a 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt @@ -88,6 +88,12 @@ fun quickBuildStatusBarUpdate( QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed) } + QuickBuildTransition.StartRefused -> { + // Not a failure: Quick Build is deliberately off here, and a retry cannot help. The tap + // shows the full refusal again. + QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger) + } + is QuickBuildTransition.ProvisioningStarted -> { when (transition.kind) { is ProvisioningKind.Rebaseline -> { diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt index 289192acbc..e24a80b995 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildTransitions.kt @@ -30,6 +30,13 @@ internal sealed interface QuickBuildTransition { */ data object StartFailed : QuickBuildTransition + /** + * A session start was declined on purpose: Quick Build is off for this project while the + * SQLite debugger is on. Nothing failed and retrying cannot help, so surfaces must not say + * "failed" or offer a retry; the tap shows the refusal again. + */ + data object StartRefused : QuickBuildTransition + /** * A full Gradle build started. * @@ -132,7 +139,9 @@ internal fun quickBuildTransition( } return when (current) { is QuickBuildStatus.Hidden -> { - if (current.lastStartFailed) { + if (current.lastStartFailed && current.startRefused) { + QuickBuildTransition.StartRefused + } else if (current.lastStartFailed) { QuickBuildTransition.StartFailed } else { QuickBuildTransition.SessionStopped 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 01913f067d..d7b1655fcc 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -216,6 +216,21 @@ class GradleQuickBuildProvisionerFailureArmsTest { .isEqualTo(context.getString(R.string.quick_build_variant_setup_missing, "debug")) } + @Test + fun `a project with the SQLite debugger on is refused as an off state, not a setup failure`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val gradle = FakeBuildService(result = TaskExecutionResult(true, null)) + + val outcome = testProvisioner(context, projectRoot, buildService = gradle).provision() + + // Its own message, so the session can tell this deliberate refusal from a fault. + assertThat(outcome).isEqualTo(ProvisionOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger)) + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(failureText(outcome)).isNotEqualTo(context.getString(R.string.quick_build_setup_failed)) + assertThat(gradle.executedTasks).isNull() + } + @Test fun `a Gradle build that failed falls back to the generic setup-failure text`() = runTest { diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt index 1bd4a2acc3..bc5952fc6a 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat import com.itsaky.androidide.resources.R import org.appdevforall.cotg.quickbuild.domain.classify.InvalidationReason import org.appdevforall.cotg.quickbuild.domain.reload.BuildDiagnostic +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildMessage import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildSessionState import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildStatus import org.appdevforall.cotg.quickbuild.domain.session.SessionEvent @@ -278,6 +279,34 @@ class QuickBuildStatusBarTest { .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed)) } + @Test + fun `a refused start says Quick Build is off, not that setup failed, and a real failure still does`() { + val reducer = SessionReducer() + val refused = + reducer + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.DisabledBySqliteDebugger), + ).state + val failed = + reducer + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.RebuildFailed), + ).state + + assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(refused))) + .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger)) + // A recreation resubscribes with no previous status and must read the same. + assertThat(update(null, QuickBuildStatus.from(refused))) + .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger)) + assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(failed))) + .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed)) + // A save clears the carried refusal as it does a failure. + assertThat(QuickBuildStatus.from(reducer.reduce(refused, SessionEvent.FileSaved).state)) + .isEqualTo(QuickBuildStatus.Hidden()) + } + @Test fun `closing the project ends the failed-start story, so the next one keeps its own line`() { // The bar cannot tell a recreation - where the failed-start line must come back, pinned diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt index b6f9f2774e..9192c8e5ff 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildMessage.kt @@ -20,6 +20,13 @@ sealed interface QuickBuildMessage { val text: String, ) : QuickBuildMessage + /** + * Quick Build is deliberately off for this project because the SQLite debugger is on + * (`cogo.sqliteDebugger=true`). Nothing failed and a retry cannot help, so the session records + * it apart from a failed start ([QuickBuildSessionState.Idle.startRefused]). + */ + data object DisabledBySqliteDebugger : QuickBuildMessage + /** * The OS wants the reinstall confirmed but no dialog could be shown, because CoGo was not * in the foreground to host it. Returning to CoGo is what re-prompts. diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt index 522e896738..bd45278231 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildSessionState.kt @@ -19,9 +19,14 @@ sealed interface QuickBuildSessionState { * failure flash. Cleared by the next tap (which starts a fresh provision anyway) or the next * save ([SessionEvent.FileSaved]) - the save clears the tone only and never retries the * start; a retry stays a tap. + * @property startRefused the failed start was a deliberate refusal + * ([QuickBuildMessage.DisabledBySqliteDebugger]) rather than a fault, so the status line says + * Quick Build is off instead of offering a retry that cannot help. Only meaningful with + * [lastStartFailed]; cleared by the same gestures. */ data class Idle( val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildSessionState /** @@ -33,10 +38,12 @@ sealed interface QuickBuildSessionState { * @property lastStartFailed carried from [Idle.lastStartFailed] so the silent warm build does * not clear the failed-start error tone on its way through; a queued tap clears it, and a * tapless finish hands it back to [Idle]. + * @property startRefused carried from [Idle.startRefused] alongside [lastStartFailed]. */ data class Prebuilding( val tapQueued: Boolean = false, val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildSessionState /** diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt index 78480dcd52..c53d6b3723 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/QuickBuildStatus.kt @@ -19,9 +19,12 @@ sealed interface QuickBuildStatus { * ([QuickBuildSessionState.Idle.lastStartFailed]), so the bolt keeps the error tone until * the next tap or save; carried here because a failed start rests in Hidden and the tone * is derived from status alone. + * @property startRefused that failed start was Quick Build deliberately declining the project + * ([QuickBuildSessionState.Idle.startRefused]), not a fault; surfaces word it differently. */ data class Hidden( val lastStartFailed: Boolean = false, + val startRefused: Boolean = false, ) : QuickBuildStatus /** @@ -120,7 +123,7 @@ sealed interface QuickBuildStatus { fun from(state: QuickBuildSessionState): QuickBuildStatus = when (state) { is QuickBuildSessionState.Idle -> { - Hidden(state.lastStartFailed) + Hidden(state.lastStartFailed, state.startRefused) } // A warm-up the user never asked for stays invisible - but it must not clear a @@ -128,7 +131,7 @@ sealed interface QuickBuildStatus { is QuickBuildSessionState.Prebuilding -> { // A warm build has no baseline to replace, so a tap that queues on one is // always a session's first provision. - if (state.tapQueued) Provisioning() else Hidden(state.lastStartFailed) + if (state.tapQueued) Provisioning() else Hidden(state.lastStartFailed, state.startRefused) } is QuickBuildSessionState.Provisioning -> { diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt index 04870a25ab..59fcf0a523 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/domain/session/SessionReducer.kt @@ -99,7 +99,10 @@ class SessionReducer { // The flag rides along so the silent warm build cannot clear a failed-start // tone: only a tap or a save is a user gesture. SessionTransition( - QuickBuildSessionState.Prebuilding(lastStartFailed = state.lastStartFailed), + QuickBuildSessionState.Prebuilding( + lastStartFailed = state.lastStartFailed, + startRefused = state.startRefused, + ), listOf(SessionEffect.StartProxyAppPrebuild), ) } @@ -164,13 +167,16 @@ class SessionReducer { // the tooling server); it queues and fires on PrebuildFinished. The tap is also // the retry gesture, so it clears a carried failed-start tone. is SessionEvent.QuickBuildTapped -> { - SessionTransition(state.copy(tapQueued = true, lastStartFailed = false), listOf(SessionEffect.RecordAsk)) + SessionTransition( + state.copy(tapQueued = true, lastStartFailed = false, startRefused = false), + listOf(SessionEffect.RecordAsk), + ) } SessionEvent.FileSaved -> { // Same clearing gesture as in Idle; the warm build itself is not one. if (state.lastStartFailed) { - SessionTransition(state.copy(lastStartFailed = false)) + SessionTransition(state.copy(lastStartFailed = false, startRefused = false)) } else { SessionTransition(state) } @@ -186,7 +192,12 @@ class SessionReducer { } else { // A carried failed-start tone goes back to Idle uncleared: the warm build's // outcome is silent either way, and only a tap or a save clears the tone. - SessionTransition(QuickBuildSessionState.Idle(lastStartFailed = state.lastStartFailed)) + SessionTransition( + QuickBuildSessionState.Idle( + lastStartFailed = state.lastStartFailed, + startRefused = state.startRefused, + ), + ) } } @@ -300,7 +311,10 @@ class SessionReducer { // lastStartFailed keeps the error tone on the bolt after the failure flash // fades - a plain Idle here read READY right after a failed start (Q8). SessionTransition( - QuickBuildSessionState.Idle(lastStartFailed = true), + QuickBuildSessionState.Idle( + lastStartFailed = true, + startRefused = event.message == QuickBuildMessage.DisabledBySqliteDebugger, + ), listOf(SessionEffect.WithdrawAsk, SessionEffect.SurfaceProvisioningError(event.message)), ) } diff --git a/resources/src/main/res/values/strings.xml b/resources/src/main/res/values/strings.xml index ba8a7c439b..fbd9b0622e 100644 --- a/resources/src/main/res/values/strings.xml +++ b/resources/src/main/res/values/strings.xml @@ -1218,6 +1218,7 @@ Quick Build: starting a full build… Quick Build failed - see Build Output Quick Build: setup failed - tap to retry + Quick Build: off while the SQLite debugger is on - tap for details Quick Build failed. See the Build Output panel for more details. Quick Build successful Quick Build compiler restarting… From 0550b8899c1e983129fda2a28cb5769bf02987e5 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:48:37 -0800 Subject: [PATCH 18/56] ADFA-7138 End a live Quick Build session in the refused state when the SQLite debugger is turned on Adding cogo.sqliteDebugger=true and saving during a live session made the rebaseline refuse, but the manager recorded that as a failed rebuild ("Quick Build failed"), parked for retry, and was refused again on every save. A refused rebuild now ends like a refused start: the session goes to Idle with startRefused, so the status line reads "Quick Build: off while the SQLite debugger is on - tap for details", and nothing retries. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../session/QuickBuildSessionManager.kt | 8 ++++++- .../session/QuickBuildSessionManagerTest.kt | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt index 353850626e..5678923034 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManager.kt @@ -1343,7 +1343,13 @@ class QuickBuildSessionManager( // event, so a still-broken build file does not loop. session.orchestrator.onProxyAppRebuildFailed() val park = rebuildPark - if (park != null) { + if (result.message == QuickBuildMessage.DisabledBySqliteDebugger) { + // Not a build failure: the user turned the SQLite debugger on mid-session, so + // Quick Build is off for this project. A park would retry on every save and be + // refused each time; end like a refused start (Idle, "off" status) instead. + log.info("Proxy app rebuild refused: the SQLite debugger is on; ending the session") + dispatch(SessionEvent.ProvisioningFailed(result.message)) + } else if (park != null) { // Park recoverable instead of dying to Idle: the session and the running proxy // app are both fine; what failed is the user's build files. The message is // surfaced here rather than through SurfaceProvisioningError, whose effect diff --git a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt index ae7120ce84..212220a830 100644 --- a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt +++ b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/service/session/QuickBuildSessionManagerTest.kt @@ -1680,6 +1680,28 @@ class QuickBuildSessionManagerTest { assertThat(userMessages).contains(QuickBuildMessage.Literal("manifest does not build")) } + @Test + fun `a rebuild refused for the SQLite debugger ends in the refused state with no retry`() = + runTest { + proxyAppRebuildOutcome = { ProxyAppRebuildOutcome.Failure(QuickBuildMessage.DisabledBySqliteDebugger) } + val manager = createManager() + manager.onQuickBuildTapped() + advanceUntilIdle() + + manager.save(gradleFile) + advanceUntilIdle() + + // Same state as a refused start, not a parked failed rebuild. + assertThat(manager.state.value) + .isEqualTo(QuickBuildSessionState.Idle(lastStartFailed = true, startRefused = true)) + assertThat(userMessages).contains(QuickBuildMessage.DisabledBySqliteDebugger) + assertThat(proxyAppRebuildCount).isEqualTo(1) + + manager.save(gradleFile) + advanceUntilIdle() + assertThat(proxyAppRebuildCount).isEqualTo(1) + } + // Review gap (2026-07-26 #69): pin the failed proxy app rebuild's DAEMON state and the // recovery - the session must stay recoverable, not linger wedged and daemon-less. @Test From babc5fe530a63d08dff1ac3129cc37bd0408d29e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:55:56 -0800 Subject: [PATCH 19/56] ADFA-7138 Drop the unused sqliteDebuggerMessage and share the AAR-property check The Quick Build opt-in check threw sqliteDebuggerMessage's result away and kept only its null test, and the function's doc still pointed at that use; it and its test are gone and the doc moved to isSqliteDebuggerEnabled. SqliteRewritePlugin repeated SqliteDebuggerPlugin's AAR-property check; both now call Project.requireSqliteAar, with the same messages. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 6 ++-- .../quickbuild/QuickBuildProjectSupport.kt | 24 ++++--------- .../QuickBuildProjectSupportTest.kt | 7 ---- .../com/itsaky/androidide/gradle/SqliteAar.kt | 34 +++++++++++++++++++ .../androidide/gradle/SqliteDebuggerPlugin.kt | 16 +-------- .../gradle/sqlite/SqliteRewritePlugin.kt | 15 ++------ 6 files changed, 46 insertions(+), 56 deletions(-) create mode 100644 gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.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 0d2eda903a..db7848ac70 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -560,13 +560,13 @@ class GradleQuickBuildProvisioner( projectManager: IProjectManager, projectRoot: File, ): ProxyAppBuildResult.Refused? { - QuickBuildProjectSupport.sqliteDebuggerMessage( + val enabled = QuickBuildProjectSupport.isSqliteDebuggerEnabled( projectRoot, Environment.GRADLE_USER_HOME, projectManager.getAndroidAppModules().map { File(it.projectDirPath) }, - ), - ) ?: return null + ) + if (!enabled) return null log.info( "Quick Build is not provisioned: {} is true in the Gradle user home's, an app module's or the project's gradle.properties", GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED, 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 58fe82ebff..6ad9f0beac 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupport.kt @@ -68,23 +68,6 @@ object QuickBuildProjectSupport { 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 one `gradle.properties` text opts in, decided as the Gradle plugin does: the value is * exactly `true`. @@ -94,7 +77,12 @@ object QuickBuildProjectSupport { 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 + * Whether the project opts in to `cogo.sqliteDebugger=true`, in which case Quick Build must not + * provision it: the Gradle plugin moves such a project to requery and never applies Quick Build + * to it, so a build would succeed and end in a "variant setup missing" message that blames the + * variant. Decided per project, from all app modules (see [moduleDirs]). + * + * Reads the opt-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 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 ceb3e13999..9a6ab5b9ee 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildProjectSupportTest.kt @@ -77,13 +77,6 @@ class QuickBuildProjectSupportTest { 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() diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt new file mode 100644 index 0000000000..625f2c997b --- /dev/null +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt @@ -0,0 +1,34 @@ +package com.itsaky.androidide.gradle + +import com.itsaky.androidide.tooling.api.GradlePluginConfig.PROPERTY_SQLITE_DEBUGGER_ENABLED +import org.gradle.api.GradleException +import org.gradle.api.Project +import java.io.File + +/** + * The AAR that [property] names, for a plugin applied only when `cogo.sqliteDebugger=true`. + * Fails the build when the property is unset or the file is missing: the project opted in, and a + * build that quietly drops the feature leaves the user wondering why it does nothing. + * + * @param aarLabel what the AAR is called in the "not found" message. + * @param feature what the user's opt-in turns on, for the "remove the line" advice. + */ +internal fun Project.requireSqliteAar( + property: String, + aarLabel: String, + feature: String, +): File { + val aar = + findProperty(property)?.let { File(it.toString()) } + ?: throw GradleException( + "'$PROPERTY_SQLITE_DEBUGGER_ENABLED' is true but no property '$property' is set. " + + "Remove the line from gradle.properties to build without the $feature.", + ) + if (!aar.isFile) { + throw GradleException( + "$aarLabel not found at '${aar.absolutePath}'. " + + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it.", + ) + } + return aar +} 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 72e2b3985d..a52cfbb5f7 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 @@ -6,11 +6,9 @@ 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 /** @@ -54,19 +52,7 @@ class SqliteDebuggerPlugin : Plugin { 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.", - ) - } + val aar = target.requireSqliteAar(PROPERTY_SQLITE_DEBUGGER_AAR, "SQLite debugger AAR", "SQLite debugger") target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).apply { val addDebugger = { variant: ApplicationVariant -> 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 3f80201b35..5fece80083 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 @@ -6,14 +6,13 @@ 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.requireRuntimeConfiguration +import com.itsaky.androidide.gradle.requireSqliteAar 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 @@ -40,17 +39,7 @@ class SqliteRewritePlugin : Plugin { 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.requireSqliteAar(PROPERTY_SQLITE_REWRITE_RUNTIME_AAR, "SQLite rewrite runtime AAR", "SQLite rewrite") target.extensions.getByType(ApplicationAndroidComponentsExtension::class.java).onVariants { variant -> variant.instrumentation.transformClassesWith( From b93bf8e3e2b37e37dbde60f37771526e306adeb7 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:55:56 -0800 Subject: [PATCH 20/56] ADFA-7138 Document that the SQLite debugger opt-in is decided from all app modules Quick Build builds only the first app module, but the opt-in check reads every app module's gradle.properties, so two app modules that disagree are refused for both. That is deliberate: the opt-in is per project. A comment at the check and a sentence in the README say so. No behaviour change. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt | 2 ++ subprojects/sqlite-debugger/README.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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 db7848ac70..908d0b80b8 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -560,6 +560,8 @@ class GradleQuickBuildProvisioner( projectManager: IProjectManager, projectRoot: File, ): ProxyAppBuildResult.Refused? { + // Every app module's gradle.properties is read, although Quick Build builds only the first + // app module: the opt-in is decided per project, so two modules that disagree refuse here. val enabled = QuickBuildProjectSupport.isSqliteDebuggerEnabled( projectRoot, diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 25e532f056..8d6516f4a1 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -13,7 +13,7 @@ A project made from the JavaScript in SQLite template has it on already. For any 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. +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. The opt-in is decided per project from all app modules: Code on the Go reads the Gradle user home, every app module and the root, so one module that opts in turns Quick Build off for all of them, even though Quick Build builds only the first app module. 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, 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). From 541a706ac41cc46cc714170e33af04807792e732 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:40:08 -0800 Subject: [PATCH 21/56] ADFA-7138 Watch every gradle.properties the SQLite debugger opt-in reads QuickBuildProjectLayout.watchedFiles() listed only the root's gradle.properties, so turning cogo.sqliteDebugger=true on in an app module's or the Gradle user home's gradle.properties never invalidated a live Quick Build session and Quick Build stayed on. It now lists every module's and the Gradle user home's gradle.properties too, so a change invalidates the session and the next start refuses. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 2 ++ .../data/QuickBuildProjectLayout.kt | 15 +++++++++---- .../data/QuickBuildProjectLayoutTest.kt | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 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 908d0b80b8..d6588555bf 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -169,6 +169,7 @@ class GradleQuickBuildProvisioner( extraSourceRoots = proxyApp.sourceRoots, stableIdsFile = proxyApp.stableIdsFile, libraryResourceFlats = proxyApp.libraryResourceFlats, + gradleUserHome = Environment.GRADLE_USER_HOME, ), variantName = buildResult.variantName, baselineGeneration = buildResult.baselineGeneration, @@ -255,6 +256,7 @@ class GradleQuickBuildProvisioner( extraSourceRoots = buildResult.proxyApp.sourceRoots, stableIdsFile = buildResult.proxyApp.stableIdsFile, libraryResourceFlats = buildResult.proxyApp.libraryResourceFlats, + gradleUserHome = Environment.GRADLE_USER_HOME, ), ) } diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt index f0dcf22769..e47c0b1243 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt @@ -29,6 +29,8 @@ import java.io.File * @property libraryResourceFlats pre-compiled `.flat` resource units from the proxy app build, * passed to `aapt2 link` as `-R` overlays so a relink can resolve a resource only a dependency * AAR declares (e.g. Material3's `Theme.Material3.DayNight.NoActionBar`). + * @property gradleUserHome the Gradle user home, whose `gradle.properties` is watched with the + * project's own; null when there is none to watch. */ class QuickBuildProjectLayout( val projectRoot: File, @@ -37,6 +39,7 @@ class QuickBuildProjectLayout( private val extraSourceRoots: List = emptyList(), private val stableIdsFile: File? = null, private val libraryResourceFlats: List = emptyList(), + private val gradleUserHome: File? = null, ) { private val mainDir = File(appModuleDir, "src/main") @@ -107,18 +110,22 @@ class QuickBuildProjectLayout( /** * Exact files watched outside the roots (gradle config; changes invalidate). * - * @return the root's settings/properties/version-catalog files plus both build-script - * spellings for every module, listed unconditionally - only the existing ones are polled. + * @return the root's settings/properties/version-catalog files, both build-script spellings + * and a `gradle.properties` for every module, and the Gradle user home's `gradle.properties`, + * listed unconditionally - only the existing ones are polled. Every `gradle.properties` that + * `QuickBuildProjectSupport.isSqliteDebuggerEnabled` can read is here, so turning the + * opt-in on invalidates a live session. */ fun watchedFiles(): List = - listOf( + listOfNotNull( File(projectRoot, "settings.gradle"), File(projectRoot, "settings.gradle.kts"), File(projectRoot, "gradle.properties"), File(projectRoot, "gradle/libs.versions.toml"), + gradleUserHome?.let { File(it, "gradle.properties") }, ) + moduleDirs().flatMap { - listOf(File(it, "build.gradle"), File(it, "build.gradle.kts")) + listOf(File(it, "build.gradle"), File(it, "build.gradle.kts"), File(it, "gradle.properties")) } /** diff --git a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt index cdb7410ea8..9342209127 100644 --- a/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt +++ b/quickbuild/core/src/test/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayoutTest.kt @@ -1,6 +1,7 @@ package org.appdevforall.cotg.quickbuild.data import com.google.common.truth.Truth.assertThat +import org.appdevforall.cotg.quickbuild.domain.watch.WatchFilter import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File @@ -142,6 +143,27 @@ class QuickBuildProjectLayoutTest { ) } + @Test + fun `watchedFiles includes every gradle properties file the sqlite debugger opt-in reads`() { + write("app/build.gradle.kts") + write("feature-login/build.gradle") + val home = File(root, "gradle-user-home") + + val layout = QuickBuildProjectLayout(root, gradleUserHome = home) + val watched = layout.watchedFiles() + + assertThat(watched).containsAtLeast( + File(root, "gradle.properties"), + File(root, "app/gradle.properties"), + File(root, "feature-login/gradle.properties"), + File(home, "gradle.properties"), + ) + // The session's filter must pass a change to each of them, or the opt-in goes unseen. + val filter = WatchFilter(layout.watchedRoots(), watched) + assertThat(filter.isRelevant(File(home, "gradle.properties"))).isTrue() + assertThat(filter.isRelevant(File(root, "feature-login/gradle.properties"))).isTrue() + } + @Test fun `module discovery skips build intermediates and hidden dirs`() { write("app/build.gradle.kts") From 5ebd1fc54b54adb15283777858d80200defd2343 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:40:08 -0800 Subject: [PATCH 22/56] ADFA-7138 Name the Quick Build refusal that replaced sqliteDebuggerMessage in the docs Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index ff317cac29..cd7a92d8ce 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -161,7 +161,7 @@ provider, `` entry or classes). QA runs it on the release APK of an opt 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 + either (`GradleQuickBuildProvisioner.sqliteDebuggerRefusal`): 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. From b843c60d0e091c4740bc7a358c94c69cf5165879 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:40:08 -0800 Subject: [PATCH 23/56] ADFA-7138 Describe the opt-in rule: the first file that defines it decides Modules that disagree do not refuse: the Gradle user home, then each app module, then the root are read in order and the first one that defines cogo.sqliteDebugger decides for the whole project. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../androidide/quickbuild/GradleQuickBuildProvisioner.kt | 4 +++- subprojects/sqlite-debugger/README.md | 2 +- 2 files changed, 4 insertions(+), 2 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 d6588555bf..452fed5594 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -563,7 +563,9 @@ class GradleQuickBuildProvisioner( projectRoot: File, ): ProxyAppBuildResult.Refused? { // Every app module's gradle.properties is read, although Quick Build builds only the first - // app module: the opt-in is decided per project, so two modules that disagree refuse here. + // app module: the opt-in is decided per project, and the first file that defines the + // property decides it (the Gradle user home, then the modules in order, then the root), so + // a module's `false` overrides a root `true` as it does in Gradle. val enabled = QuickBuildProjectSupport.isSqliteDebuggerEnabled( projectRoot, diff --git a/subprojects/sqlite-debugger/README.md b/subprojects/sqlite-debugger/README.md index 8d6516f4a1..e01df107f5 100644 --- a/subprojects/sqlite-debugger/README.md +++ b/subprojects/sqlite-debugger/README.md @@ -13,7 +13,7 @@ A project made from the JavaScript in SQLite template has it on already. For any 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. The opt-in is decided per project from all app modules: Code on the Go reads the Gradle user home, every app module and the root, so one module that opts in turns Quick Build off for all of them, even though Quick Build builds only the first app module. Commenting the line out turns all of this off. +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. The opt-in is decided per project from all app modules: Code on the Go reads the Gradle user home, every app module and the root, so the first of those files that defines the line decides for the whole project, whatever the other modules say, even though Quick Build builds only the first app module. 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, 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). From 90266f213dce525a468496f8f5398971619fb27d Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 12:53:52 -0800 Subject: [PATCH 24/56] ADFA-7138 Narrow the watchedFiles KDoc to the modules the scan finds The KDoc said every gradle.properties isSqliteDebuggerEnabled can read is watched. sqliteDebuggerRefusal reads the directories of every app module in the project model, but watchedFiles lists only the app module Quick Build builds plus the modules moduleDirs finds, at most MODULE_SCAN_MAX_DEPTH levels below the root. Say so, and that a deeper app module's opt-in is read at the next proxy app build (provision or rebaseline) but does not end a running session. Documentation only: no behaviour change, so no test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../cotg/quickbuild/data/QuickBuildProjectLayout.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt index e47c0b1243..454e570131 100644 --- a/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt +++ b/quickbuild/core/src/main/java/org/appdevforall/cotg/quickbuild/data/QuickBuildProjectLayout.kt @@ -111,10 +111,13 @@ class QuickBuildProjectLayout( * Exact files watched outside the roots (gradle config; changes invalidate). * * @return the root's settings/properties/version-catalog files, both build-script spellings - * and a `gradle.properties` for every module, and the Gradle user home's `gradle.properties`, - * listed unconditionally - only the existing ones are polled. Every `gradle.properties` that - * `QuickBuildProjectSupport.isSqliteDebuggerEnabled` can read is here, so turning the - * opt-in on invalidates a live session. + * and a `gradle.properties` for every module [moduleDirs] returns (the app module Quick Build + * builds, and each module the scan finds up to [MODULE_SCAN_MAX_DEPTH] levels below the root), + * and the Gradle user home's `gradle.properties`, listed unconditionally - only the existing + * ones are polled. So turning `QuickBuildProjectSupport.isSqliteDebuggerEnabled`'s opt-in on + * in one of those files invalidates a live session. Another app module nested deeper is read + * at the next proxy app build (a provision or a rebaseline), but its `gradle.properties` is + * not watched, so turning the opt-in on there does not end a running session. */ fun watchedFiles(): List = listOfNotNull( From 27e004b6263815d88a5d9d2fce38c8fa1bcf1c17 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 19:09:05 -0800 Subject: [PATCH 25/56] ADFA-7138 Run the release-APK guard on every push Nothing ran scripts/check-release-apk-no-sqlite-debugger.sh. SqliteReleaseApkTest builds the minified release and the debug variant of the functional tests' sample app, opted in, with the SQLite debugger and rewrite runtime AARs this repo builds and requery 3.49.0 (JitPack) as a file dependency, and runs the script on both APKs. The release must pass. The debug APK is the control: it must fail on the manifest provider and on libsqlitedbg.so, the checks R8 cannot defeat by renaming classes. The release build also runs the classpath check against requery's real AAR, which the other tests replace with jar stand-ins. :gradle-plugin:sqliteReleaseApkTest builds the two AARs and passes their paths, so :gradle-plugin:test leaves the class out. debug.yml's build_apk job runs the task before the assemble, which then finds the AARs up to date. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .github/workflows/debug.yml | 10 ++ gradle-plugin/build.gradle.kts | 63 ++++++++++++- gradle-plugin/docs/sqlite-rewrite.md | 11 ++- .../androidide/gradle/SqliteReleaseApkTest.kt | 94 +++++++++++++++++++ gradle/libs.versions.toml | 2 + .../check-release-apk-no-sqlite-debugger.sh | 3 +- 6 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml index b0f21d2304..9800a82276 100644 --- a/.github/workflows/debug.yml +++ b/.github/workflows/debug.yml @@ -221,6 +221,16 @@ jobs: exit "$status" fi + # ADFA-7138: builds the minified release and the debug variant of a sample app that opted into the + # SQLite debugger, with the real debugger, rewrite runtime and requery AARs, and runs + # scripts/check-release-apk-no-sqlite-debugger.sh on both. The release must pass; the debug APK must + # fail, which shows the check still sees the debugger. The AARs it builds are the ones the assemble + # below bundles, so they are not built twice. + - name: Check that an opted-in app's release APK carries no SQLite debugger + run: | + command -v unzip || { sudo apt-get update && sudo apt-get install -y unzip; } + flox activate -d flox/base -- ./gradlew :gradle-plugin:sqliteReleaseApkTest --no-daemon + - name: Install advancecomp (advzip, used by recompressApk) run: | command -v advzip || { sudo apt-get update && sudo apt-get install -y advancecomp; } diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index ac6d85a7c2..96cee1492b 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -35,9 +35,7 @@ description = "Gradle Plugin for projects that are built with AndroidIDE" // projectsEvaluated sweep silently misses projects under configure-on-demand. val mavenLocalStagingProjects = listOf(":logsender", ":logger", ":build-info") -tasks.named("test") { - useJUnitPlatform() - +fun Test.stageMavenLocalRepos() { val stagedRepos = mavenLocalStagingProjects.map { path -> dependsOn("$path:publishAllPublicationsToBuildMavenLocalRepository") @@ -65,6 +63,65 @@ tasks.named("test") { } } +val sqliteReleaseApkTestClass = "com.itsaky.androidide.gradle.SqliteReleaseApkTest" + +tasks.named("test") { + useJUnitPlatform() + stageMavenLocalRepos() + // Run by sqliteReleaseApkTest below, which builds the AARs it needs + filter { excludeTestsMatching(sqliteReleaseApkTestClass) } +} + +// The release-APK guard (ADFA-7138): SqliteReleaseApkTest builds the minified release and the debug variant +// of a sample app that opted into the SQLite debugger, with the real debugger, rewrite runtime and requery +// AARs, and runs scripts/check-release-apk-no-sqlite-debugger.sh on both APKs. A task of its own because it +// needs the NDK-built debugger AAR, which the other tests do not; CI's build_apk job runs it on every push. +val requeryAar = + configurations.create("sqliteReleaseApkTestRequery") { + isCanBeConsumed = false + isTransitive = false + } + +dependencies { + add(requeryAar.name, variantOf(libs.tests.requery.sqliteAndroid) { artifactType("aar") }) +} + +// Under configure-on-demand the AAR projects are not configured yet when the task below reads their build +// directories, as in :app +val sqliteReleaseApkTestAarProjects = + mapOf( + "debugger" to ":subprojects:sqlite-debugger", + "rewriteRuntime" to ":subprojects:sqlite-rewrite-runtime", + ) +sqliteReleaseApkTestAarProjects.values.forEach(::evaluationDependsOn) + +tasks.register("sqliteReleaseApkTest") { + description = "Builds an opted-in sample app's release APK and checks that it carries no SQLite debugger" + group = "verification" + val testSourceSet = sourceSets["test"] + testClassesDirs = testSourceSet.output.classesDirs + classpath = testSourceSet.runtimeClasspath + useJUnitPlatform() + stageMavenLocalRepos() + filter { includeTestsMatching(sqliteReleaseApkTestClass) } + + // The tasks AndroidIDEAssetsPlugin bundles these AARs with, so this task and :app:assemble build them once + for ((name, path) in sqliteReleaseApkTestAarProjects) { + dependsOn("$path:assembleRelease") + val aar = + project(path) + .layout.buildDirectory + .file("outputs/aar/${path.substringAfterLast(':')}-release.aar") + .get() + .asFile + inputs.file(aar).withPropertyName("${name}Aar") + systemProperty("cogo.test.sqlite.${name}Aar", aar.absolutePath) + } + inputs.files(requeryAar).withPropertyName("requeryAar") + inputs.file(rootProject.file("scripts/check-release-apk-no-sqlite-debugger.sh")).withPropertyName("checkScript") + jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-Dcogo.test.sqlite.requeryAar=${requeryAar.singleFile.absolutePath}") }) +} + configurations { val androidBuildTool = create("androidBuildTool") diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index cd7a92d8ce..f1ef51e631 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -138,7 +138,16 @@ 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. +provider, `` entry or classes). The release build must never contain the debugger. R8 renames and drops +classes in a minified release, so the class check can miss them; the manifest and native-library checks are the +ones that hold. + +`:gradle-plugin:sqliteReleaseApkTest` (`SqliteReleaseApkTest`) runs it on every push, in the `build_apk` job of +`.github/workflows/debug.yml`. It builds the minified release and the debug variant of the functional tests' sample +app, opted in, with the debugger and rewrite runtime AARs this repo builds and requery 3.49.0 from JitPack as a file +dependency, and runs the script on both APKs. The release must pass. The debug APK must fail on the provider and on +`libsqlitedbg.so`, which shows the check still sees what the AAR brings. The task builds the AARs it needs, so +`:gradle-plugin:test` leaves this test out. QA can still run the script on any release APK. ## Known limits diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt new file mode 100644 index 0000000000..e290ea18b1 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt @@ -0,0 +1,94 @@ +package com.itsaky.androidide.gradle + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.gradle.sqlite.requireEnvironment +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 java.io.File +import java.util.concurrent.TimeUnit + +/** + * The release-APK guard (ADFA-7138): an app that opted into the SQLite debugger gets it in its debuggable + * variants only. Builds the sample app's minified release and its debug variant with the real debugger, + * rewrite runtime and requery AARs, and runs scripts/check-release-apk-no-sqlite-debugger.sh on both APKs. + * + * The debug APK is the control: it must fail the check, which shows the check still sees the debugger the + * AAR brings. R8 renames and drops classes in a release, so the dex grep of the script can miss them; the + * manifest provider and libsqlitedbg.so are what the control requires to fail. + * + * Run by the sqliteReleaseApkTest task, which builds the AARs and passes their paths. + */ +class SqliteReleaseApkTest { + private fun aar(name: String): File { + val path = System.getProperty("cogo.test.sqlite.${name}Aar") + requireEnvironment(path != null && File(path).isFile, "the $name AAR: run the :gradle-plugin:sqliteReleaseApkTest task") + return File(path!!) + } + + private class CheckResult( + val exitCode: Int, + val output: String, + ) + + private fun checkApk(apk: File): CheckResult { + assertWithMessage("APK not built").that(apk.isFile).isTrue() + val script = FileProvider.projectRoot().resolve("scripts/check-release-apk-no-sqlite-debugger.sh").toFile() + val process = ProcessBuilder("bash", script.path, apk.path).redirectErrorStream(true).start() + val output = process.inputStream.bufferedReader().readText() + check(process.waitFor(1, TimeUnit.MINUTES)) { "the check script did not finish" } + println("${apk.name}: exit ${process.exitValue()}\n$output") + return CheckResult(process.exitValue(), output) + } + + @Test + fun `an opted-in app's release APK carries no SQLite debugger, and its debug APK fails the check`( + @TempDir dir: File, + ) { + val debuggerAar = aar("debugger") + val rewriteRuntimeAar = aar("rewriteRuntime") + val requeryAar = aar("requery") + + // compileSdk: the platform CI's flox environment installs. requery comes from libs/ in a real app. + val project = + copySampleProject( + dir, + """ + android { compileSdk = 36 } + dependencies { implementation(files("${requeryAar.invariantSeparatorsPath}")) } + """.trimIndent(), + ) + // R8 does not fit in the fixture's 512m daemon + project.resolve("gradle.properties").toFile().appendText("\norg.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8\n") + + val result = + buildProject(task = ":app:assembleDemoRelease", projectDir = project, configureArgs = { + it += ":app:assembleDemoDebug" + it += "-P$PROPERTY_SQLITE_DEBUGGER_ENABLED=true" + it += "-P$PROPERTY_SQLITE_DEBUGGER_AAR=${debuggerAar.absolutePath}" + it += "-P$PROPERTY_SQLITE_REWRITE_RUNTIME_AAR=${rewriteRuntimeAar.absolutePath}" + }) + + // The release is a real opted-in build: its classpath check accepts requery's AAR as a file dependency, + // and it is rewritten, then minified + assertThat(result.task(":app:checkDemoReleaseSqliteRewriteClasspath")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:transformDemoReleaseClassesWithAsm")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.task(":app:minifyDemoReleaseWithR8")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(result.output).contains("Adding SQLite debugger dependency to variant 'demoDebug' of project ':app'") + + val apks = project.resolve("app/build/outputs/apk/demo").toFile() + val release = checkApk(File(apks, "release/app-demo-release-unsigned.apk")) + assertWithMessage(release.output).that(release.exitCode).isEqualTo(0) + assertThat(release.output).contains("OK: no SQLite debugger found") + + val debug = checkApk(File(apks, "debug/app-demo-debug.apk")) + assertWithMessage(debug.output).that(debug.exitCode).isEqualTo(1) + assertThat(debug.output).contains("FAIL: manifest declares provider org.appdevforall.sqlitedebugger.DebuggerStartup") + assertThat(debug.output).containsMatch("FAIL: native library lib/[^/]+/libsqlitedbg\\.so") + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 45225bc68f..0e023731a4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -302,6 +302,8 @@ tests-junit-platformLauncher = { module = "org.junit.platform:junit-platform-lau tests-junit-vintageEngine = { module = "org.junit.vintage:junit-vintage-engine", version.ref = "junit-jupiter" } core-tests-anroidx-arch = { module = "androidx.arch.core:core-testing", version.ref = "anroidx-test-core" } tests-google-truth = { module = "com.google.truth:truth", version = "1.4.1" } +# The requery the ADFA-7082 template ships in libs/ (JitPack); :gradle-plugin's sqliteReleaseApkTest builds against it +tests-requery-sqliteAndroid = { module = "com.github.requery:sqlite-android", version = "3.49.0" } tests-robolectric = { module = "org.robolectric:robolectric", version = "4.11.1" } tests-androidx-test-core = { module = "androidx.test:core", version = "1.5.0" } tests-androidx-test-monitor = { module = "androidx.test:monitor", version = "1.8.0" } diff --git a/scripts/check-release-apk-no-sqlite-debugger.sh b/scripts/check-release-apk-no-sqlite-debugger.sh index 5e25938e5e..53a6cf42f9 100755 --- a/scripts/check-release-apk-no-sqlite-debugger.sh +++ b/scripts/check-release-apk-no-sqlite-debugger.sh @@ -79,7 +79,8 @@ if awk ' 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). +# the manifest is clean (a library that registers nothing). R8 can rename or drop classes in a minified release, +# so this check can miss them: the manifest and native-library checks above are the ones that hold. # 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) From 298071c398c29484e1a1af4a962d11d1299e6733 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 19:09:05 -0800 Subject: [PATCH 26/56] ADFA-7138 Document that COPY_FRAMES cannot weaken another instrumentation Review asked whether the rewrite's per-variant COPY_FRAMES could leave stale frames in classes another instrumentation changes. It cannot: AGP 7.3.0, 8.8.2 and 9.3.1 keep the highest mode any plugin asks for (javap of AsmClassVisitorsFactoryRegistry.setAsmFramesComputationMode), and COPY_FRAMES is the lowest. The docs record that and the one-off AGP 9.3.1 build that confirmed it, with its control. No code change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index f1ef51e631..084754990d 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -93,6 +93,20 @@ cannot load, so the transform runs only on projects that opted in. Commenting th 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. + The mode is per variant, but `COPY_FRAMES` cannot weaken another instrumentation in the same build (Hilt, Firebase, + an app's own `transformClassesWith`) that needs frames computed: `setAsmFramesComputationMode` keeps the highest + mode any plugin asks for (AGP 7.3.0, 8.8.2 and 9.3.1 compare the enum ordinals; checked with javap), and + `COPY_FRAMES` is the lowest. Checked once (2026-11-17, AGP 9.3.1, not a standing test) in the order a last-call-wins + rule would break: CoGo applies its plugin in `afterEvaluate`, so its `onVariants` runs after the app's. The + functional tests' sample app, opted in, got a `SQLiteOpenHelper` subclass, a `SQLiteCursorDriver` and a second + `AsmClassVisitorFactory` (scope ALL) that inserts `if (0 != 0) throw null` at the start of every method but + constructors of the app, androidx.sqlite and Room, and asks for `COMPUTE_FRAMES_FOR_INSTRUMENTED_METHODS`. + demoDebug and demoRelease assembled (D8, R8). Their transformed classes, 232 each, pass ASM's `CheckClassAdapter` + and the JVM's verifier (each class linked against android.jar and the instrumented dependency jars), except Room's + `LimitOffsetDataSource`, which cannot load because the app has no `androidx.paging`. `Driver.query` carries the + `CHECKCAST` prologue and the branch, with a computed frame at the branch target. The control, the same build + without the request for computed frames, fails the JVM's verifier (`Expecting a stackmap frame at branch + target`), so the check does see stale frames. 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 From 61104a772731b85797c103f7ab8ffb7f0313bac8 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 19:09:05 -0800 Subject: [PATCH 27/56] ADFA-7138 Warn about strings that name a class the rewrite moves to requery Strings are not rewritten, so Class.forName("android.database.sqlite.SQLiteDatabase") or reflection on such a name reaches Android's class while the rewritten code uses requery's. Under the full policy, a string constant naming one of the renamed classes (dots or slashes, '$' or '.' before a nested class) now logs a warning with class, method and line. Not a failure: the string may only be printed. Names of kept classes, such as SQLiteException or SQLiteDatabaseLockedException, are not reported. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 4 ++- .../gradle/sqlite/SqliteRewriteRules.kt | 14 ++++++++++ .../gradle/sqlite/SqliteRewriter.kt | 10 ++++++- .../gradle/sqlite/SqliteRewriterTest.kt | 27 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 084754990d..c248a82194 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -187,7 +187,9 @@ dependency, and runs the script on both APKs. The release must pass. The debug A either (`GradleQuickBuildProvisioner.sqliteDebuggerRefusal`): 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. + rewritten. A string constant that names one of the renamed classes (dots or slashes, `$` or `.` before a nested + class) logs a warning with class, method and line, in the classes the fail rule covers; names of kept classes such + as `SQLiteException` do not. - 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 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 index b238f290b9..f7cd025855 100644 --- 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 @@ -73,6 +73,20 @@ internal object SqliteRewriteRules { /** The Android classes that get renamed. */ val SQLITE_CLASSES: Set = RENAMES.keys.filter { it.startsWith(SQLITE_PKG) }.toSet() + /** + * A renamed class in a string, as Class.forName or reflection spells it: dots or slashes, `$` or `.` before a + * nested class. Longest names first, and not followed by more of a name, so a kept class such as + * SQLiteDatabaseLockedException does not match SQLiteDatabase. + */ + val RENAMED_CLASS_NAME: Regex = + Regex( + RENAMES.keys + .sortedByDescending { it.length } + .joinToString("|", prefix = "(?:", postfix = ")(?![A-Za-z0-9_])") { + it.replace("$", "[.$]").replace("/", "[./]") + }, + ) + /** Renamed superclasses of the renamed classes, from the Wave 0 supertypes.tsv. */ val RENAMED_SUPERS: Map> = mapOf( 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 96628ae14b..ef801f9b19 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 @@ -330,13 +330,21 @@ object SqliteRewriter { "SQL text uses COLLATE UNICODE, which does not exist in requery's SQLite (no ICU); the statement would fail", ) } + val where = if (line >= 0) "line $line" else "no line number" 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", ) } + // A warning, not a failure: the string may only be printed. Strings are not rewritten, so a class loaded by + // this name is Android's, which the rewritten code cannot mix with requery's. + Rules.RENAMED_CLASS_NAME.find(text)?.let { + warn( + "${cn.name.replace('/', '.')}.${m.name} ($where): the string \"${it.value}\" names a class the rewrite moves " + + "to requery. Strings are not rewritten: Class.forName or reflection on it reaches Android's class, not requery's", + ) + } } fun invokeDynamic( 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 70e7e6e7b2..809cc62335 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 @@ -1150,6 +1150,33 @@ class SqliteRewriterTest { assertThat(warnings.single()).contains("byte") } + @Test + fun `a string naming a renamed class only warns, and one naming a kept class does not`() { + val classes = + Compiler.compile( + listOf( + src( + "p.ByName", + """ + package p; + public class ByName { + Class db() throws Exception { return Class.forName("android.database.sqlite.SQLiteDatabase"); } + String factory() { return "android/database/sqlite/SQLiteDatabase${'$'}CursorFactory"; } + String kept() { return "android.database.sqlite.SQLiteException android.database.sqlite.SQLiteDatabaseLockedException"; } + } + """.trimIndent(), + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/ByName"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("p.ByName.db (line ") + assertThat(warnings[0]).contains("\"android.database.sqlite.SQLiteDatabase\"") + assertThat(warnings[1]).contains("p.ByName.factory (line ") + assertThat(warnings[1]).contains("\"android/database/sqlite/SQLiteDatabase\$CursorFactory\"") + } + @Test fun `calls on android's CursorWindow are not reported even when the hierarchy says it extends SQLiteClosable`() { val classes = From 9ca47ff481277ecd62387549ab5b7e6d41cc0c31 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 19:56:39 -0800 Subject: [PATCH 28/56] ADFA-7138 Run the release-APK guard without the Maven signing key The first CI run of the guard failed before the test: the functional tests stage :logger, :logsender and :build-info into build-local Maven repositories, and with debug.yml's ORG_GRADLE_PROJECT_signingInMemoryKey in the environment MavenPublishConf runs the Sign tasks, which failed ("Could not evaluate spec for 'Signing is required, or signatory is set'"). Staging needs no signing, so the step drops the three signing variables, as on a developer machine. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .github/workflows/debug.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml index 9800a82276..6ef3e75efc 100644 --- a/.github/workflows/debug.yml +++ b/.github/workflows/debug.yml @@ -229,7 +229,12 @@ jobs: - name: Check that an opted-in app's release APK carries no SQLite debugger run: | command -v unzip || { sudo apt-get update && sudo apt-get install -y unzip; } - flox activate -d flox/base -- ./gradlew :gradle-plugin:sqliteReleaseApkTest --no-daemon + # The test stages :logger, :logsender and :build-info into build-local Maven repositories, which + # needs no signing. With the job's signing key present MavenPublishConf runs the Sign tasks, and + # they fail, so the key is left out of this step. + env -u ORG_GRADLE_PROJECT_signingInMemoryKey -u ORG_GRADLE_PROJECT_signingInMemoryKeyId \ + -u ORG_GRADLE_PROJECT_signingInMemoryKeyPassword \ + flox activate -d flox/base -- ./gradlew :gradle-plugin:sqliteReleaseApkTest --no-daemon - name: Install advancecomp (advzip, used by recompressApk) run: | From 22d390f28dfe81ac00db0d781b09526d19fe9d03 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 22:03:32 -0800 Subject: [PATCH 29/56] ADFA-7138 Do not warn about a string that only contains a renamed class name RENAMED_CLASS_NAME had no leading boundary and is matched with find, so under the full policy a string such as "xandroid.database.sqlite.SQLiteDatabase" or "com.android.database.sqlite.SQLiteDatabase" warned that it names a class the rewrite moves to requery. Only a warning, never a failed build. The name now may not follow a letter, digit, '_', '.' or '$'. The one prefix still allowed is the 'L' of a type descriptor, itself not following a name character: a plain lookbehind would have stopped the warning on "[Landroid.database.sqlite.SQLiteDatabase;" (the name Class.forName takes for an array) and "(Landroid/database/sqlite/SQLiteDatabase;)V" (a descriptor MethodType.fromMethodDescriptorString resolves), which reach Android's class as the plain name does. A test pins both forms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 5 +- .../gradle/sqlite/SqliteRewriteRules.kt | 12 +++-- .../gradle/sqlite/SqliteRewriterTest.kt | 47 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index c248a82194..2667295640 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -188,8 +188,9 @@ dependency, and runs the script on both APKs. The release must pass. The debug A 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. A string constant that names one of the renamed classes (dots or slashes, `$` or `.` before a nested - class) logs a warning with class, method and line, in the classes the fail rule covers; names of kept classes such - as `SQLiteException` do not. + class, alone or in a type descriptor) logs a warning with class, method and line, in the classes the fail rule + covers; names of kept classes such as `SQLiteException`, and longer names that contain a renamed one, such as + `xandroid.database.sqlite.SQLiteDatabase`, do not. - 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 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 index f7cd025855..9d3f47775c 100644 --- 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 @@ -75,14 +75,20 @@ internal object SqliteRewriteRules { /** * A renamed class in a string, as Class.forName or reflection spells it: dots or slashes, `$` or `.` before a - * nested class. Longest names first, and not followed by more of a name, so a kept class such as - * SQLiteDatabaseLockedException does not match SQLiteDatabase. + * nested class. Longest names first, and neither preceded nor followed by more of a name, so a kept class such as + * SQLiteDatabaseLockedException does not match SQLiteDatabase, nor does a longer name such as + * xandroid.database.sqlite.SQLiteDatabase. The `L` of a type descriptor may precede it, as in + * `[Landroid.database.sqlite.SQLiteDatabase;`, the name Class.forName takes for an array of it. */ val RENAMED_CLASS_NAME: Regex = Regex( RENAMES.keys .sortedByDescending { it.length } - .joinToString("|", prefix = "(?:", postfix = ")(?![A-Za-z0-9_])") { + .joinToString( + "|", + prefix = "(?:(?() + SqliteRewriter.rewrite(classes["p/Longer"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Longer.named (line ") + } + + @Test + fun `a type descriptor or array class name naming a renamed class still warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Descriptors", + """ + package p; + public class Descriptors { + String array() { return "[Landroid.database.sqlite.SQLiteDatabase;"; } + String method() { return "(Landroid/database/sqlite/SQLiteDatabase;)V"; } + } + """.trimIndent(), + ), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Descriptors"]!!, warn = { warnings += it }) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("p.Descriptors.array (line ") + assertThat(warnings[1]).contains("p.Descriptors.method (line ") + } + @Test fun `calls on android's CursorWindow are not reported even when the hierarchy says it extends SQLiteClosable`() { val classes = From 6fd1fdcf97a1b161ffc9249a292f2bff3c91aa94 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 22:03:32 -0800 Subject: [PATCH 30/56] ADFA-7138 Docs: say SqliteReleaseApkTest skips without its AAR paths unless CI is set Run outside the sqliteReleaseApkTest task, as from an IDE, the test has no AAR paths and skips with a message (requireEnvironment); with the environment variable CI set it fails instead. The KDoc now says so. Test KDoc only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt index e290ea18b1..b85f62305b 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt @@ -22,7 +22,8 @@ import java.util.concurrent.TimeUnit * AAR brings. R8 renames and drops classes in a release, so the dex grep of the script can miss them; the * manifest provider and libsqlitedbg.so are what the control requires to fail. * - * Run by the sqliteReleaseApkTest task, which builds the AARs and passes their paths. + * Run by the sqliteReleaseApkTest task, which builds the AARs and passes their paths. Run without an AAR's path, as + * from an IDE, it skips with a message, and fails instead when the environment variable CI is set (requireEnvironment). */ class SqliteReleaseApkTest { private fun aar(name: String): File { From 9fedd913d3df0635ae233478e86e5ae7787cb124 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 22:38:47 -0800 Subject: [PATCH 31/56] ADFA-7138 Fail the build for a SQLiteQueryBuilder override of query with a signal The rewrite gives calls to SQLiteQueryBuilder.query(..., CancellationSignal) requery's descriptor, which ends in androidx.core's signal, but left a subclass's override of that overload with Android's. The calls then resolved to requery's inherited method and skipped the override. Such an override now fails the build, naming the class and method. Retyping the declaration was the alternative: its body is written against Android's signal, so the rewrite would have to turn requery's signal back into Android's on entry and keep the two connected, and patch the frames. The signal-less overloads keep their descriptors in requery and still override as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 4 ++- .../gradle/sqlite/SqliteRewriter.kt | 15 ++++++++ .../gradle/sqlite/SqliteRewriterTest.kt | 35 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 2667295640..49aab18cdc 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -81,7 +81,9 @@ cannot load, so the transform runs only on projects that opted in. Commenting th 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). + the override). So does an override of `SQLiteQueryBuilder.query` with a `CancellationSignal` in a subclass: its + calls get requery's descriptor while the override keeps Android's, so they would reach requery's method and skip + it. Retyping the override would mean turning requery's signal back into Android's inside its body. - 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 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 ef801f9b19..044529031b 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 @@ -155,6 +155,7 @@ object SqliteRewriter { 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.fail) bypassedOverride(m)?.let { violations += it } if (policy.redirect && m.name == Rules.DRIVER_QUERY_NAME && m.desc == Rules.DRIVER_QUERY_DESC) { retypeParameters(m, Rules.DRIVER_QUERY_NEW_DESC) } @@ -242,6 +243,20 @@ object SqliteRewriter { } } + /** + * A method that overrides one whose calls [decide] retypes, SQLiteQueryBuilder's `query` with a + * CancellationSignal: the calls get requery's descriptor and the declaration keeps Android's, so they would + * reach requery's inherited method and skip the override. Retyping the declaration too would mean turning + * requery's signal back into Android's in a body written for Android's, so the build fails instead. + */ + fun bypassedOverride(m: MethodNode): String? { + if (m.access and (Opcodes.ACC_STATIC or Opcodes.ACC_PRIVATE) != 0 || m.name !in Rules.DESCRIPTOR_RULE_NAMES) return null + val ancestors = cn.superName?.let { renamedAncestors(it) }.orEmpty() + val rule = Rules.DESCRIPTOR_RULES.firstOrNull { it.owner in ancestors && it.name == m.name && it.desc == m.desc } ?: return null + return "method '${m.name}${m.desc}' overrides ${simple(rule.owner)}.${m.name}, whose calls the rewrite gives requery's " + + "descriptor; they would reach requery's method and skip the override" + } + fun checkDeclarations() { for (f in cn.fields) { if (f.desc.contains(Rules.OPEN_PARAMS)) violations += "field '${f.name}' has type OpenParams, which requery lacks" 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 168c8f3283..e760dce5f7 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 @@ -1394,4 +1394,39 @@ class SqliteRewriterTest { assertThat(e.message).contains(name) } } + + @Test + fun `a query builder subclass that overrides the query taking a CancellationSignal fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.MyBuilder", + """ + package p; + import android.database.Cursor; + import android.database.sqlite.*; + import android.os.CancellationSignal; + public class MyBuilder extends SQLiteQueryBuilder { + @Override + public Cursor query(SQLiteDatabase db, String[] p, String s, String[] a, String g, String h, String o, String l, + CancellationSignal c) { + return super.query(db, p, s, a, g, h, o, l, c); + } + @Override + public Cursor query(SQLiteDatabase db, String[] p, String s, String[] a, String g, String h, String o) { + return null; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/MyBuilder"]!!, hierarchy) } + val overrides = e.message!!.lines().filter { "overrides SQLiteQueryBuilder.query" in it } + // the overload without a signal keeps its descriptor in requery, so overriding it still works + assertThat(overrides).hasSize(1) + assertThat(overrides.single()).contains("Landroid/os/CancellationSignal;)Landroid/database/Cursor;'") + assertThat(e.message).contains("p.MyBuilder") + } } From 7e38a3f9d78b9fe13ccacafe9f510c6186026e8a Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 22:51:28 -0800 Subject: [PATCH 32/56] ADFA-7138 Fail a call a rule could match when its owner cannot be resolved renamedAncestors turned a hierarchy lookup that found nothing (AGP's class loader cannot load the owner) into "no renamed ancestors", silently. A call such as isStrict() on an unresolved subclass of SQLiteQueryBuilder then passed the transform and would fail only at run time. Under the full policy, such a call now fails the build when its name and descriptor are those of a member a rule changes or rejects in a renamed class another class can extend: the members requery lacks and the retyped query. That is the policy the rewrite already has for a Context database method on an unresolved owner. Members of the final SQLiteDatabase, SQLiteQuery and SQLiteStatement and constructors are left out, since no other class inherits them, so a call such as rawQuery or delete(String) on an unresolved class does not fail. Field handles and method references go through the same check. The supertype cache now keeps null results too: getOrPut asked the hierarchy again for every call on an unresolved owner. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 4 ++ .../gradle/sqlite/SqliteRewriteRules.kt | 21 +++++++ .../gradle/sqlite/SqliteRewriter.kt | 31 ++++++++- .../gradle/sqlite/SqliteRewriterTest.kt | 63 ++++++++++++++++--- 4 files changed, 110 insertions(+), 9 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 49aab18cdc..8ab6558de8 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -84,6 +84,10 @@ cannot load, so the transform runs only on projects that opted in. Commenting th the override). So does an override of `SQLiteQueryBuilder.query` with a `CancellationSignal` in a subclass: its calls get requery's descriptor while the override keeps Android's, so they would reach requery's method and skip it. Retyping the override would mean turning requery's signal back into Android's inside its body. + Likewise a call on an owner that AGP's class loader cannot resolve fails when its name and descriptor are those of + a member a rule above changes or rejects in a renamed class another class can extend (not the final + `SQLiteDatabase`, `SQLiteQuery` or `SQLiteStatement`): the owner may inherit that member, and its renamed + ancestors are unknown. - 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 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 index 9d3f47775c..e378bd03f5 100644 --- 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 @@ -241,6 +241,27 @@ internal object SqliteRewriteRules { /** Names of the calls whose descriptor changes. */ val DESCRIPTOR_RULE_NAMES: Set = DESCRIPTOR_RULES.map { it.name }.toSet() + /** The renamed classes android.jar declares final: no other class extends them. */ + private val FINAL_CLASSES: Set = setOf(DATABASE, "${SQLITE_PKG}SQLiteQuery", "${SQLITE_PKG}SQLiteStatement") + + /** + * Name plus descriptor of each member that a rule changes or rejects and that another class can inherit (not a + * constructor, nor a member of a final class), mapped to the renamed class that declares it. A class the build + * cannot resolve may inherit one of these, so a call that matches one exactly cannot be checked. + */ + val INHERITABLE_RULE_MEMBERS: Map = + buildMap { + for ((key, descs) in MISSING_MEMBERS) { + val owner = key.substringBeforeLast('.') + val name = key.substringAfterLast('.') + if (owner in FINAL_CLASSES || name == "") continue + for (desc in descs) putIfAbsent(name + desc, owner) + } + for (rule in DESCRIPTOR_RULES) { + if (rule.owner !in FINAL_CLASSES) putIfAbsent(rule.name + rule.desc, rule.owner) + } + } + /** 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;" 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 044529031b..1a578bb487 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 @@ -271,14 +271,19 @@ object SqliteRewriter { private val ancestorsCache = HashMap>() private val superTypesCache = HashMap?>() - private fun superTypes(owner: String): Collection? = superTypesCache.getOrPut(owner) { hierarchy.superTypes(owner) } + // not getOrPut, which asks again for an owner that cannot be resolved (null) + private fun superTypes(owner: String): Collection? = + if (owner in superTypesCache) superTypesCache[owner] else hierarchy.superTypes(owner).also { superTypesCache[owner] = it } /** 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. */ + /** + * The renamed classes that [owner] is or extends, nearest first, so the rule tables see them all. None for an + * owner that cannot be resolved: [unresolvedOwner] reports the calls a rule could match on one. + */ fun renamedAncestors(owner: String): List { if (owner.startsWith("[")) return emptyList() return ancestorsCache.getOrPut(owner) { @@ -408,6 +413,27 @@ object SqliteRewriter { return renamedAncestors(owner) .firstOrNull { Rules.MISSING_MEMBERS["$it.$name"]?.contains(desc) == true } ?.let { "${simple(it)}.$name does not exist in requery" } + ?: unresolvedOwner(owner, name, desc) + } + + /** + * Why `owner.name desc` cannot be checked, or null: [owner] cannot be resolved, so its renamed ancestors are + * unknown, and the member has the name and descriptor of one a rule changes or rejects in a renamed class + * another class can extend. Like a Context database method on an unresolved owner, this fails under the + * full policy rather than pass a call that may break at run time. + */ + fun unresolvedOwner( + owner: String, + name: String, + desc: String, + ): String? { + val declaring = Rules.INHERITABLE_RULE_MEMBERS[name + desc] ?: return null + if (owner.startsWith("[") || owner in Rules.SQLITE_CLASSES || cannotBeSqlite(owner) || RewritePolicy.isExcluded(owner)) { + return null + } + if (superTypes(owner) != null) return null + return "${owner.replace('/', '.')} cannot be resolved, so the rewrite cannot tell whether its $name$desc is " + + "${simple(declaring)}.$name, which it changes or rejects" } fun typeInsn( @@ -503,6 +529,7 @@ object SqliteRewriter { "the platform method would not link", ) } + unresolvedOwner(owner, name, desc)?.let { return Action.Fail(it) } return Action.None } for (c in ancestors) { 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 e760dce5f7..6ab23eea42 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 @@ -535,13 +535,17 @@ class SqliteRewriterTest { ), ) val asked = mutableListOf() - SqliteRewriter.rewrite( - classes["p/Named"]!!, - ClassHierarchy { name -> - asked += name - null - }, - ) + try { + SqliteRewriter.rewrite( + classes["p/Named"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) + } catch (e: SqliteRewriteException) { + // getUniqueId on an owner that cannot be resolved is reported, which is not what this test is about + } assertThat(asked).containsExactly("p/Mine", "p/Other") } @@ -1429,4 +1433,49 @@ class SqliteRewriterTest { assertThat(overrides.single()).contains("Landroid/os/CancellationSignal;)Landroid/database/Cursor;'") assertThat(e.message).contains("p.MyBuilder") } + + @Test + fun `a call a rule could match on an owner the hierarchy cannot resolve fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesLost", + """ + package p; + import android.database.sqlite.*; + public class UsesLost { + void go(Lost l, SQLiteDatabase db, String[] a, android.os.CancellationSignal s) { + l.isStrict(); + l.query(db, a, "w", a, null, null, null, "1", s); + l.rawQuery("x", a); + l.delete("x"); + } + } + """, + ), + // left out of the hierarchy, as a class missing from the runtime classpath is + src( + "p.Lost", + """ + package p; + public class Lost extends android.database.sqlite.SQLiteQueryBuilder { + public android.database.Cursor rawQuery(String s, String[] a) { return null; } + public int delete(String s) { return 0; } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/UsesLost"]!!, hierarchy) } + val unresolved = + e.message!! + .lines() + .filter { "p.Lost cannot be resolved" in it } + .map { it.substringAfter("whether its ").substringBefore(" is ") } + // SQLiteDatabase is final, so no class inherits its rawQuery, and no rule has a delete(String) + assertThat(unresolved).hasSize(2) + assertThat(unresolved[0]).isEqualTo("isStrict()Z") + assertThat(unresolved[1]).startsWith("query(Landroid/database/sqlite/SQLiteDatabase;") + } } From 3bdeb36f78add77cfeaa69aca0bf68d3b4a6007d Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:10:00 -0800 Subject: [PATCH 33/56] ADFA-7138 Fail the build for an SQLite cursor used as an Android cursor class The cast rule caught `(AbstractCursor) c` only where javac writes a CHECKCAST. An upcast has none: `AbstractCursor up(SQLiteCursor c) { return c; }` returns the value as is. After the rename the parameter is requery's SQLiteCursor, which does not extend Android's AbstractCursor, so the class no longer verified. A second analysis now follows values whose static type is SQLiteCursor, or a class that extends it, and fails the build where one is used as AbstractWindowedCursor, AbstractCursor or CrossProcessCursor without a cast: returned, passed, called on, stored in a field, or held in a local or stack slot that a frame declares as one of them. The frames come from a new non-mutating fullFrames, which expandFrames now uses too. Conversions to android.database.Cursor and Object stay allowed. Values that pass through arrays are not followed; storing one into such an array throws ArrayStoreException at run time. The test also shows the failure mode: the same class, rewritten in a package the fail rule exempts, fails ASM's CheckClassAdapter and the JVM verifier. 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 | 155 +++++++++++++++++- .../gradle/sqlite/SqliteRewriterTest.kt | 49 ++++++ 3 files changed, 204 insertions(+), 3 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 8ab6558de8..623cb8a881 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -70,6 +70,9 @@ cannot load, so the transform runs only on projects that opted in. Commenting th `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`). + The same conversion without a cast fails too: a value declared `SQLiteCursor`, or a subclass, returned, passed, + called on, stored in a field, or held in a local as one of those three types (`AbstractCursor up(SQLiteCursor c) + { return c; }`). javac writes no `CHECKCAST` for it, and the renamed class would not verify. 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, with 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 1a578bb487..9d93aae8a0 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 @@ -136,6 +136,18 @@ object SqliteRewriter { override fun hashCode(): Int = super.hashCode() * 31 + if (sqliteCursor) 1 else 0 } + /** A reference whose static type, on some path, is SQLiteCursor or a subclass, which the rename makes requery's. */ + private class StaticCursorValue( + type: Type?, + val renamedCursor: Boolean, + ) : BasicValue(type) { + // as TaintValue: Frame.merge keeps the old value when the merged one equals it + override fun equals(other: Any?): Boolean = + other is BasicValue && super.equals(other) && renamedCursor == ((other as? StaticCursorValue)?.renamedCursor ?: false) + + override fun hashCode(): Int = super.hashCode() * 31 + if (renamedCursor) 1 else 0 + } + private class ClassRewrite( val cn: ClassNode, val policy: RewritePolicy, @@ -204,6 +216,26 @@ object SqliteRewriter { m: MethodNode, originalDesc: String, ) { + for (full in fullFrames(m, originalDesc)) { + full.node.type = Opcodes.F_NEW + full.node.local = ArrayList(full.locals) + full.node.stack = ArrayList(full.stack) + } + } + + /** A frame of a method with its locals and stack in full, as a compressed frame leaves them implied. */ + class FullFrame( + val node: FrameNode, + val locals: List, + val stack: List, + ) + + /** Every frame of [m] in full, starting from [originalDesc]'s parameters; changes nothing. */ + fun fullFrames( + m: MethodNode, + originalDesc: String, + ): List { + val result = ArrayList() var locals = ArrayList() if (m.access and Opcodes.ACC_STATIC == 0) locals += cn.name for (t in Type.getArgumentTypes(originalDesc)) { @@ -237,10 +269,9 @@ object SqliteRewriter { stack = ArrayList(frame.stack) } } - frame.type = Opcodes.F_NEW - frame.local = ArrayList(locals) - frame.stack = ArrayList(stack) + result += FullFrame(frame, ArrayList(locals), stack) } + return result } /** @@ -301,6 +332,7 @@ object SqliteRewriter { fun rewriteMethod(m: MethodNode) { val tainted = if (policy.fail) cursorCastTaint(m) else emptySet() + val conversions = if (policy.fail) implicitCursorConversions(m) else emptyMap() var line = -1 var insn: AbstractInsnNode? = m.instructions.first while (insn != null) { @@ -308,6 +340,7 @@ object SqliteRewriter { if (insn is LineNumberNode) { line = insn.line } + conversions[insn]?.let { violate(m, line, it) } when (insn) { is MethodInsnNode -> { methodInsn(m, insn, line) @@ -721,6 +754,122 @@ object SqliteRewriter { fun simple(name: String) = name.substringAfterLast('/') + /** SQLiteCursor, or a class that extends it, which the rename makes a subclass of requery's SQLiteCursor. */ + fun isRenamedCursorType(type: Type?): Boolean = + type != null && type.sort == Type.OBJECT && type.descriptor != "Lnull;" && + Rules.SQLITE_CURSOR in renamedAncestors(type.internalName) + + /** [type]'s internal name when it is one of the Android-only cursor types, or null. */ + private fun androidOnlyCursor(type: Type): String? = if (type.sort == Type.OBJECT) androidOnlyCursor(type.internalName) else null + + /** A type in a frame or an internal name, when it is one of the Android-only cursor types, or null. */ + private fun androidOnlyCursor(entry: Any?): String? = (entry as? String)?.takeIf { it in Rules.ANDROID_ONLY_CURSOR_TYPES } + + /** Whether [m] names an Android-only cursor type where a value could be converted to it without a cast. */ + private fun mayConvertToAndroidCursor(m: MethodNode): Boolean { + fun names(desc: String) = Rules.ANDROID_ONLY_CURSOR_TYPES.any { desc.contains("L$it;") } + if (names(Type.getReturnType(m.desc).descriptor)) return true + return m.instructions.any { insn -> + when (insn) { + is MethodInsnNode -> insn.owner in Rules.ANDROID_ONLY_CURSOR_TYPES || names(insn.desc) + is InvokeDynamicInsnNode -> names(insn.desc) + is FieldInsnNode -> names(insn.desc) + is FrameNode -> (insn.local.orEmpty() + insn.stack.orEmpty()).any { androidOnlyCursor(it) != null } + else -> false + } + } + } + + /** + * The instructions where a value whose static type is SQLiteCursor, or a class that extends it, is used as an + * AbstractWindowedCursor, AbstractCursor or CrossProcessCursor without a cast, by the message for each: returned, + * passed, called on, stored in a field, or held where a frame declares one of them. javac writes no CHECKCAST + * for such an upcast, and requery's SQLiteCursor neither extends those classes nor implements the interface, so + * the renamed class would not verify (or, for the interface, the call would fail). Values stored in or loaded + * from arrays are not followed; storing one into such an array throws ArrayStoreException. + */ + fun implicitCursorConversions(m: MethodNode): Map { + if (!mayConvertToAndroidCursor(m)) return emptyMap() + val interpreter = + object : BasicInterpreter(Opcodes.ASM9) { + override fun newValue(type: Type?): BasicValue? { + val v = super.newValue(type) ?: return null + return if (v.isReference) StaticCursorValue(v.type, isRenamedCursorType(type)) else v + } + + override fun merge( + value1: BasicValue, + value2: BasicValue, + ): BasicValue { + val renamed = + (value1 as? StaticCursorValue)?.renamedCursor == true || (value2 as? StaticCursorValue)?.renamedCursor == true + if (renamed && value1.type != null && value1.type == value2.type) return StaticCursorValue(value1.type, true) + return super.merge(value1, value2) + } + } + val frames: Array?> = + try { + Analyzer(interpreter).analyze(cn.name, m) + } catch (e: AnalyzerException) { + return emptyMap() + } + val declared = fullFrames(m, m.desc).associateBy { it.node } + val returnType = Type.getReturnType(m.desc) + val result = LinkedHashMap() + for ((i, insn) in m.instructions.withIndex()) { + val f = frames[i] ?: continue + + fun renamed(value: BasicValue?) = (value as? StaticCursorValue)?.renamedCursor == true + + fun fromTop(depth: Int) = if (depth < f.stackSize) f.getStack(f.stackSize - 1 - depth) else null + val target: String? = + when { + insn is MethodInsnNode || insn is InvokeDynamicInsnNode -> { + val args = Type.getArgumentTypes((insn as? MethodInsnNode)?.desc ?: (insn as InvokeDynamicInsnNode).desc) + args.indices.firstNotNullOfOrNull { k -> + androidOnlyCursor(args[k])?.takeIf { renamed(fromTop(args.size - 1 - k)) } + } + ?: (insn as? MethodInsnNode) + ?.takeIf { + it.opcode != Opcodes.INVOKESTATIC && it.owner in Rules.ANDROID_ONLY_CURSOR_TYPES && + renamed(fromTop(args.size)) + }?.owner + } + + insn is FieldInsnNode && (insn.opcode == Opcodes.PUTFIELD || insn.opcode == Opcodes.PUTSTATIC) -> { + androidOnlyCursor(Type.getType(insn.desc))?.takeIf { renamed(fromTop(0)) } + } + + insn.opcode == Opcodes.ARETURN -> { + androidOnlyCursor(returnType)?.takeIf { renamed(fromTop(0)) } + } + + insn is FrameNode -> { + declared[insn]?.let { full -> + var slot = 0 + var hit: String? = null + for (t in full.locals) { + if (hit == null && slot < f.locals && renamed(f.getLocal(slot))) hit = androidOnlyCursor(t) + slot += if (t == Opcodes.LONG || t == Opcodes.DOUBLE) 2 else 1 + } + hit ?: full.stack.indices.firstNotNullOfOrNull { k -> + androidOnlyCursor(full.stack[k])?.takeIf { k < f.stackSize && renamed(f.getStack(k)) } + } + } + } + + else -> { + null + } + } + if (target != null) { + result[insn] = "an SQLite cursor used as ${target.replace('/', '.')} without a cast; requery's SQLiteCursor is not " + + "one, so the rewritten class would not verify or the call would fail" + } + } + return result + } + /** * 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 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 6ab23eea42..f518d4787c 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 @@ -1478,4 +1478,53 @@ class SqliteRewriterTest { assertThat(unresolved[0]).isEqualTo("isStrict()Z") assertThat(unresolved[1]).startsWith("query(Landroid/database/sqlite/SQLiteDatabase;") } + + @Test + fun `an SQLite cursor used as an Android cursor class without a cast fails the build, and would not verify`() { + fun up(pkg: String) = + src( + "$pkg.Up", + """ + package $pkg; + import android.database.*; + import android.database.sqlite.*; + public class Up { + AbstractCursor field; + AbstractCursor returned(SQLiteCursor c) { return c; } + void passed(SQLiteCursor c) { take(c); } + static void take(AbstractWindowedCursor w) {} + void stored(SQLiteCursor c) { field = c; } + int calledOn(SQLiteCursor c) { + AbstractCursor a = c; + return a.getCount(); + } + Object inFrame(SQLiteCursor c, boolean b) { + AbstractCursor a = c; + if (b) { + a = null; + } + return a; + } + Cursor fine(SQLiteCursor c, Cursor other) { + Cursor k = c; + Object o = c; + AbstractCursor a = (AbstractCursor) other; + return k; + } + } + """, + ) + val classes = Compiler.compile(listOf(up("p"))) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Up"]!!, hierarchy) } + for (name in listOf("returned", "passed", "stored", "calledOn", "inFrame")) { + assertThat(e.message).contains(" - $name(") + } + assertThat(e.message).contains("an SQLite cursor used as android.database.AbstractWindowedCursor without a cast") + assertThat(e.message).contains("an SQLite cursor used as android.database.AbstractCursor without a cast") + assertThat(e.message).doesNotContain("fine(") + // The failure mode: rewritten in a package the fail rule exempts, the same class does not verify + val unverified = assertThrows(IllegalStateException::class.java) { transform(up("androidx.sqlite.db.framework")) } + assertThat(unverified.message).contains("does not verify") + assertThat(unverified.message).contains("androidx/sqlite/db/framework/Up") + } } From 183a69f2a1811a951110db29f9ffa72e82468861 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:10:26 -0800 Subject: [PATCH 34/56] ADFA-7138 Check the SQLite debugger opt-in again after the model wait, always The second refusal check ran only when the model was missing at the first one (modulesKnown). A sync during the staging or the wait can replace a published model, and a module it adds can carry cogo.sqliteDebugger=true in its own gradle.properties. That project then reached Gradle and ended in "variant setup missing" instead of the refusal. The check after the wait now always runs, and modulesKnown is gone. It reads only small property files and returns early without an opt-in. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 7 ++--- ...dleQuickBuildProvisionerFailureArmsTest.kt | 30 +++++++++++++++++++ .../GradleQuickBuildProvisionerFakes.kt | 21 +++++++++++++ 3 files changed, 54 insertions(+), 4 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 452fed5594..5e97d2d18c 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -365,7 +365,6 @@ class GradleQuickBuildProvisioner( // Build to an opted-in project, so building would only end in "variant setup missing". During // a sync the model has no app modules yet, so an opt-in in a module's own gradle.properties is // seen only by the second check, after the wait for the model. - val modulesKnown = projectManager.workspace != null sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } stage(context, paths) @@ -379,9 +378,9 @@ class GradleQuickBuildProvisioner( context.getString(R.string.quick_build_waiting_for_sync), ) } - if (!modulesKnown) { - sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } - } + // Again, whatever the first check saw: a sync during the staging or the wait can publish modules, and + // one of them can carry the opt-in in its own gradle.properties. + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } val module = quickBuildModule(projectManager) 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 d7b1655fcc..8b82af6c25 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -118,6 +118,36 @@ class GradleQuickBuildProvisionerFailureArmsTest { assertThat(gradle.executedTasks).isNull() } + @Test + fun `an opt-in in a module that a sync publishes during the staging is refused`() = + runTest { + val app = moduleDir() + val feature = File(projectRoot, "feature").apply { mkdirs() } + File(feature, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val appModule = androidModule(buildDir = File(app, "build"), projectDir = app) + val featureModule = androidModule(path = ":feature", buildDir = File(feature, "build"), projectDir = feature) + val gradle = FakeBuildService() + val project = + ResyncingProjectManager( + FakeProjectManager(projectRoot, appModules = listOf(appModule)), + FakeProjectManager(projectRoot, appModules = listOf(appModule, featureModule)), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + // The model was already published, so the first check read the old modules; the module with the + // opt-in appears only after it. + stage = { _, _ -> project.resynced = true }, + ).provision() + + assertThat(failureText(outcome)).isEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + 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/GradleQuickBuildProvisionerFakes.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt index eb6e4c9022..1ce6bd7b00 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFakes.kt @@ -150,6 +150,27 @@ internal class SyncingProjectManager( override fun getAndroidAppModules(): List = if (published) synced.getAndroidAppModules() else emptyList() } +/** + * A project whose model is published from the start, and replaced by a sync that runs while Quick Build stages: + * [before]'s modules until [resynced] is set, [synced]'s after. + */ +internal class ResyncingProjectManager( + private val before: FakeProjectManager, + private val synced: FakeProjectManager, +) : IProjectManager by synced { + var resynced = false + + private val current: FakeProjectManager + get() = if (resynced) synced else before + + override val workspace: Workspace? + get() = current.workspace + + override fun getAndroidModules(): List = current.getAndroidModules() + + override fun getAndroidAppModules(): List = current.getAndroidAppModules() +} + /** * An Android module as the Build Variants sidebar presents it. * From 1648b5a2670df4828bfade8fe189646894f6a719 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:10:41 -0800 Subject: [PATCH 35/56] ADFA-7138 Do not refuse Quick Build from the root opt-in before the modules are known The early refusal check ran before the wait for the model. During a sync it saw no app modules, so it read the Gradle user home and the root only, and a root cogo.sqliteDebugger=true refused at once, even when the app module sets false. The documented rule, that the first file that defines the line decides and a module's file comes before the root's, held only after the sync. The early check now runs only once the model is published. During a sync the check after the wait, which always runs, decides with the modules' files. The cost: a root opt-in tapped during a sync now pays for staging before it is refused. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../quickbuild/GradleQuickBuildProvisioner.kt | 13 +++++---- ...dleQuickBuildProvisionerFailureArmsTest.kt | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 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 5e97d2d18c..03aed21800 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisioner.kt @@ -362,10 +362,13 @@ class GradleQuickBuildProvisioner( val projectRoot = File(projectManager.projectDirPath) // Before staging, which a refused build need not pay for: the Gradle plugin never applies Quick - // Build to an opted-in project, so building would only end in "variant setup missing". During - // a sync the model has no app modules yet, so an opt-in in a module's own gradle.properties is - // seen only by the second check, after the wait for the model. - sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } + // Build to an opted-in project, so building would only end in "variant setup missing". Only once + // the model has the app modules: during a sync this check would read the root's file without + // theirs, so a root opt-in would refuse a project whose module turns it off. The second check, + // after the wait for the model, decides then. + if (projectManager.workspace != null) { + sqliteDebuggerRefusal(projectManager, projectRoot)?.let { return it } + } stage(context, paths) // The project model only exists once CoGo's Gradle sync has populated it, and a tap @@ -555,7 +558,7 @@ class GradleQuickBuildProvisioner( /** * The refusal for a project with `cogo.sqliteDebugger=true`, or null. It reads the app modules' * own `gradle.properties` only for the modules [projectManager] knows, which is none before the - * sync publishes the model. + * sync publishes the model, so it decides only once the model is published. */ private fun sqliteDebuggerRefusal( projectManager: IProjectManager, 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 8b82af6c25..987a2f03c5 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/GradleQuickBuildProvisionerFailureArmsTest.kt @@ -118,6 +118,35 @@ class GradleQuickBuildProvisionerFailureArmsTest { assertThat(gradle.executedTasks).isNull() } + @Test + fun `a root opt-in that the app module turns off is not refused when the tap comes during the sync`() = + runTest { + File(projectRoot, "gradle.properties").writeText("cogo.sqliteDebugger=true\n") + val moduleDir = moduleDir() + File(moduleDir, "gradle.properties").writeText("cogo.sqliteDebugger=false\n") + val gradle = FakeBuildService() + val project = + SyncingProjectManager( + FakeProjectManager( + projectRoot, + appModules = listOf(androidModule(buildDir = File(moduleDir, "build"), projectDir = moduleDir)), + ), + ) + + val outcome = + testProvisioner( + context = context, + projectRoot = projectRoot, + buildService = gradle, + projectManager = project, + stage = { _, _ -> project.published = true }, + ).provision() + + // The module's file comes before the root's, so its false decides, as in Gradle: the build runs. + assertThat(failureText(outcome)).isNotEqualTo(context.getString(R.string.quick_build_disabled_sqlite_debugger)) + assertThat(gradle.executedTasks).isNotNull() + } + @Test fun `an opt-in in a module that a sync publishes during the staging is refused`() = runTest { From 625924675cd61c12f9a55455a30ae8bb643bcbc7 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:10:53 -0800 Subject: [PATCH 36/56] ADFA-7138 Replace the SQLite AARs in one rename when app init extracts them ToolsManager.init extracts sqlite-debugger.aar and sqlite-rewrite-runtime.aar on a background thread, and GradleBuildService passes their paths to a build without waiting. Each extractor deleted the AAR and then copied straight to its path, so a build of an opted-in project that started meanwhile could find the file missing ("not found", with the wrong advice to remove the opt-in) or read it half-written. Both now go through replaceAtomically: copy to a .part sibling, then rename over the final path, as the tooling jar already does. A build sees the previous complete AAR or the new one. This is smaller than making builds wait for extraction. The one window left is a fresh install's very first extraction: a build that early finds no file and says so. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../androidide/managers/ToolsManager.java | 43 +++++++----- .../managers/ToolsManagerSqliteAarTest.kt | 66 +++++++++++++++++++ 2 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt 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 b097f8e08c..12376129cb 100755 --- a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java +++ b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java @@ -210,6 +210,23 @@ static boolean isToolingJarCurrent(File toolingJarFile, File stampFile, String s return toolingJarFile.isFile() && stamp != null && stamp.equals(readStampFile(stampFile)); } + /** + * Copies the stream to a temp sibling of the target, then renames it over the target. {@link #init} extracts the SQLite AARs in the background, and GradleBuildService passes their paths to a build without waiting, so a build that starts meanwhile must find the previous complete file or the new one, never a missing or partly written one. rename(2) within one directory atomically replaces the target on Linux. Does not close the stream. + */ + @VisibleForTesting + static void replaceAtomically(InputStream source, File target) throws IOException { + final var dir = Objects.requireNonNull(target.getParentFile()); + dir.mkdirs(); + final var tempFile = new File(dir, target.getName() + ".part"); + try (final var fos = new FileOutputStream(tempFile)) { + IoUtilsKt.transferToStream(source, fos); + } + if (!tempFile.renameTo(target)) { + tempFile.delete(); + throw new IOException("Could not move " + tempFile + " to " + target); + } + } + private static void deleteIdeenv() { final var file = new File(Environment.BIN_DIR, "ideenv"); if (file.exists() && !file.delete()) { @@ -263,30 +280,24 @@ 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); + private static void extractSqliteAar(String asset, File aar) { + try (InputStream source = BaseApplication.getBaseInstance().getAssets().open(asset)) { + replaceAtomically(source, aar); + } catch (IOException err) { + LOG.error("Failed to extract {} to {}", asset, aar, err); } + } - Environment.mkdirIfNotExists(Environment.SQLITE_DEBUGGER_DIR); - + @WorkerThread + private static void extractSqliteDebugger() { // One AAR with both ABIs, unlike logsender's per-flavor AARs - ResourceUtils.copyFileFromAssets(getCommonAsset("sqlite-debugger-release.aar"), - Environment.SQLITE_DEBUGGER_AAR.getAbsolutePath()); + extractSqliteAar(getCommonAsset("sqlite-debugger-release.aar"), Environment.SQLITE_DEBUGGER_AAR); } @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()); + extractSqliteAar(getCommonAsset("sqlite-rewrite-runtime-release.aar"), Environment.SQLITE_REWRITE_RUNTIME_AAR); } private static void generateKeystore() { diff --git a/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt new file mode 100644 index 0000000000..9c4a85d177 --- /dev/null +++ b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt @@ -0,0 +1,66 @@ +package com.itsaky.androidide.managers + +import com.google.common.truth.Truth.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File +import java.io.InputStream + +/** + * App init extracts the SQLite debugger and rewrite runtime AARs in the background, and a build gets their paths + * without waiting, so [ToolsManager.replaceAtomically] must never leave a missing or partly written AAR at the path. + */ +class ToolsManagerSqliteAarTest { + @get:Rule + val tempFolder = TemporaryFolder() + + /** Hands out one byte per read, and records what a build reading [target] would find before each one. */ + private class WatchingStream( + private val bytes: ByteArray, + private val target: File, + ) : InputStream() { + val seen = mutableListOf() + private var next = 0 + + override fun read(): Int { + seen += if (target.isFile) target.readText() else null + return if (next < bytes.size) bytes[next++].toInt() and 0xff else -1 + } + + override fun read( + b: ByteArray, + off: Int, + len: Int, + ): Int { + if (len == 0) return 0 + val c = read() + if (c < 0) return -1 + b[off] = c.toByte() + return 1 + } + } + + @Test + fun `a build that reads the AAR while it is replaced finds the previous complete file`() { + val aar = File(tempFolder.root, "sqlite-debugger/sqlite-debugger.aar") + aar.parentFile!!.mkdirs() + aar.writeText("previous-install-aar") + val source = WatchingStream("this-install-aar".toByteArray(), aar) + + ToolsManager.replaceAtomically(source, aar) + + assertThat(source.seen.toSet()).containsExactly("previous-install-aar") + assertThat(aar.readText()).isEqualTo("this-install-aar") + assertThat(File(aar.parentFile, "sqlite-debugger.aar.part").exists()).isFalse() + } + + @Test + fun `the first extraction creates the directory and the AAR`() { + val aar = File(tempFolder.root, "sqlite-rewrite-runtime/sqlite-rewrite-runtime.aar") + + ToolsManager.replaceAtomically("runtime-aar".byteInputStream(), aar) + + assertThat(aar.readText()).isEqualTo("runtime-aar") + } +} From 72e44e8f50f6d55178d665b0de6d0d89dbaa0acc Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:58 -0800 Subject: [PATCH 37/56] ADFA-7138 Accept an instanceof guard only when it holds on every path to the cast The guard check was syntactic: an INSTANCEOF of the cast's type on the same local, followed by an IFEQ whose target lies past the cast, counted. In `if (b || c instanceof AbstractCursor) return (AbstractCursor) c;` that holds, yet b alone reaches the cast, which then throws on requery's cursor. The cast analysis now proves the guard itself. A Frame subclass sees `ALOAD n; INSTANCEOF X; IFEQ/IFNE` written back to back (no label, so no path joins in between) and, through Frame.initJumpTarget, gives local n's value the proof on the edge where the test succeeded only. The proof travels with the value through copies and casts, and a merge keeps it only when every path brings it. A store to the local replaces the value, so it drops the proof. The simple, &&, ?: and early-exit guards still pass, and a guard on a copy of the local now counts too. The syntactic check and its helpers are gone. 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 | 161 ++++++++++-------- .../gradle/sqlite/SqliteRewriterTest.kt | 68 ++++++++ 3 files changed, 159 insertions(+), 73 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 623cb8a881..17d3f3cbfe 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -74,7 +74,8 @@ cannot load, so the transform runs only on projects that opted in. Commenting th called on, stored in a field, or held in a local as one of those three types (`AbstractCursor up(SQLiteCursor c) { return c; }`). javac writes no `CHECKCAST` for it, and the renamed class would not verify. 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 + or an early return when it is false) is not reported when the guard holds on every path to the cast: in + `if (b || c instanceof X) ((X) c)` the cast is reached with `b` alone, so it is 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, 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 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 9d93aae8a0..13d14f2668 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 @@ -18,7 +18,7 @@ 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.LabelNode import org.objectweb.asm.tree.LdcInsnNode import org.objectweb.asm.tree.LineNumberNode import org.objectweb.asm.tree.MethodInsnNode @@ -30,6 +30,7 @@ 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 org.objectweb.asm.tree.analysis.Interpreter 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. */ @@ -125,15 +126,64 @@ object SqliteRewriter { } } + /** [instanceOf]: the types an `instanceof` proved the value to be, on every path to here. */ private class TaintValue( type: Type?, val sqliteCursor: Boolean, + val instanceOf: Set = emptySet(), ) : BasicValue(type) { - // Frame.merge keeps the old value when the merged one equals it, so the taint must count + // Frame.merge keeps the old value when the merged one equals it, so the taint and the proofs must count override fun equals(other: Any?): Boolean = - other is BasicValue && super.equals(other) && sqliteCursor == ((other as? TaintValue)?.sqliteCursor ?: false) + other is BasicValue && super.equals(other) && sqliteCursor == ((other as? TaintValue)?.sqliteCursor ?: false) && + instanceOf == ((other as? TaintValue)?.instanceOf ?: emptySet()) - override fun hashCode(): Int = super.hashCode() * 31 + if (sqliteCursor) 1 else 0 + override fun hashCode(): Int = (super.hashCode() * 31 + if (sqliteCursor) 1 else 0) * 31 + instanceOf.hashCode() + + fun provedInstanceOf(type: String) = TaintValue(this.type, sqliteCursor, instanceOf + type) + } + + /** + * A frame that learns from `ALOAD n; INSTANCEOF X; IFEQ/IFNE`, written without anything between them (no label, so + * no other path joins there): on the edge where the test succeeded, local n's value is proved an X. The proof goes + * with the value through copies, and a merge keeps it only when every path brings it. + */ + private class GuardFrame : Frame { + constructor(numLocals: Int, maxStack: Int) : super(numLocals, maxStack) + constructor(frame: Frame) : super(frame) + + private var local = -1 + private var type: String? = null + private var provedOnJump = false + private var before: BasicValue? = null + + override fun execute( + insn: AbstractInsnNode, + interpreter: Interpreter, + ) { + local = -1 + before = null + val test = insn.previous as? TypeInsnNode + val load = test?.previous as? VarInsnNode + if ((insn.opcode == Opcodes.IFEQ || insn.opcode == Opcodes.IFNE) && test?.opcode == Opcodes.INSTANCEOF && + load?.opcode == Opcodes.ALOAD + ) { + local = load.`var` + type = test.desc + provedOnJump = insn.opcode == Opcodes.IFNE + } + super.execute(insn, interpreter) + } + + // Called on this frame once per edge of the jump, before it is merged there, in no order this relies on + override fun initJumpTarget( + opcode: Int, + target: LabelNode?, + ) { + if (local < 0) return + val value = before ?: getLocal(local).also { before = it } + val proved = (target != null) == provedOnJump + setLocal(local, if (proved && value is TaintValue) value.provedInstanceOf(type!!) else value) + } } /** A reference whose static type, on some path, is SQLiteCursor or a subclass, which the rename makes requery's. */ @@ -478,9 +528,7 @@ object SqliteRewriter { 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) - ) { + if (t.opcode == Opcodes.CHECKCAST && t.desc in Rules.ANDROID_ONLY_CURSOR_TYPES && t in tainted) { violate( m, line, @@ -693,63 +741,6 @@ object SqliteRewriter { ) = 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('/') @@ -873,8 +864,11 @@ object SqliteRewriter { /** * 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). + * SQLiteCursor), through locals and the stack, unless an `instanceof` of the cast's type proved + * the operand on every path to the cast (see [GuardFrame]): `if (c instanceof X) ((X) c)`, `&&`, + * `?:` and an early exit when it is false, but not `b || c instanceof X`, which reaches the cast + * with `b` alone. Approximate: values that went through other calls, array elements or static + * fields are not followed (a miss). */ fun cursorCastTaint(m: MethodNode): Set { val casts = @@ -914,7 +908,12 @@ object SqliteRewriter { } 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) + val operand = value as? TaintValue + return TaintValue( + r.type, + operand?.sqliteCursor == true || castTo == Rules.SQLITE_CURSOR, + operand?.instanceOf.orEmpty(), + ) } return r } @@ -940,13 +939,30 @@ object SqliteRewriter { 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) + val t1 = value1 as? TaintValue + val t2 = value2 as? TaintValue + // BasicInterpreter.merge answers UNINITIALIZED for values that differ, taint and proofs included + if ((t1 != null || t2 != null) && value1.type != null && value1.type == value2.type) { + val merged = + TaintValue( + value1.type, + t1?.sqliteCursor == true || t2?.sqliteCursor == true, + t1?.instanceOf.orEmpty() intersect t2?.instanceOf.orEmpty(), + ) + return if (merged == value1) value1 else merged + } return super.merge(value1, value2) } } - val analyzer = Analyzer(interpreter) + val analyzer = + object : Analyzer(interpreter) { + override fun newFrame( + numLocals: Int, + numStack: Int, + ): Frame = GuardFrame(numLocals, numStack) + + override fun newFrame(frame: Frame): Frame = GuardFrame(frame) + } val frames: Array?> = try { analyzer.analyze(cn.name, m) @@ -957,7 +973,8 @@ object SqliteRewriter { return casts .filter { cast -> val frame = frames[insns.indexOf(cast)] - frame != null && (frame.getStack(frame.stackSize - 1) as? TaintValue)?.sqliteCursor == true + val operand = frame?.let { it.getStack(it.stackSize - 1) } as? TaintValue + operand != null && operand.sqliteCursor && (cast as TypeInsnNode).desc !in operand.instanceOf }.toSet() } } 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 f518d4787c..276264bba9 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 @@ -1527,4 +1527,72 @@ class SqliteRewriterTest { assertThat(unverified.message).contains("does not verify") assertThat(unverified.message).contains("androidx/sqlite/db/framework/Up") } + + @Test + fun `an instanceof guard counts only when it covers every path to the cast`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Partly", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Partly { + Object or(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (b || c instanceof AbstractCursor) { + return (AbstractCursor) c; + } + return null; + } + Object beforeTheLoop(SQLiteDatabase db, int n) { + Cursor c = db.rawQuery("x", null); + for (int i = 0; i < n; i++) { + if (!(c instanceof AbstractWindowedCursor)) { + return null; + } + c = db.rawQuery("y", null); + } + return (AbstractWindowedCursor) c; + } + } + """, + ), + src( + "p.Everywhere", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Everywhere { + Object andAfter(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (c instanceof AbstractCursor && b) { + return (AbstractCursor) c; + } + return null; + } + Object onBothBranches(SQLiteDatabase db, boolean b) { + Cursor c = db.rawQuery("x", null); + if (b) { + if (!(c instanceof AbstractWindowedCursor)) return null; + } else { + if (!(c instanceof AbstractWindowedCursor)) return null; + } + Cursor copy = c; + return (AbstractWindowedCursor) copy; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Partly"]!!, hierarchy) } + // b alone reaches the cast in or; the loop may run zero times, or end on the unchecked cursor + assertThat(e.message).contains(" - or(") + assertThat(e.message).contains(" - beforeTheLoop(") + SqliteRewriter.rewrite(classes["p/Everywhere"]!!, hierarchy) + } } From abf700266c86c7dff520a3bf21ee37b477ee160e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:58 -0800 Subject: [PATCH 38/56] ADFA-7138 Cancel every query that shares an Android CancellationSignal toCancellationSignal registered a new listener on Android's signal for each call, and Android's signal holds one listener, so the second registration replaced the first. requery keeps a query's signal for its cursor's later reads, so with two lazy queries on one Android signal, cancelling it left the first cursor's signal uncancelled. Each Android signal now gets one forwarding listener, kept in a WeakHashMap, that cancels every requery signal converted from it. It holds them weakly and drops cleared ones on each call, so a closed and dropped cursor is not kept for a signal the app reuses. Because setOnCancelListener does not call a listener that is already set, a signal cancelled before the call is checked directly. The runtime test replaces android.jar's stub of the signal with a copy of Android's listener rules, to run the conversion on the JVM. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 4 +- .../gradle/sqlite/SqliteRewriteRuntimeTest.kt | 82 ++++++++++++++++++- .../sqliterewrite/SqliteRewriteRuntime.java | 63 ++++++++++++-- 3 files changed, 136 insertions(+), 13 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 17d3f3cbfe..883865954b 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -213,6 +213,8 @@ dependency, and runs the script on both APKs. The release must pass. The debug A - `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. +- Android's `CancellationSignal` holds one listener: the conversion replaces any listener the caller set. Each + query gets its own androidx.core signal, and cancelling the Android signal cancels every one still in use, so + two lazy queries that share it both stop (requery keeps a query's signal for its cursor's later reads). - 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/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 index e8e892d959..02324e9e6c 100644 --- 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 @@ -1,12 +1,16 @@ package com.itsaky.androidide.gradle.sqlite import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage 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. */ +/** + * The runtime helper's functions that need no device, run on the JVM from its bytecode compiled against android.jar, + * with Android's CancellationSignal replaced by a copy of its listener logic (android.jar's methods only throw). + */ class SqliteRewriteRuntimeTest { - private val runtime: Class<*> by lazy { + private val loader: ClassLoader by lazy { val source = FileProvider .projectRoot() @@ -15,12 +19,29 @@ class SqliteRewriteRuntimeTest { .readText() val classes = Compiler.compile( - listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", source)), + listOf( + JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", source), + JavaSource("android.os.CancellationSignal", ANDROID_SIGNAL), + ), listOf(TestJars.androidJar, TestJars.androidxCoreJar), ) - BytecodeVerifier.loader(classes).loadClass("org.appdevforall.sqliterewrite.SqliteRewriteRuntime") + BytecodeVerifier.loader(classes) } + private val runtime: Class<*> by lazy { loader.loadClass("org.appdevforall.sqliterewrite.SqliteRewriteRuntime") } + + private val androidSignal: Class<*> by lazy { loader.loadClass("android.os.CancellationSignal") } + + private fun newAndroidSignal(): Any = androidSignal.getConstructor().newInstance() + + private fun convert(signal: Any): Any = runtime.getMethod("toCancellationSignal", androidSignal).invoke(null, signal) + + private fun cancel(signal: Any) { + signal.javaClass.getMethod("cancel").invoke(signal) + } + + private fun isCanceled(signal: Any) = signal.javaClass.getMethod("isCanceled").invoke(signal) as Boolean + private fun translate(flags: Int) = runtime.getMethod("translateOpenFlags", Int::class.javaPrimitiveType).invoke(null, flags) as Int private val readOnly = 1 @@ -45,4 +66,57 @@ class SqliteRewriteRuntimeTest { assertThat(translate(16)).isEqualTo(2) assertThat(translate(8 or createIfNecessary)).isEqualTo(6) } + + @Test + fun `two queries that share an Android signal are both cancelled with it`() { + val signal = newAndroidSignal() + // requery keeps each query's signal for its cursor's later reads + val first = convert(signal) + val second = convert(signal) + + cancel(signal) + + assertWithMessage("the first query's signal").that(isCanceled(first)).isTrue() + assertWithMessage("the second query's signal").that(isCanceled(second)).isTrue() + } + + @Test + fun `a query given a signal that is already cancelled gets a cancelled one`() { + val signal = newAndroidSignal() + convert(signal) + cancel(signal) + + assertThat(isCanceled(convert(signal))).isTrue() + } + + private companion object { + /** Android's listener rules: one listener, a new one runs at once on a cancelled signal, the same one does not. */ + val ANDROID_SIGNAL = + """ + package android.os; + public final class CancellationSignal { + private boolean canceled; + private OnCancelListener listener; + public boolean isCanceled() { synchronized (this) { return canceled; } } + public void cancel() { + OnCancelListener l; + synchronized (this) { + if (canceled) return; + canceled = true; + l = listener; + } + if (l != null) l.onCancel(); + } + public void setOnCancelListener(OnCancelListener l) { + synchronized (this) { + if (listener == l) return; + listener = l; + if (!canceled || l == null) return; + } + l.onCancel(); + } + public interface OnCancelListener { void onCancel(); } + } + """.trimIndent() + } } 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 index 8e810f980f..9fc44c855a 100644 --- 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 @@ -8,6 +8,11 @@ import android.text.TextUtils; import java.io.File; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; /** * Targets of the calls that CoGo's Gradle plugin redirects when it rewrites an app from android.database.sqlite to requery (ADFA-7138). @@ -41,6 +46,9 @@ public final class SqliteRewriteRuntime { private static final int CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x8; + /** The listener each Android signal forwards cancellation through; weak keys, so a signal the app dropped goes. */ + private static final Map FORWARDERS = new WeakHashMap<>(); + /** * 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. */ @@ -132,7 +140,7 @@ public static String stringForQuery(SQLiteStatement prog, String[] selectionArgs } /** - * 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. + * Android's signal for requery's. Each call returns a new requery signal, and cancelling Android's cancels every one of them still in use: requery keeps a query's signal for the cursor's later reads, so two lazy queries that share an Android signal must both stop. 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). */ public static androidx.core.os.CancellationSignal toCancellationSignal( CancellationSignal signal) { @@ -140,16 +148,21 @@ public static androidx.core.os.CancellationSignal toCancellationSignal( return null; } final androidx.core.os.CancellationSignal converted = new androidx.core.os.CancellationSignal(); + CancelForwarder forwarder; + synchronized (FORWARDERS) { + forwarder = FORWARDERS.get(signal); + if (forwarder == null) { + forwarder = new CancelForwarder(); + FORWARDERS.put(signal, forwarder); + } + } + forwarder.add(converted); + signal.setOnCancelListener(forwarder); + // setOnCancelListener does not call a listener that is already set, so a signal cancelled before this call + // has to be checked if (signal.isCanceled()) { converted.cancel(); - return converted; } - signal.setOnCancelListener(new CancellationSignal.OnCancelListener() { - @Override - public void onCancel() { - converted.cancel(); - } - }); return converted; } @@ -168,4 +181,38 @@ public static int translateOpenFlags(int androidFlags) { } private SqliteRewriteRuntime() {} + + /** + * Cancels every requery signal converted from one Android signal. It holds them weakly: a query's signal lives as long as its cursor, and a closed and dropped cursor must not be kept for a signal the app reuses. + */ + private static final class CancelForwarder implements CancellationSignal.OnCancelListener { + private final List> converted = new ArrayList<>(); + + @Override + public void onCancel() { + List live = new ArrayList<>(); + synchronized (this) { + for (WeakReference ref : converted) { + androidx.core.os.CancellationSignal signal = ref.get(); + if (signal != null) { + live.add(signal); + } + } + converted.clear(); + } + // outside the lock: requery's listener interrupts the native query + for (androidx.core.os.CancellationSignal signal : live) { + signal.cancel(); + } + } + + synchronized void add(androidx.core.os.CancellationSignal signal) { + for (int i = converted.size() - 1; i >= 0; i--) { + if (converted.get(i).get() == null) { + converted.remove(i); + } + } + converted.add(new WeakReference<>(signal)); + } + } } From 68bf024d93e0a73ce3696c1c29a9e37d02b63355 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:58 -0800 Subject: [PATCH 39/56] ADFA-7138 Docs: say a cancelled query throws androidx.core's OperationCanceledException The rewrite converts Android's CancellationSignal to androidx.core's, and requery throws androidx.core.os.OperationCanceledException for a cancelled query, from its Java check and from its native layer (checked with javap and in the 3.49.0 .so). That class does not extend Android's, and catch clauses are not rewritten, so a catch of android.os.OperationCanceledException no longer catches the cancellation. The docs listed this among the unverified review items. It is now a known limit of its own, with the multi-catch an app can use with the SQLite debugger on and off, and the rewriter's comment at the conversion says so. Docs and a comment only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 10 ++++++++-- .../itsaky/androidide/gradle/sqlite/SqliteRewriter.kt | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 883865954b..1307fa4951 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -184,8 +184,7 @@ dependency, and runs the script on both APKs. The release must pass. The debug A `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 + one connection only; Android turns write-ahead logging off on the first `ATTACH`); 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 @@ -213,6 +212,13 @@ dependency, and runs the script on both APKs. The release must pass. The debug A - `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. +- A cancelled query throws `androidx.core.os.OperationCanceledException`, not + `android.os.OperationCanceledException`: requery checks androidx.core's signal, which the rewrite converts Android's + to, and its native layer throws the same class (checked with javap and in requery 3.49.0's `.so`). The two classes + are unrelated, and the rewrite does not change `catch` clauses, so a `catch (android.os.OperationCanceledException e)` + around a query, or around reading its cursor, no longer catches a cancellation, which reaches the caller instead. An + app that catches it should catch both, `catch (android.os.OperationCanceledException | + androidx.core.os.OperationCanceledException e)`, which works with the SQLite debugger on and off. - Android's `CancellationSignal` holds one listener: the conversion replaces any listener the caller set. Each query gets its own androidx.core signal, and cancelling the Android signal cancels every one still in use, so two lazy queries that share it both stop (requery keeps a query's signal for its cursor's later reads). 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 13d14f2668..67dfcbe378 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 @@ -655,6 +655,8 @@ object SqliteRewriter { is Action.Retype -> { if (action.signal) { + // requery checks androidx.core's signal, so a cancelled query throws androidx.core's + // OperationCanceledException, which a catch of Android's does not catch (see the docs) m.instructions.insertBefore( mi, MethodInsnNode(Opcodes.INVOKESTATIC, Rules.HELPER, "toCancellationSignal", Rules.HELPER_SIGNAL_DESC, false), From ade142c7d940ab80dfe9e5891734a85cdbc8858e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:58 -0800 Subject: [PATCH 40/56] ADFA-7138 Look up the SQLite rewrite tests' jars in the active Gradle user home TestJars searched ~/.gradle/caches only. With GRADLE_USER_HOME or --gradle-user-home pointing elsewhere, the tests skipped locally, or failed on CI, although the jars were in the active cache. Both test tasks now pass gradle.gradleUserHomeDir as cogo.test.gradleUserHome, as a JVM argument rather than an input, so the per-machine path does not change the task's cache key. gradleUserHome() reads it, then GRADLE_USER_HOME, then falls back to ~/.gradle, as when the tests run from an IDE. Test code and build script only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/build.gradle.kts | 10 +++++++ .../gradle/sqlite/SqliteTestSupport.kt | 15 +++++++++-- .../gradle/sqlite/SqliteTestSupportTest.kt | 26 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index 96cee1492b..f8f9159f7e 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -63,11 +63,20 @@ fun Test.stageMavenLocalRepos() { } } +// The SQLite rewrite tests find the jars they compile against in the cache of this build's Gradle user home, which +// GRADLE_USER_HOME or --gradle-user-home can move from ~/.gradle. An argument, not an input: the path is per machine. +val gradleUserHomePath: String = gradle.gradleUserHomeDir.absolutePath + +fun Test.passGradleUserHome() { + jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-Dcogo.test.gradleUserHome=$gradleUserHomePath") }) +} + val sqliteReleaseApkTestClass = "com.itsaky.androidide.gradle.SqliteReleaseApkTest" tasks.named("test") { useJUnitPlatform() stageMavenLocalRepos() + passGradleUserHome() // Run by sqliteReleaseApkTest below, which builds the AARs it needs filter { excludeTestsMatching(sqliteReleaseApkTestClass) } } @@ -103,6 +112,7 @@ tasks.register("sqliteReleaseApkTest") { classpath = testSourceSet.runtimeClasspath useJUnitPlatform() stageMavenLocalRepos() + passGradleUserHome() filter { includeTestsMatching(sqliteReleaseApkTestClass) } // The tasks AndroidIDEAssetsPlugin bundles these AARs with, so this task and :app:assemble build them once 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 index fd9e8a91eb..6d677ea7e5 100644 --- 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 @@ -28,9 +28,20 @@ internal fun requireEnvironment( assumeTrue(false, missing) } +/** + * The Gradle user home whose cache holds the jars: the one of the build that runs the tests, which the test tasks pass + * as cogo.test.gradleUserHome, then GRADLE_USER_HOME, then ~/.gradle, as from an IDE. + */ +internal fun gradleUserHome( + property: String? = System.getProperty("cogo.test.gradleUserHome"), + env: String? = System.getenv("GRADLE_USER_HOME"), + userHome: File = File(System.getProperty("user.home")), +): File = property?.takeIf { it.isNotEmpty() }?.let(::File) ?: env?.takeIf { it.isNotEmpty() }?.let(::File) ?: File(userHome, ".gradle") + /** 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")) + private val modules by lazy { File(gradleUserHome(), "caches/modules-2/files-2.1") } val androidJar: File by lazy { val sdkRoots = @@ -59,7 +70,7 @@ internal object TestJars { version: String, file: String, ): File? = - File(home, ".gradle/caches/modules-2/files-2.1/$group/$artifact/$version") + File(modules, "$group/$artifact/$version") .walkTopDown() .firstOrNull { it.name == file } @@ -97,7 +108,7 @@ internal object TestJars { /** 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") + File(modules, "org.jetbrains.kotlin/kotlin-stdlib") .walkTopDown() .filter { it.name.startsWith("kotlin-stdlib-") && it.name.endsWith(".jar") && "sources" !in it.name } .maxByOrNull { it.name } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt new file mode 100644 index 0000000000..a205592a23 --- /dev/null +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt @@ -0,0 +1,26 @@ +package com.itsaky.androidide.gradle.sqlite + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +/** + * The test support's own logic, which the rewrite tests rely on to find their jars and to tell a linking reference + * from a broken one. + */ +class SqliteTestSupportTest { + @Test + fun `the jars are looked up in the Gradle user home of the build that runs the tests`( + @TempDir dir: File, + ) { + val home = File(dir, "home") + val active = File(dir, "active-gradle-home") + val env = File(dir, "env-gradle-home") + + assertThat(gradleUserHome(active.path, env.path, home)).isEqualTo(active) + // run from an IDE, without the property the test tasks pass + assertThat(gradleUserHome(null, env.path, home)).isEqualTo(env) + assertThat(gradleUserHome(null, null, home)).isEqualTo(File(home, ".gradle")) + } +} From bf86cdde7fbaf2284f5a0ab8009c276a1a5434f5 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:58 -0800 Subject: [PATCH 41/56] ADFA-7138 Do not inherit constructors when the tests resolve a reference ClassIndex.resolve searched superclasses for every member, so a reference to Child.()V resolved to Parent's constructor when Child declares only Child(int). The JVM rejects that invokespecial with NoSuchMethodError, so the resolution tests could accept a constructor reference that does not link. A constructor is now looked up in the named owner only; methods and fields are still inherited. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/sqlite/SqliteTestSupport.kt | 4 +++- .../gradle/sqlite/SqliteTestSupportTest.kt | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 index 6d677ea7e5..99aa55cc27 100644 --- 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 @@ -250,7 +250,8 @@ internal class ClassIndex( * 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. + * for a caller that is not a subclass, as a probe that stands in for one. A constructor is looked up + * in [owner] only: it is not inherited. */ fun resolve( owner: String, @@ -279,6 +280,7 @@ internal class ClassIndex( if (isStatic != null && (access and Opcodes.ACC_STATIC != 0) != isStatic) return null return if (accessible(access, n, caller, protectedOk)) n else null } + if (!field && name == "") return null c.superName?.let(pending::add) pending.addAll(c.interfaces) } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt index a205592a23..56fff64fde 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt @@ -23,4 +23,23 @@ class SqliteTestSupportTest { assertThat(gradleUserHome(null, env.path, home)).isEqualTo(env) assertThat(gradleUserHome(null, null, home)).isEqualTo(File(home, ".gradle")) } + + @Test + fun `a constructor resolves in the named class only, while methods are still inherited`() { + val classes = + Compiler.compile( + listOf( + JavaSource("p.Parent", "package p; public class Parent { public Parent() {} public void m() {} }"), + JavaSource("p.Child", "package p; public class Child extends Parent { public Child(int i) {} }"), + ), + classpath = emptyList(), + ) + val index = ClassIndex(emptyList()) + classes.values.forEach(index::addClass) + + assertThat(index.resolve("p/Child", "", "(I)V", field = false)).isEqualTo("p/Child") + // the JVM throws NoSuchMethodError for `new Child()`: Parent's constructor is not Child's + assertThat(index.resolve("p/Child", "", "()V", field = false)).isNull() + assertThat(index.resolve("p/Child", "m", "()V", field = false)).isEqualTo("p/Parent") + } } From 0cab2d4cf36bfe1c0b95e61f5f757831cc9a06e1 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:59 -0800 Subject: [PATCH 42/56] ADFA-7138 Bound the release-APK check script by its timeout before reading output checkApk read the script's output to its end and only then made the timed wait, so a script, or a child it left behind, that kept stdout open would hang the test past its one-minute limit. runBounded now sends the output to a temp file, waits with the timeout first, destroys the process on timeout and fails with what it printed, and reads the file after the process ends. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../androidide/gradle/SqliteReleaseApkTest.kt | 18 ++++------ .../gradle/sqlite/SqliteTestSupport.kt | 33 +++++++++++++++++++ .../gradle/sqlite/SqliteTestSupportTest.kt | 21 ++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt index b85f62305b..b82e66bd76 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteReleaseApkTest.kt @@ -2,7 +2,9 @@ package com.itsaky.androidide.gradle import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.gradle.sqlite.ProcessResult import com.itsaky.androidide.gradle.sqlite.requireEnvironment +import com.itsaky.androidide.gradle.sqlite.runBounded 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 @@ -11,7 +13,6 @@ import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File -import java.util.concurrent.TimeUnit /** * The release-APK guard (ADFA-7138): an app that opted into the SQLite debugger gets it in its debuggable @@ -32,19 +33,12 @@ class SqliteReleaseApkTest { return File(path!!) } - private class CheckResult( - val exitCode: Int, - val output: String, - ) - - private fun checkApk(apk: File): CheckResult { + private fun checkApk(apk: File): ProcessResult { assertWithMessage("APK not built").that(apk.isFile).isTrue() val script = FileProvider.projectRoot().resolve("scripts/check-release-apk-no-sqlite-debugger.sh").toFile() - val process = ProcessBuilder("bash", script.path, apk.path).redirectErrorStream(true).start() - val output = process.inputStream.bufferedReader().readText() - check(process.waitFor(1, TimeUnit.MINUTES)) { "the check script did not finish" } - println("${apk.name}: exit ${process.exitValue()}\n$output") - return CheckResult(process.exitValue(), output) + val result = runBounded(listOf("bash", script.path, apk.path), timeoutSeconds = 60) + println("${apk.name}: exit ${result.exitCode}\n${result.output}") + return result } @Test 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 index 99aa55cc27..d41edad4bd 100644 --- 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 @@ -7,6 +7,7 @@ import org.objectweb.asm.tree.ClassNode import java.io.File import java.net.URI import java.nio.file.Files +import java.util.concurrent.TimeUnit import java.util.zip.ZipFile import javax.tools.JavaFileObject import javax.tools.SimpleJavaFileObject @@ -121,6 +122,38 @@ internal object TestJars { } } +/** A finished process: its exit code, and its standard output and error together. */ +internal class ProcessResult( + val exitCode: Int, + val output: String, +) + +/** + * Runs [command] with its output going to a temp file, so the time limit holds even for a process, or a child it + * leaves behind, that keeps the output open: reading a pipe to its end would wait for that. On timeout it destroys + * the process and fails with what it printed. + */ +internal fun runBounded( + command: List, + timeoutSeconds: Long, +): ProcessResult { + val log = Files.createTempFile("sqlite-test-process", ".log").toFile() + try { + val process = + ProcessBuilder(command) + .redirectErrorStream(true) + .redirectOutput(log) + .start() + if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) { + process.destroyForcibly().waitFor(10, TimeUnit.SECONDS) + throw AssertionError("${command.joinToString(" ")} did not finish in $timeoutSeconds s; it printed:\n${log.readText()}") + } + return ProcessResult(process.exitValue(), log.readText()) + } finally { + log.delete() + } +} + internal class JavaSource( val className: String, val code: String, diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt index 56fff64fde..84f83ebcb5 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt @@ -1,9 +1,11 @@ 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.junit.jupiter.api.io.TempDir import java.io.File +import java.time.Duration /** * The test support's own logic, which the rewrite tests rely on to find their jars and to tell a linking reference @@ -42,4 +44,23 @@ class SqliteTestSupportTest { assertThat(index.resolve("p/Child", "", "()V", field = false)).isNull() assertThat(index.resolve("p/Child", "m", "()V", field = false)).isEqualTo("p/Parent") } + + @Test + fun `a process that keeps its output open fails at its time limit, with what it printed`() { + val started = System.nanoTime() + + val e = assertThrows(AssertionError::class.java) { runBounded(listOf("bash", "-c", "echo started; sleep 20"), timeoutSeconds = 1) } + + assertThat(e.message).contains("did not finish in 1 s") + assertThat(e.message).contains("started") + assertThat(Duration.ofNanos(System.nanoTime() - started)).isLessThan(Duration.ofSeconds(15)) + } + + @Test + fun `a process that finishes gives its exit code and output`() { + val result = runBounded(listOf("bash", "-c", "echo out; echo err >&2; exit 3"), timeoutSeconds = 30) + + assertThat(result.exitCode).isEqualTo(3) + assertThat(result.output.lines()).containsAtLeast("out", "err") + } } From 7572eced8f962e7f300e0baf69e68e0071087929 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 23:19:59 -0800 Subject: [PATCH 43/56] ADFA-7138 Check this run's helper in the transform test, not any cached one The test looked for a rewritten SqliteRewriteRuntime in every jar of the shared transform cache. The helper's name is fixed, so a rewritten copy from an earlier run passed the check even when this run left the helper alone. The helper AAR now also carries a class with a per-run name, and the test inspects only the transformed jars that hold it. Checked against a build that skips the helper package in isInstrumentable: the new test fails, and the old one still passed on a cached helper from an earlier run. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/SqliteRewriteTransformTest.kt | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) 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 index b392f5da73..08d5583ff3 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt @@ -57,16 +57,24 @@ class SqliteRewriteTransformTest { /** * 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. + * in the variant's transform task, so the output is in Gradle's transform cache, which every run of this test + * shares: the bytes of [read] in each jar that holds [marker]. [marker] names a class no earlier run used, so a + * hit is this run's. */ - private fun transformedJarsWith(entry: String): List { + private fun transformedJarsWith( + marker: String, + read: String = marker, + ): 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() + runCatching { + ZipFile(jar).use { zip -> + if (zip.getEntry(marker) == null) null else zip.getEntry(read)?.let { zip.getInputStream(it).readBytes() } + } + }.getOrNull() }.toList() } @@ -89,9 +97,17 @@ class SqliteRewriteTransformTest { .projectRoot() .resolve("subprojects/sqlite-rewrite-runtime/src/main/java/org/appdevforall/sqliterewrite/SqliteRewriteRuntime.java") .toFile() + // The helper's name is fixed, so its AAR also carries a class of this run's, to tell its transformed jar apart + val markerName = "Marker${System.nanoTime()}" val helper = Compiler.compile( - listOf(JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText())), + listOf( + JavaSource("org.appdevforall.sqliterewrite.SqliteRewriteRuntime", helperSource.readText()), + JavaSource( + "org.appdevforall.sqliterewrite.$markerName", + "package org.appdevforall.sqliterewrite; class $markerName {}", + ), + ), listOf(TestJars.androidJar, TestJars.androidxCoreJar), ) val libAar = aar(File(dir, "libs/x.aar"), "x.lib", library) @@ -160,8 +176,10 @@ class SqliteRewriteTransformTest { // 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) } } + transformedJarsWith( + "org/appdevforall/sqliterewrite/$markerName.class", + read = "org/appdevforall/sqliterewrite/SqliteRewriteRuntime.class", + ).map { bytes -> ClassNode().also { ClassReader(bytes).accept(it, 0) } } assertThat(helpers).isNotEmpty() assertThat( helpers.any { helper -> From 5a2fd6e729f68718b32a5ce70fb1ceb5dc06abdd Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 04:33:28 -0800 Subject: [PATCH 44/56] ADFA-7138: Tapping the "Quick Build: off" status line shows the refusal, as the button does While the SQLite debugger is on, the status line reads "Quick Build: off while the SQLite debugger is on - tap for details", but the line was plain text: a tap fell through to the sheet header, and only the Quick Build button showed the refusal again (F221, found on a Galaxy Note 20). The refused-state QuickBuildStatusBarUpdate.Show now carries a tapLabel. ProjectHandlerActivity binds it to the status line after writing the text, and every status write by anyone (doSetStatus) unbinds it first, so the tap lasts only while that line is up. The tap runs the toolbar's Quick Build action through the registry, the button's own path; the dropdown's Quick Build row now shares that function (runQuickBuildAction) instead of repeating it. While tappable the line is clickable and focusable, and TalkBack reads "double- tap to show why Quick Build is off" (new quick_build_status_off_sqlite_debugger_action string). Its size is unchanged, so the 2x font-scale header sizing is untouched; the rest of the header still expands the sheet. Left alone: "setup failed - tap to retry", "stopped - tap to retry" and "built - tap to start app" also say "tap" and are still plain text; they are not part of F221. Test: QuickBuildStatusLineTapTest (Robolectric) inflates the real status line, applies the real mapping the way the activity does, and clicks it. The editor activity itself is too large for a JVM test, so the callback stands in for the toolbar action run. Reverting either the mapping's tapLabel or the binder's click wiring fails it at "value of: performClick() expected to be true". gradle-plugin/docs/sqlite-rewrite.md now mentions the tap. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../editor/EditorHandlerActivity.kt | 20 ++- .../editor/ProjectHandlerActivity.kt | 12 ++ .../quickbuild/QuickBuildStatusBar.kt | 15 ++- .../itsaky/androidide/ui/EditorBottomSheet.kt | 15 +++ .../com/itsaky/androidide/ui/StatusLineTap.kt | 37 +++++ .../quickbuild/QuickBuildStatusBarTest.kt | 12 +- .../quickbuild/QuickBuildStatusLineTapTest.kt | 127 ++++++++++++++++++ gradle-plugin/docs/sqlite-rewrite.md | 4 +- resources/src/main/res/values/strings.xml | 2 + 9 files changed, 233 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt create mode 100644 app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 40f2dc6425..5942453004 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -879,9 +879,7 @@ open class EditorHandlerActivity : popup.setOnMenuItemClickListener { item -> when (item.itemId) { R.id.action_quick_build -> { - // Through the registry, same as Standard Run below, so the menu entry - // and the toolbar tap share one code path (incl. the analytics event). - if (quickBuild != null && quickBuild.enabled) registry.executeAction(quickBuild, data) + runQuickBuildAction(data) true } @@ -907,6 +905,22 @@ open class EditorHandlerActivity : popup.show() } + /** + * Runs the toolbar's Quick Build action through the registry, as the button's own tap does, so + * the dropdown row and a status line that asks for a tap share the button's code path + * (including the analytics event). Does nothing while the button is greyed out, as the button + * does. + */ + private fun runQuickBuildAction(data: ActionData) { + val registry = getInstance() as DefaultActionsRegistry + val quickBuild = registry.findAction(EDITOR_TOOLBAR, QuickBuildAction.ID) ?: return + if (quickBuild.enabled) registry.executeAction(quickBuild, data) + } + + override fun onQuickBuildStatusLineTapped() { + runQuickBuildAction(createToolbarActionData()) + } + private fun createToolbarActionData(): ActionData { val data = ActionData.create(this) val currentEditor = getCurrentEditor() diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index 126b52f792..59598551fb 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -541,6 +541,10 @@ abstract class ProjectHandlerActivity : // setStatus resets ownership (any caller takes the bar over); reclaim it. setStatus(getString(update.text, *update.args.toTypedArray())) ownsQuickBuildStatus = true + // After setStatus, which drops any tap the line had. + update.tapLabel?.let { label -> + contentOrNull?.bottomSheet?.setStatusTap(getString(label), ::onQuickBuildStatusLineTapped) + } } } @@ -1438,9 +1442,17 @@ abstract class ProjectHandlerActivity : gravity: Int, ) { ownsQuickBuildStatus = false + // A tap belongs to the line that asked for it; the next writer's line is plain text. + contentOrNull?.bottomSheet?.setStatusTap(null, null) super.doSetStatus(text, gravity) } + /** + * A tap on a Quick Build status line that asks for one: it does what a tap on the Quick Build + * button does, through the same action, so the line cannot promise what the button would not. + */ + protected abstract fun onQuickBuildStatusLineTapped() + fun appendBuildOutput(str: String) { if (_binding == null || isDestroyed || isFinishing) return content.bottomSheet.appendBuildOut(str) diff --git a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt index fc75a5546a..eef44fcf6e 100644 --- a/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt +++ b/app/src/main/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBar.kt @@ -21,11 +21,16 @@ sealed interface QuickBuildStatusBarUpdate { * @property args positional format arguments for [text], in order. * @property onlyIfOwned apply only if Quick Build's text is still on the bar, so a passive * refresh cannot clobber a line another writer took over. + * @property tapLabel set for a [text] that asks for a tap: a tap on the line then does what a + * tap on the Quick Build button does, and this is what TalkBack says the tap does. Null for + * a line that is plain text. The tap lasts only while this text is on the bar; the next + * write to the bar, by anyone, drops it. */ data class Show( @param:StringRes @get:StringRes val text: Int, val args: List = emptyList(), val onlyIfOwned: Boolean = false, + @param:StringRes @get:StringRes val tapLabel: Int? = null, ) : QuickBuildStatusBarUpdate /** Clear the bar - but only if the last write was Quick Build's (the caller tracks that). */ @@ -89,9 +94,13 @@ fun quickBuildStatusBarUpdate( } QuickBuildTransition.StartRefused -> { - // Not a failure: Quick Build is deliberately off here, and a retry cannot help. The tap - // shows the full refusal again. - QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger) + // Not a failure: Quick Build is deliberately off here, and a retry cannot help. The text + // says "tap for details", so the line itself takes the tap: it runs the button's tap, + // which re-runs the cheap check and shows the full refusal again. + QuickBuildStatusBarUpdate.Show( + R.string.quick_build_status_off_sqlite_debugger, + tapLabel = R.string.quick_build_status_off_sqlite_debugger_action, + ) } is QuickBuildTransition.ProvisioningStarted -> { diff --git a/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt b/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt index f4cf3638cd..c2294b296a 100644 --- a/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt +++ b/app/src/main/java/com/itsaky/androidide/ui/EditorBottomSheet.kt @@ -421,6 +421,7 @@ class EditorBottomSheet binding.viewOptionsOutputAction.clearLongPressHelp() binding.copyDiagnosticsFab.setOnClickListener(null) binding.headerContainer.setOnClickListener(null) + setStatusTap(null, null) removeOnLayoutChangeListener(fabLayoutChangeListener) ViewCompat.setOnApplyWindowInsetsListener(this, null) @@ -757,6 +758,20 @@ class EditorBottomSheet } } + /** + * Makes the status line a button for a line that asks for a tap, or plain text again with a + * null [onTap]. Main thread only, like the status writes it follows; see [bindStatusLineTap]. + * + * @param actionLabel what the tap does, for TalkBack's "double-tap to" announcement. + * @param onTap what the tap does, or null for plain text. + */ + fun setStatusTap( + actionLabel: CharSequence?, + onTap: (() -> Unit)?, + ) { + bindStatusLineTap(binding.buildStatus.statusText, actionLabel, onTap) + } + /** * Whether a user build is narrating into the status line. While it is, the collapsed * header keeps the tallest height it has needed; when it stops, the header re-fits the diff --git a/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt b/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt new file mode 100644 index 0000000000..0ccb582194 --- /dev/null +++ b/app/src/main/java/com/itsaky/androidide/ui/StatusLineTap.kt @@ -0,0 +1,37 @@ +package com.itsaky.androidide.ui + +import android.widget.TextView +import androidx.core.view.ViewCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat + +/** + * Makes the editor's status line a button while its text asks for a tap, and plain text again + * when it does not. + * + * Plain text the rest of the time, so a tap on the line falls through to the header under it, + * which expands the bottom sheet. The line's size is left alone: the collapsed header is measured + * from it, and at a 2x font scale it needs every row it takes. + * + * @param statusText the status line. + * @param actionLabel what a tap does, which TalkBack reads as "double-tap to [actionLabel]". + * Ignored without [onTap]. + * @param onTap what a tap does, or null to make the line plain text again. + */ +internal fun bindStatusLineTap( + statusText: TextView, + actionLabel: CharSequence?, + onTap: (() -> Unit)?, +) { + if (onTap == null) { + statusText.setOnClickListener(null) + statusText.isClickable = false + statusText.isFocusable = false + ViewCompat.removeAccessibilityAction(statusText, AccessibilityActionCompat.ACTION_CLICK.id) + return + } + statusText.setOnClickListener { onTap() } + // Focusable so a keyboard or switch user can reach it, not only a finger. + statusText.isFocusable = true + // A null command keeps the default click; only the spoken label is replaced. + ViewCompat.replaceAccessibilityAction(statusText, AccessibilityActionCompat.ACTION_CLICK, actionLabel, null) +} diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt index bc5952fc6a..68f104fafb 100644 --- a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusBarTest.kt @@ -295,11 +295,15 @@ class QuickBuildStatusBarTest { SessionEvent.ProvisioningFailed(QuickBuildMessage.RebuildFailed), ).state - assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(refused))) - .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger)) + // The line says "tap for details", so it carries the tap; QuickBuildStatusLineTapTest clicks it. + val off = + QuickBuildStatusBarUpdate.Show( + R.string.quick_build_status_off_sqlite_debugger, + tapLabel = R.string.quick_build_status_off_sqlite_debugger_action, + ) + assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(refused))).isEqualTo(off) // A recreation resubscribes with no previous status and must read the same. - assertThat(update(null, QuickBuildStatus.from(refused))) - .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_off_sqlite_debugger)) + assertThat(update(null, QuickBuildStatus.from(refused))).isEqualTo(off) assertThat(update(QuickBuildStatus.Provisioning(), QuickBuildStatus.from(failed))) .isEqualTo(QuickBuildStatusBarUpdate.Show(R.string.quick_build_status_start_failed)) // A save clears the carried refusal as it does a failure. diff --git a/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt new file mode 100644 index 0000000000..ee210b7a4f --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/quickbuild/QuickBuildStatusLineTapTest.kt @@ -0,0 +1,127 @@ +package com.itsaky.androidide.quickbuild + +import android.content.Context +import android.view.LayoutInflater +import android.view.View.MeasureSpec +import android.view.accessibility.AccessibilityNodeInfo +import androidx.appcompat.view.ContextThemeWrapper +import androidx.test.core.app.ApplicationProvider +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.R +import com.itsaky.androidide.databinding.LayoutEditorBuildStatusBinding +import com.itsaky.androidide.ui.bindStatusLineTap +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildMessage +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildSessionState +import org.appdevforall.cotg.quickbuild.domain.session.QuickBuildStatus +import org.appdevforall.cotg.quickbuild.domain.session.SessionEvent +import org.appdevforall.cotg.quickbuild.domain.session.SessionReducer +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import com.itsaky.androidide.resources.R as Res + +/** + * The Quick Build status line takes the tap its text asks for (F221). + * + * While the SQLite debugger is on, the line reads "Quick Build: off while the SQLite debugger is + * on - tap for details", and tapping it did nothing: only the toolbar button showed the refusal. + * A tap on the line must now run the button's tap, which shows the refusal again. + * + * The editor activity is far beyond a JVM test, so this drives the parts it joins: the real + * status mapping, the real status-line layout, and the binder the bottom sheet uses, applied the + * way ProjectHandlerActivity applies them (every write drops the tap, then a Show that asks for one + * binds it). The tap callback stands in for the activity's run of the toolbar action. + */ +@RunWith(RobolectricTestRunner::class) +class QuickBuildStatusLineTapTest { + private val context = ApplicationProvider.getApplicationContext() + + /** The real status block, inflated against the app's theme, which its text colour needs. */ + private val line = + LayoutEditorBuildStatusBinding.inflate( + LayoutInflater.from(ContextThemeWrapper(context, R.style.Theme_AndroidIDE)), + ) + + private var quickBuildTaps = 0 + + private val refused = + QuickBuildStatus.from( + SessionReducer() + .reduce( + QuickBuildSessionState.Provisioning(), + SessionEvent.ProvisioningFailed(QuickBuildMessage.DisabledBySqliteDebugger), + ).state, + ) + + private fun show(update: QuickBuildStatusBarUpdate?) { + val show = update as QuickBuildStatusBarUpdate.Show + bindStatusLineTap(line.statusText, null, null) + line.statusText.text = context.getString(show.text, *show.args.toTypedArray()) + show.tapLabel?.let { label -> + bindStatusLineTap(line.statusText, context.getString(label)) { quickBuildTaps++ } + } + } + + private fun clickAction(): AccessibilityNodeInfo.AccessibilityAction? = + line.statusText + .createAccessibilityNodeInfo() + .actionList + .singleOrNull { it.id == AccessibilityNodeInfo.ACTION_CLICK } + + private fun measuredHeight(): Int { + line.root.measure( + MeasureSpec.makeMeasureSpec(WIDTH_PX, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + ) + return line.statusText.measuredHeight + } + + @Test + fun `a tap on the refused line runs the Quick Build button's tap`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + assertThat(line.statusText.text.toString()) + .isEqualTo(context.getString(Res.string.quick_build_status_off_sqlite_debugger)) + + assertThat(line.statusText.performClick()).isTrue() + + assertThat(quickBuildTaps).isEqualTo(1) + } + + @Test + fun `the refused line is a labelled button for TalkBack and the keyboard`() { + show(quickBuildStatusBarUpdate(null, refused)) + + assertThat(line.statusText.isClickable).isTrue() + assertThat(line.statusText.isFocusable).isTrue() + assertThat(clickAction()?.label?.toString()) + .isEqualTo(context.getString(Res.string.quick_build_status_off_sqlite_debugger_action)) + // TalkBack's double-tap is an accessibility click, not a touch. + assertThat(line.statusText.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null)).isTrue() + assertThat(quickBuildTaps).isEqualTo(1) + } + + @Test + fun `the tap leaves the line's size alone`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + val withTap = measuredHeight() + bindStatusLineTap(line.statusText, null, null) + + assertThat(withTap).isEqualTo(measuredHeight()) + } + + @Test + fun `the next line is plain text again, so a tap on it reaches the header`() { + show(quickBuildStatusBarUpdate(QuickBuildStatus.Provisioning(), refused)) + show(quickBuildStatusBarUpdate(refused, QuickBuildStatus.Provisioning())) + + assertThat(line.statusText.isClickable).isFalse() + assertThat(line.statusText.isFocusable).isFalse() + assertThat(clickAction()).isNull() + assertThat(line.statusText.performClick()).isFalse() + assertThat(quickBuildTaps).isEqualTo(0) + } + + private companion object { + const val WIDTH_PX = 600 + } +} diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 1307fa4951..c0f2744631 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -194,7 +194,9 @@ dependency, and runs the script on both APKs. The release must pass. The debug A 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 (`GradleQuickBuildProvisioner.sqliteDebuggerRefusal`): it shows `quick_build_disabled_sqlite_debugger`, which - says Quick Build is off while the SQLite debugger is on. + says Quick Build is off while the SQLite debugger is on. The editor's status line then reads + `quick_build_status_off_sqlite_debugger`, and a tap on it, like a tap on the Quick Build button, shows that + message again. - Classes loaded by name (`Class.forName`, reflection, strings naming `android.database.sqlite.*`) are not rewritten. A string constant that names one of the renamed classes (dots or slashes, `$` or `.` before a nested class, alone or in a type descriptor) logs a warning with class, method and line, in the classes the fail rule diff --git a/resources/src/main/res/values/strings.xml b/resources/src/main/res/values/strings.xml index 412e4da19d..9ec6aa932f 100644 --- a/resources/src/main/res/values/strings.xml +++ b/resources/src/main/res/values/strings.xml @@ -1219,6 +1219,8 @@ Quick Build failed - see Build Output Quick Build: setup failed - tap to retry Quick Build: off while the SQLite debugger is on - tap for details + + show why Quick Build is off Quick Build failed. See the Build Output panel for more details. Quick Build successful Quick Build compiler restarting… From 5ad480da7b0cd20bcf4c9479771b86ecfe82505f Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 10:41:02 -0800 Subject: [PATCH 45/56] ADFA-7138 Delete a failed SQLite AAR copy's temp file, and sync it before the rename replaceAtomically deleted the .part file only when the rename failed, so a copy that threw (an asset read error, a full disk) left .aar.part beside the AAR. It is now deleted in a finally block, which after a successful rename finds nothing. The copy is also synced (fd.sync()) before the rename, so a power loss after the rename cannot leave the AAR empty on a file system that does not order data before metadata. The temp name stays fixed rather than unique per extractor. ToolsManager.init is the only extractor, called once per process (CredentialProtectedApplicationLoader guards it with _isLoaded.compareAndSet) and the app runs one process, so no second extractor races it. A unique name would leave a new orphan after every process death mid-copy, which no finally block can clean, while a fixed name is overwritten by the next run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../androidide/managers/ToolsManager.java | 17 +++++++++------ .../managers/ToolsManagerSqliteAarTest.kt | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) 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 12376129cb..714bc4a16a 100755 --- a/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java +++ b/common/src/main/java/com/itsaky/androidide/managers/ToolsManager.java @@ -211,19 +211,24 @@ static boolean isToolingJarCurrent(File toolingJarFile, File stampFile, String s } /** - * Copies the stream to a temp sibling of the target, then renames it over the target. {@link #init} extracts the SQLite AARs in the background, and GradleBuildService passes their paths to a build without waiting, so a build that starts meanwhile must find the previous complete file or the new one, never a missing or partly written one. rename(2) within one directory atomically replaces the target on Linux. Does not close the stream. + * Copies the stream to a temp sibling of the target, syncs it, then renames it over the target. {@link #init} extracts the SQLite AARs in the background, and GradleBuildService passes their paths to a build without waiting, so a build that starts meanwhile must find the previous complete file or the new one, never a missing or partly written one. rename(2) within one directory atomically replaces the target on Linux; syncing first means a power loss after the rename cannot leave the target empty. The temp file is deleted when any step fails. Its name is fixed, not unique: init runs once per process, and the next run overwrites what a killed process left. Does not close the stream. */ @VisibleForTesting static void replaceAtomically(InputStream source, File target) throws IOException { final var dir = Objects.requireNonNull(target.getParentFile()); dir.mkdirs(); final var tempFile = new File(dir, target.getName() + ".part"); - try (final var fos = new FileOutputStream(tempFile)) { - IoUtilsKt.transferToStream(source, fos); - } - if (!tempFile.renameTo(target)) { + try { + try (final var fos = new FileOutputStream(tempFile)) { + IoUtilsKt.transferToStream(source, fos); + fos.getFD().sync(); + } + if (!tempFile.renameTo(target)) { + throw new IOException("Could not move " + tempFile + " to " + target); + } + } finally { + // gone after a successful rename; a leftover of a failed copy is what this removes tempFile.delete(); - throw new IOException("Could not move " + tempFile + " to " + target); } } diff --git a/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt index 9c4a85d177..3d95db8389 100644 --- a/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt +++ b/common/src/test/java/com/itsaky/androidide/managers/ToolsManagerSqliteAarTest.kt @@ -1,10 +1,12 @@ package com.itsaky.androidide.managers import com.google.common.truth.Truth.assertThat +import org.junit.Assert.assertThrows import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import java.io.IOException import java.io.InputStream /** @@ -55,6 +57,25 @@ class ToolsManagerSqliteAarTest { assertThat(File(aar.parentFile, "sqlite-debugger.aar.part").exists()).isFalse() } + @Test + fun `a copy that fails leaves the previous AAR and no temp file`() { + val aar = File(tempFolder.root, "sqlite-debugger/sqlite-debugger.aar") + aar.parentFile!!.mkdirs() + aar.writeText("previous-install-aar") + val failing = + object : InputStream() { + private var left = 4 + + override fun read(): Int = if (left-- > 0) 'x'.code else throw IOException("asset read failed") + } + + val e = assertThrows(IOException::class.java) { ToolsManager.replaceAtomically(failing, aar) } + + assertThat(e).hasMessageThat().isEqualTo("asset read failed") + assertThat(aar.readText()).isEqualTo("previous-install-aar") + assertThat(aar.parentFile!!.list()).asList().containsExactly("sqlite-debugger.aar") + } + @Test fun `the first extraction creates the directory and the AAR`() { val aar = File(tempFolder.root, "sqlite-rewrite-runtime/sqlite-rewrite-runtime.aar") From 586a9b91f443209df02ae1ab37575b0b0bc3715f Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 10:42:05 -0800 Subject: [PATCH 46/56] ADFA-7138 Fail the build for an SQLite cursor stored into an Android cursor array javac writes no CHECKCAST for `AbstractCursor[] a; a[0] = sqliteCursor;`. After the rename the value is requery's SQLiteCursor, which is not an AbstractCursor, so the store verifies but throws ArrayStoreException at run time, in release builds too: the rewrite runs in every variant of an opted-in app (D10, SqliteRewritePlugin.kt:44). implicitCursorConversions now fails an AASTORE whose value is statically an SQLiteCursor (or a subclass) and whose array is, on every path to the store, an AbstractCursor[], AbstractWindowedCursor[] or CrossProcessCursor[] from a parameter, field, call, cast or ANEWARRAY. The message names the class, method and line, like the other rewrite failures. The analysis value carries the array's element type, and a merge keeps it only when both paths agree. The pre-filter now also looks at a method's parameters and at ANEWARRAY and array casts, since an array parameter is all such a store needs. Not checked, on purpose, and listed in sqlite-rewrite.md's known limits: a store into an array known only as a Cursor[] or Object[] (failing those would fail ordinary apps, though such an array can be an AbstractCursor[] at run time), a store whose array type differs between the paths that merge before it, and values loaded from arrays. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 9 +- .../gradle/sqlite/SqliteRewriter.kt | 52 +++++++-- .../gradle/sqlite/SqliteRewriterTest.kt | 101 ++++++++++++++++++ 3 files changed, 151 insertions(+), 11 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index c0f2744631..72636ee26b 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -72,7 +72,9 @@ 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`). The same conversion without a cast fails too: a value declared `SQLiteCursor`, or a subclass, returned, passed, called on, stored in a field, or held in a local as one of those three types (`AbstractCursor up(SQLiteCursor c) - { return c; }`). javac writes no `CHECKCAST` for it, and the renamed class would not verify. + { return c; }`). javac writes no `CHECKCAST` for it, and the renamed class would not verify. So does storing one + into an array of those types (`AbstractCursor[] a; a[0] = c;`), which would throw `ArrayStoreException`: the + array must be one on every path to the store, from a parameter, field, call or `new`. 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 when the guard holds on every path to the cast: in `if (b || c instanceof X) ((X) c)` the cast is reached with `b` alone, so it is reported. `COLLATE LOCALIZED` only logs a warning: it works, but @@ -202,6 +204,11 @@ dependency, and runs the script on both APKs. The release must pass. The debug A class, alone or in a type descriptor) logs a warning with class, method and line, in the classes the fail rule covers; names of kept classes such as `SQLiteException`, and longer names that contain a renamed one, such as `xandroid.database.sqlite.SQLiteDatabase`, do not. +- Stores of an SQLite cursor into an array known only as a `Cursor[]` or `Object[]` are allowed on purpose (failing + them would fail ordinary apps), but such an array can be an `AbstractCursor[]` at run time (arrays are covariant), + and a store whose array type is unknown where branches merge is not checked. Both can still throw + `ArrayStoreException`, in release builds too (the rewrite runs in every variant of an opted-in app). Cursors loaded + from arrays are not followed. - 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 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 67dfcbe378..74c94e0270 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 @@ -186,16 +186,22 @@ object SqliteRewriter { } } - /** A reference whose static type, on some path, is SQLiteCursor or a subclass, which the rename makes requery's. */ + /** + * A reference whose static type, on some path, is SQLiteCursor or a subclass, which the rename makes requery's + * ([renamedCursor]), or, on every path, an array of one of the Android-only cursor types ([androidCursorArray], + * that element type). + */ private class StaticCursorValue( type: Type?, val renamedCursor: Boolean, + val androidCursorArray: String? = null, ) : BasicValue(type) { // as TaintValue: Frame.merge keeps the old value when the merged one equals it override fun equals(other: Any?): Boolean = - other is BasicValue && super.equals(other) && renamedCursor == ((other as? StaticCursorValue)?.renamedCursor ?: false) + other is BasicValue && super.equals(other) && renamedCursor == ((other as? StaticCursorValue)?.renamedCursor ?: false) && + androidCursorArray == (other as? StaticCursorValue)?.androidCursorArray - override fun hashCode(): Int = super.hashCode() * 31 + if (renamedCursor) 1 else 0 + override fun hashCode(): Int = (super.hashCode() * 31 + if (renamedCursor) 1 else 0) * 31 + androidCursorArray.hashCode() } private class ClassRewrite( @@ -758,15 +764,21 @@ object SqliteRewriter { /** A type in a frame or an internal name, when it is one of the Android-only cursor types, or null. */ private fun androidOnlyCursor(entry: Any?): String? = (entry as? String)?.takeIf { it in Rules.ANDROID_ONLY_CURSOR_TYPES } + /** [type]'s element type when it is a one-dimensional array of an Android-only cursor type, or null. */ + private fun androidCursorArray(type: Type?): String? = + if (type != null && type.sort == Type.ARRAY && type.dimensions == 1) androidOnlyCursor(type.elementType) else null + /** Whether [m] names an Android-only cursor type where a value could be converted to it without a cast. */ private fun mayConvertToAndroidCursor(m: MethodNode): Boolean { fun names(desc: String) = Rules.ANDROID_ONLY_CURSOR_TYPES.any { desc.contains("L$it;") } - if (names(Type.getReturnType(m.desc).descriptor)) return true + // the parameters too: an array parameter is all that a store into one needs + if (names(m.desc)) return true return m.instructions.any { insn -> when (insn) { is MethodInsnNode -> insn.owner in Rules.ANDROID_ONLY_CURSOR_TYPES || names(insn.desc) is InvokeDynamicInsnNode -> names(insn.desc) is FieldInsnNode -> names(insn.desc) + is TypeInsnNode -> names(insn.desc) || (insn.opcode == Opcodes.ANEWARRAY && androidOnlyCursor(insn.desc) != null) is FrameNode -> (insn.local.orEmpty() + insn.stack.orEmpty()).any { androidOnlyCursor(it) != null } else -> false } @@ -778,8 +790,11 @@ object SqliteRewriter { * AbstractWindowedCursor, AbstractCursor or CrossProcessCursor without a cast, by the message for each: returned, * passed, called on, stored in a field, or held where a frame declares one of them. javac writes no CHECKCAST * for such an upcast, and requery's SQLiteCursor neither extends those classes nor implements the interface, so - * the renamed class would not verify (or, for the interface, the call would fail). Values stored in or loaded - * from arrays are not followed; storing one into such an array throws ArrayStoreException. + * the renamed class would not verify (or, for the interface, the call would fail). Also an AASTORE of such a value + * into an array that is, on every path to it, one of those types' arrays (from a parameter, field, call or + * ANEWARRAY): it would throw ArrayStoreException. Not checked: stores into a Cursor[] or Object[], allowed on + * purpose although one can be such an array at run time; a store whose array type differs between the paths + * that merge before it; values loaded from arrays, which are not followed. */ fun implicitCursorConversions(m: MethodNode): Map { if (!mayConvertToAndroidCursor(m)) return emptyMap() @@ -787,16 +802,25 @@ object SqliteRewriter { object : BasicInterpreter(Opcodes.ASM9) { override fun newValue(type: Type?): BasicValue? { val v = super.newValue(type) ?: return null - return if (v.isReference) StaticCursorValue(v.type, isRenamedCursorType(type)) else v + return if (v.isReference) StaticCursorValue(v.type, isRenamedCursorType(type), androidCursorArray(type)) else v } override fun merge( value1: BasicValue, value2: BasicValue, ): BasicValue { - val renamed = - (value1 as? StaticCursorValue)?.renamedCursor == true || (value2 as? StaticCursorValue)?.renamedCursor == true - if (renamed && value1.type != null && value1.type == value2.type) return StaticCursorValue(value1.type, true) + val s1 = value1 as? StaticCursorValue + val s2 = value2 as? StaticCursorValue + // BasicInterpreter.merge answers UNINITIALIZED for values that differ in either flag + if ((s1 != null || s2 != null) && value1.type != null && value1.type == value2.type) { + val merged = + StaticCursorValue( + value1.type, + s1?.renamedCursor == true || s2?.renamedCursor == true, + s1?.androidCursorArray?.takeIf { it == s2?.androidCursorArray }, + ) + return if (merged == value1) value1 else merged + } return super.merge(value1, value2) } } @@ -815,6 +839,14 @@ object SqliteRewriter { fun renamed(value: BasicValue?) = (value as? StaticCursorValue)?.renamedCursor == true fun fromTop(depth: Int) = if (depth < f.stackSize) f.getStack(f.stackSize - 1 - depth) else null + if (insn.opcode == Opcodes.AASTORE) { + val element = (fromTop(2) as? StaticCursorValue)?.androidCursorArray + if (element != null && renamed(fromTop(0))) { + result[insn] = "an SQLite cursor stored into an array of ${element.replace('/', '.')}; requery's SQLiteCursor " + + "is not one, so the store would throw ArrayStoreException" + } + continue + } val target: String? = when { insn is MethodInsnNode || insn is InvokeDynamicInsnNode -> { 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 276264bba9..55b5fc1a51 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 @@ -1528,6 +1528,107 @@ class SqliteRewriterTest { assertThat(unverified.message).contains("androidx/sqlite/db/framework/Up") } + @Test + fun `an SQLite cursor stored into an array of an Android cursor class fails the build`() { + val classes = + Compiler.compile( + listOf( + src( + "p.Stores", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class Stores { + void param(AbstractCursor[] a, SQLiteCursor c) { + a[0] = c; + } + AbstractWindowedCursor[] fresh(SQLiteCursor c) { + AbstractWindowedCursor[] a = new AbstractWindowedCursor[1]; + a[0] = c; + return a; + } + void crossProcess(CrossProcessCursor[] a, SQLiteCursor c) { + Object[] same = a; + same[0] = c; + } + } + """, + ), + ), + ) + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/Stores"]!!, hierarchy) } + assertThat(e.message).contains("'p.Stores'") + assertThat(e.message).contains( + " - param([Landroid/database/AbstractCursor;Landroid/database/sqlite/SQLiteCursor;)V (line 7): an SQLite cursor stored " + + "into an array of android.database.AbstractCursor; requery's SQLiteCursor is not one, so the store would throw ArrayStoreException", + ) + assertThat(e.message).contains( + " - fresh(Landroid/database/sqlite/SQLiteCursor;)[Landroid/database/AbstractWindowedCursor; (line 11): " + + "an SQLite cursor stored into an array of android.database.AbstractWindowedCursor", + ) + // the Object[] local holds the parameter's array, which is a CrossProcessCursor[] on every path + assertThat(e.message).contains( + " - crossProcess([Landroid/database/CrossProcessCursor;Landroid/database/sqlite/SQLiteCursor;)V (line 16): " + + "an SQLite cursor stored into an array of android.database.CrossProcessCursor", + ) + } + + @Test + fun `an SQLite cursor stored into a Cursor or Object array, or another value into an Android cursor array, passes`() { + val classes = + Compiler.compile( + listOf( + src( + "p.StoresFine", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class StoresFine { + void cursors(Cursor[] a, SQLiteCursor c) { + a[0] = c; + } + void objects(Object[] a, SQLiteCursor c) { + a[0] = c; + } + void otherValues(AbstractCursor[] a, MatrixCursor m) { + a[0] = m; + a[1] = null; + } + } + """, + ), + ), + ) + SqliteRewriter.rewrite(classes["p/StoresFine"]!!, hierarchy) + } + + @Test + fun `a store whose array is an Android cursor array on one path only is not checked`() { + val classes = + Compiler.compile( + listOf( + src( + "p.StoresMerged", + """ + package p; + import android.database.*; + import android.database.sqlite.*; + public class StoresMerged { + void merged(AbstractCursor[] a, Cursor[] k, SQLiteCursor c, boolean b) { + Cursor[] t = b ? a : k; + t[0] = c; + } + } + """, + ), + ), + ) + // a known limit: when b is true, the store throws ArrayStoreException at run time + SqliteRewriter.rewrite(classes["p/StoresMerged"]!!, hierarchy) + } + @Test fun `an instanceof guard counts only when it covers every path to the cast`() { val classes = From db7781c772ac9db806b7266d8f36e68c6589e385 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 10:42:05 -0800 Subject: [PATCH 47/56] ADFA-7138 Docs: list getWindow() on an unresolvable SQLiteCursor subclass as a known miss getWindow()Landroid/database/CursorWindow; is not in the rule table, so on a SQLiteCursor subclass that the build cannot resolve the call passes the rewrite and fails at run time. Failing every unresolved call that returns a CursorWindow would fail ordinary apps: android's kept AbstractWindowedCursor and CrossProcessCursor declare getWindow too. The known limits now say so. Docs only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 72636ee26b..ab502ab813 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -218,6 +218,9 @@ dependency, and runs the script on both APKs. The release must pass. The debug A - 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. +- On a `SQLiteCursor` subclass whose class the build cannot resolve, `getWindow()` is not detected, and the call fails + at run time: it is not in the rule table, and failing every unresolved call that returns a `CursorWindow` would fail + ordinary apps (android's kept `AbstractWindowedCursor` and `CrossProcessCursor` declare it too). - `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. From 229feb03657ba4dd656fa03232fb5806b91c7d17 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 10:42:05 -0800 Subject: [PATCH 48/56] ADFA-7138 Say to retry when an SQLite AAR is missing right after Code on the Go starts App init extracts the SQLite debugger and rewrite runtime AARs in the background, so on a fresh install an opted-in build that starts before the first extraction ends finds the AAR missing. The "not found" message only said to remove cogo.sqliteDebugger, which is the wrong advice for that transient case. It now ends: "Remove 'cogo.sqliteDebugger' from gradle.properties to build without it, or retry in a few seconds if Code on the Go has just started." The message that ADFA-7138's Steps to QA quote ("Remove cogo.sqliteDebugger from gradle.properties to build without them") is the runtime-classpath check's, which this does not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../main/java/com/itsaky/androidide/gradle/SqliteAar.kt | 7 +++++-- .../itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt | 5 +++++ .../itsaky/androidide/gradle/SqliteRewritePluginTest.kt | 5 ++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt index 625f2c997b..73e88d2fad 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/SqliteAar.kt @@ -8,7 +8,9 @@ import java.io.File /** * The AAR that [property] names, for a plugin applied only when `cogo.sqliteDebugger=true`. * Fails the build when the property is unset or the file is missing: the project opted in, and a - * build that quietly drops the feature leaves the user wondering why it does nothing. + * build that quietly drops the feature leaves the user wondering why it does nothing. A missing file + * can be transient: app init extracts the AARs in the background, and a build started right after a + * fresh install can run before the first extraction ends, so the message also says to retry. * * @param aarLabel what the AAR is called in the "not found" message. * @param feature what the user's opt-in turns on, for the "remove the line" advice. @@ -27,7 +29,8 @@ internal fun Project.requireSqliteAar( if (!aar.isFile) { throw GradleException( "$aarLabel not found at '${aar.absolutePath}'. " + - "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it.", + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", ) } return aar 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 index edb4bc1cc1..38c5146d07 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteDebuggerPluginTest.kt @@ -138,5 +138,10 @@ class SqliteDebuggerPluginTest { assertThat(failure).isInstanceOf(UnexpectedBuildResultException::class.java) assertThat(failure!!.message).contains("SQLite debugger AAR not found") + // app init extracts the AAR in the background, so right after a fresh install it can be missing for a while + assertThat(failure.message).contains( + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", + ) } } 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 index 93514157a2..141cae889f 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewritePluginTest.kt @@ -97,7 +97,10 @@ class SqliteRewritePluginTest { 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") + assertThat(message).contains( + "Remove '$PROPERTY_SQLITE_DEBUGGER_ENABLED' from gradle.properties to build without it, " + + "or retry in a few seconds if Code on the Go has just started.", + ) } private fun jar( From f556c0c433e6447a5a6cc9fc62020bfed8bbe3a8 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 10:42:05 -0800 Subject: [PATCH 49/56] ADFA-7138 Test that the SQLite rewrite tests find requery in a non-default Gradle user home SqliteTestSupportTest pinned only the order in which the Gradle user home is chosen, not the cache scan that uses it. The requery lookup is now a function of the Gradle user home (requeryAar, over gradleCacheFile), and a new test puts the AAR only in a non-default home's module cache, with the default home's cache present but without it, and checks that the scan finds it there, through the test task's property and through GRADLE_USER_HOME. With the scan hard-coded to ~/.gradle again, the test fails. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/sqlite/SqliteTestSupport.kt | 45 ++++++++++++------- .../gradle/sqlite/SqliteTestSupportTest.kt | 17 +++++++ 2 files changed, 45 insertions(+), 17 deletions(-) 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 index d41edad4bd..782d613ffd 100644 --- 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 @@ -39,10 +39,33 @@ internal fun gradleUserHome( userHome: File = File(System.getProperty("user.home")), ): File = property?.takeIf { it.isNotEmpty() }?.let(::File) ?: env?.takeIf { it.isNotEmpty() }?.let(::File) ?: File(userHome, ".gradle") +/** The module cache of [gradleHome]: group/artifact/version//file. */ +internal fun moduleCache(gradleHome: File = gradleUserHome()): File = File(gradleHome, "caches/modules-2/files-2.1") + +/** [file] of [group]:[artifact]:[version] in the module cache of [gradleHome], or null. */ +internal fun gradleCacheFile( + group: String, + artifact: String, + version: String, + file: String, + gradleHome: File = gradleUserHome(), +): File? = + File(moduleCache(gradleHome), "$group/$artifact/$version") + .walkTopDown() + .firstOrNull { it.name == file } + +/** requery's AAR: the file REQUERY_AAR names, else the one in the module cache of [gradleHome], or null. */ +internal fun requeryAar( + env: String? = System.getenv("REQUERY_AAR"), + gradleHome: File = gradleUserHome(), +): File? = + env?.let(::File)?.takeIf { it.isFile } + ?: gradleCacheFile("com.github.requery", "sqlite-android", "3.49.0", "sqlite-android-3.49.0.aar", gradleHome) + /** 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")) - private val modules by lazy { File(gradleUserHome(), "caches/modules-2/files-2.1") } + private val modules by lazy { moduleCache() } val androidJar: File by lazy { val sdkRoots = @@ -65,16 +88,6 @@ internal object TestJars { jar!! } - private fun gradleCacheAar( - group: String, - artifact: String, - version: String, - file: String, - ): File? = - File(modules, "$group/$artifact/$version") - .walkTopDown() - .firstOrNull { it.name == file } - private fun classesJarOf( aar: File, name: String, @@ -88,21 +101,19 @@ internal object TestJars { } 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") + val aar = requeryAar() 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") + val aar = gradleCacheFile("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") + val aar = gradleCacheFile("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") } @@ -116,7 +127,7 @@ internal object TestJars { } val androidxCoreJar: File by lazy { - val aar = gradleCacheAar("androidx.core", "core", "1.13.1", "core-1.13.1.aar") + val aar = gradleCacheFile("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") } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt index 84f83ebcb5..e3290bc65c 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteTestSupportTest.kt @@ -26,6 +26,23 @@ class SqliteTestSupportTest { assertThat(gradleUserHome(null, null, home)).isEqualTo(File(home, ".gradle")) } + @Test + fun `the requery AAR is found when only a Gradle user home other than the default one has it`( + @TempDir dir: File, + ) { + val home = File(dir, "home") + // the default home's cache exists, without requery + File(home, ".gradle/caches/modules-2/files-2.1").mkdirs() + val active = File(dir, "active-gradle-home") + val aar = File(active, "caches/modules-2/files-2.1/com.github.requery/sqlite-android/3.49.0/0123abcd/sqlite-android-3.49.0.aar") + aar.parentFile.mkdirs() + aar.writeText("aar") + + assertThat(requeryAar(env = null, gradleHome = gradleUserHome(active.path, null, home))).isEqualTo(aar) + // GRADLE_USER_HOME, as when the tests run from an IDE + assertThat(requeryAar(env = null, gradleHome = gradleUserHome(null, active.path, home))).isEqualTo(aar) + } + @Test fun `a constructor resolves in the named class only, while methods are still inherited`() { val classes = From 85c3068a69a73ea941e6f00f282be991413fab45 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 02:19:40 +0000 Subject: [PATCH 50/56] ADFA-7138: Warn, not fail, for an unresolvable owner unless the member names an SQLite type Libraries reference optional classes the app does not ship, so a bare name+descriptor match (isStrict()Z, isString(I)Z) on an unresolvable owner failed ordinary app builds with an SQLite message. It now warns; it still fails when the descriptor names an SQLite, requery, Cursor, CursorWindow, CancellationSignal or DatabaseErrorHandler type. Tests: a warn-only case (com.unknown.Row.isString), a still-fails case (SQLite descriptor), and the existing unresolved-owner test now expects isStrict()Z to warn. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba --- gradle-plugin/docs/sqlite-rewrite.md | 7 +- .../gradle/sqlite/SqliteRewriteRules.kt | 8 +- .../gradle/sqlite/SqliteRewriter.kt | 24 ++++-- .../gradle/sqlite/SqliteRewriterTest.kt | 74 +++++++++++++++++-- 4 files changed, 99 insertions(+), 14 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index ab502ab813..58aafa2795 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -92,8 +92,11 @@ cannot load, so the transform runs only on projects that opted in. Commenting th it. Retyping the override would mean turning requery's signal back into Android's inside its body. Likewise a call on an owner that AGP's class loader cannot resolve fails when its name and descriptor are those of a member a rule above changes or rejects in a renamed class another class can extend (not the final - `SQLiteDatabase`, `SQLiteQuery` or `SQLiteStatement`): the owner may inherit that member, and its renamed - ancestors are unknown. + `SQLiteDatabase`, `SQLiteQuery` or `SQLiteStatement`) and the descriptor names an SQLite-related type (any + `android.database.sqlite` or requery type, `Cursor`, `CursorWindow`, `CancellationSignal`, + `DatabaseErrorHandler`): the owner may inherit that member, and its renamed ancestors are unknown. When the + descriptor names none (`isStrict()Z`, `isString(I)Z`), the call only logs a warning: libraries reference optional + classes the app does not ship, and failing every app build over them would be worse than missing the case. - 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 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 index e378bd03f5..2cd4d0ef80 100644 --- 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 @@ -247,7 +247,9 @@ internal object SqliteRewriteRules { /** * Name plus descriptor of each member that a rule changes or rejects and that another class can inherit (not a * constructor, nor a member of a final class), mapped to the renamed class that declares it. A class the build - * cannot resolve may inherit one of these, so a call that matches one exactly cannot be checked. + * cannot resolve may inherit one of these, so a call that matches one exactly cannot be checked. Such a call + * fails the build only when its descriptor names an SQLite-related type ([SQLITE_DESCRIPTOR_TOKENS]); otherwise + * it is a warning, since a bare name and descriptor like isStrict()Z is common in unrelated classes. */ val INHERITABLE_RULE_MEMBERS: Map = buildMap { @@ -299,6 +301,10 @@ internal object SqliteRewriteRules { /** How a renamed class appears in a descriptor. */ val RENAMED_DESCRIPTOR_TOKENS: List = RENAMES.keys.map { "L$it;" } + /** What marks a descriptor as SQLite-related: a class in either package, or one of the Cursor and signal types. */ + val SQLITE_DESCRIPTOR_TOKENS: List = + listOf("L$SQLITE_PKG", "L$REQUERY_DB_PKG", CUR, "L$CURSOR_WINDOW;", "L$ANDROID_SIGNAL;", "L$CORE_SIGNAL;", "L$ERROR_HANDLER;") + val CONTEXT_FAMILY: Set = setOf( CONTEXT, 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 74c94e0270..5b2d369445 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 @@ -211,6 +211,7 @@ object SqliteRewriter { val warn: (String) -> Unit, ) { val violations = linkedSetOf() + private val warned = hashSetOf() fun run() { if (policy.fail) { @@ -506,10 +507,12 @@ object SqliteRewriter { } /** - * Why `owner.name desc` cannot be checked, or null: [owner] cannot be resolved, so its renamed ancestors are - * unknown, and the member has the name and descriptor of one a rule changes or rejects in a renamed class - * another class can extend. Like a Context database method on an unresolved owner, this fails under the - * full policy rather than pass a call that may break at run time. + * Why `owner.name desc` cannot be checked and must fail the build, or null: [owner] cannot be resolved, so its + * renamed ancestors are unknown, the member has the name and descriptor of one a rule changes or rejects in a + * renamed class another class can extend, and the descriptor names an SQLite-related type. Like a Context + * database method on an unresolved owner, this fails under the full policy rather than pass a call that may + * break at run time. A match on name and descriptor alone (isStrict()Z, isString(I)Z) is only a warning: + * libraries reference optional classes the app does not ship, and most of them are not SQLite classes. */ fun unresolvedOwner( owner: String, @@ -521,8 +524,17 @@ object SqliteRewriter { return null } if (superTypes(owner) != null) return null - return "${owner.replace('/', '.')} cannot be resolved, so the rewrite cannot tell whether its $name$desc is " + - "${simple(declaring)}.$name, which it changes or rejects" + val cannot = "${owner.replace('/', '.')} cannot be resolved, so the rewrite cannot tell whether its $name$desc is " + if (Rules.SQLITE_DESCRIPTOR_TOKENS.any { desc.contains(it) }) { + return cannot + "${simple(declaring)}.$name, which it changes or rejects" + } + if (policy.fail) { + val message = + "${cn.name.replace('/', '.')}: $cannot${simple(declaring)}.$name, which it changes or rejects; " + + "the call is left as it is, and fails at run time if the owner extends that class" + if (warned.add(message)) warn(message) + } + return null } fun typeInsn( 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 55b5fc1a51..b1bab15fa5 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 @@ -1467,16 +1467,80 @@ class SqliteRewriterTest { ), ), ) - val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(classes["p/UsesLost"]!!, hierarchy) } + val warnings = mutableListOf() + val bytes = classes["p/UsesLost"]!! + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(bytes, hierarchy, warn = warnings::add) } val unresolved = e.message!! .lines() .filter { "p.Lost cannot be resolved" in it } .map { it.substringAfter("whether its ").substringBefore(" is ") } - // SQLiteDatabase is final, so no class inherits its rawQuery, and no rule has a delete(String) - assertThat(unresolved).hasSize(2) - assertThat(unresolved[0]).isEqualTo("isStrict()Z") - assertThat(unresolved[1]).startsWith("query(Landroid/database/sqlite/SQLiteDatabase;") + // SQLiteDatabase is final, so no class inherits its rawQuery, and no rule has a delete(String). + // isStrict()Z names no SQLite type, so it only warns; the query descriptor names SQLiteDatabase, so it fails. + assertThat(unresolved).hasSize(1) + assertThat(unresolved[0]).startsWith("query(Landroid/database/sqlite/SQLiteDatabase;") + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.Lost cannot be resolved") + assertThat(warnings.single()).contains("isStrict()Z") + } + + @Test + fun `an unresolvable owner whose member names no SQLite type only warns`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesRow", + """ + package p; + public class UsesRow { + boolean go(com.unknown.Row r) { return r.isString(0); } + } + """, + ), + // left out of the hierarchy, as an optional class the app does not ship is + src("com.unknown.Row", "package com.unknown; public class Row { public boolean isString(int i) { return false; } }"), + ), + ) + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/UsesRow"]!!, hierarchy, warn = warnings::add) + assertThat(warnings).hasSize(1) + assertThat(warnings.single()).contains("p.UsesRow") + assertThat(warnings.single()).contains("com.unknown.Row cannot be resolved") + assertThat(warnings.single()).contains("isString(I)Z") + } + + @Test + fun `an unresolvable owner whose member names an SQLite type still fails`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesQb", + """ + package p; + import android.database.sqlite.*; + public class UsesQb { + void go(com.unknown.Qb q, SQLiteDatabase db, String[] a, android.os.CancellationSignal s) { + q.query(db, a, "w", a, null, null, null, "1", s); + } + } + """, + ), + src( + "com.unknown.Qb", + """ + package com.unknown; + public class Qb extends android.database.sqlite.SQLiteQueryBuilder {} + """, + ), + ), + ) + val warnings = mutableListOf() + val bytes = classes["p/UsesQb"]!! + val e = assertThrows(SqliteRewriteException::class.java) { SqliteRewriter.rewrite(bytes, hierarchy, warn = warnings::add) } + assertThat(e.message).contains("com.unknown.Qb cannot be resolved") + assertThat(warnings).isEmpty() } @Test From d392bf6417062bc294441ff03dacf9db40fc01ed Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 18:52:27 -0800 Subject: [PATCH 51/56] ADFA-7138 Fix the rewriter tests' comments about getUniqueId after the unresolved-owner change "a call whose name a rule lists ... still looks the owner up" wrapped the rewrite in a try/catch because getUniqueId()I on the unresolvable p.Mine failed the build. Since 85c3068a6 that call only warns (its descriptor names no SQLite type), so the catch caught nothing and its comment was wrong. The try/catch is gone, which also makes the test fail if the call fails the build again. "hierarchy lookups are made once per owner ..." keeps its try/catch: its getUniqueId is on SQLiteStatement, a renamed class that resolves, and still fails the build through the missing-member rule. Its comment now says so. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/sqlite/SqliteRewriterTest.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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 b1bab15fa5..3dc4fb995f 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 @@ -502,7 +502,7 @@ class SqliteRewriterTest { try { SqliteRewriter.rewrite(classes["p/Chatty"]!!, counting) } catch (e: SqliteRewriteException) { - // getUniqueId is reported, which is not what this test is about + // SQLiteStatement.getUniqueId fails the build (requery lacks it), 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() @@ -535,17 +535,14 @@ class SqliteRewriterTest { ), ) val asked = mutableListOf() - try { - SqliteRewriter.rewrite( - classes["p/Named"]!!, - ClassHierarchy { name -> - asked += name - null - }, - ) - } catch (e: SqliteRewriteException) { - // getUniqueId on an owner that cannot be resolved is reported, which is not what this test is about - } + // getUniqueId()I names no SQLite type, so on an owner that cannot be resolved it only warns + SqliteRewriter.rewrite( + classes["p/Named"]!!, + ClassHierarchy { name -> + asked += name + null + }, + ) assertThat(asked).containsExactly("p/Mine", "p/Other") } From 56e26ffd8542b1b034df07fb9aa5eedb4b0c198e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 18:52:47 -0800 Subject: [PATCH 52/56] ADFA-7138 Docs: say which rule-table matches on an unresolvable class fail and which warn The known-limits line about getWindow() on an unresolvable SQLiteCursor subclass now also states the split 85c3068a6 made: on a class the build cannot resolve, a call that matches the rule table fails the build only when its descriptor names an SQLite-related type, and any other match (isString(I)Z, isStrict()Z) logs a warning, is left as it is, and fails at run time if the class does extend the renamed class. Docs only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 58aafa2795..6061dc6f6b 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -223,7 +223,10 @@ dependency, and runs the script on both APKs. The release must pass. The debug A reported. - On a `SQLiteCursor` subclass whose class the build cannot resolve, `getWindow()` is not detected, and the call fails at run time: it is not in the rule table, and failing every unresolved call that returns a `CursorWindow` would fail - ordinary apps (android's kept `AbstractWindowedCursor` and `CrossProcessCursor` declare it too). + ordinary apps (android's kept `AbstractWindowedCursor` and `CrossProcessCursor` declare it too). On any class the + build cannot resolve, a call that matches the rule table fails the build only when its descriptor names an + SQLite-related type (see Rules); any other match, such as `isString(I)Z` or `isStrict()Z`, logs a warning and is left + as it is, and fails at run time if the class does extend the renamed class. - `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. From 36bdf9639a5970ed8098e56aa4edee03653dec6f Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 18:53:46 -0800 Subject: [PATCH 53/56] ADFA-7138 Pin the SQLite descriptor tokens and which unresolved-owner matches fail 85c3068a6 fails a rule-table match on an unresolvable owner only when the descriptor holds one of SQLITE_DESCRIPTOR_TOKENS. The test pins the tokens as descriptor forms (a package prefix after L, or a full L...; type), and the split they make of the 34 inheritable table members: 8 fail (setWindow, mWindow, setOpenParams, getCursorFactory, insert, update, delete, and query with a signal) and 26 warn, among them isString(I)Z, isStrict()Z and getUniqueId()I. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/sqlite/SqliteRewriteRulesTest.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt index e9e416aa6f..d8eb3a6042 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt @@ -1,5 +1,6 @@ package com.itsaky.androidide.gradle.sqlite +import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage import org.junit.jupiter.api.Test import org.objectweb.asm.Type @@ -17,4 +18,35 @@ class SqliteRewriteRulesTest { } } } + + @Test + fun `the SQLite descriptor tokens, and which table matches fail on an unresolvable owner`() { + assertThat(SqliteRewriteRules.SQLITE_DESCRIPTOR_TOKENS) + .containsExactly( + "Landroid/database/sqlite/", + "Lio/requery/android/database/", + "Landroid/database/Cursor;", + "Landroid/database/CursorWindow;", + "Landroid/os/CancellationSignal;", + "Landroidx/core/os/CancellationSignal;", + "Landroid/database/DatabaseErrorHandler;", + ).inOrder() + val tokens = SqliteRewriteRules.SQLITE_DESCRIPTOR_TOKENS + // a key is a name plus a descriptor, and no name holds a token + val (failing, warned) = SqliteRewriteRules.INHERITABLE_RULE_MEMBERS.keys.partition { key -> tokens.any { key.contains(it) } } + + assertThat(failing.map { it.substringBefore('(').removeSuffix("Landroid/database/CursorWindow;") }) + .containsExactly("setWindow", "mWindow", "setOpenParams", "getCursorFactory", "insert", "update", "delete", "query") + assertThat(warned).hasSize(26) + assertThat(warned).containsAtLeast( + "isDistinct()Z", + "isStrict()Z", + "getUniqueId()I", + "isString(I)Z", + "isLong(I)Z", + "isFloat(I)Z", + "isBlob(I)Z", + "getProjectionMap()Ljava/util/Map;", + ) + } } From fbd445136a815b8f6428d36f93898ec700ee0733 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 18:53:46 -0800 Subject: [PATCH 54/56] ADFA-7138 Check in a real AGP build that the unresolved-owner warning reaches the log The rewriter's default warn is the Gradle logger, which the unit tests replace. SqliteRewriteTransformTest's library AAR now also holds a call to com.unknown.Row.isString(I)Z, with Row left out of the AAR as an optional class a library references and the app does not ship. The same TestKit build that already runs AGP's transform over the AAR now checks that its output carries the warning, and that the build still succeeds. No extra build. Test code only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../gradle/SqliteRewriteTransformTest.kt | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) 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 index 08d5583ff3..3d3ba57261 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt @@ -84,14 +84,22 @@ class SqliteRewriteTransformTest { ) { 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); } }", + 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); } " + + "public boolean probe(com.unknown.Row r) { return r.isString(0); } }", + ), + JavaSource( + "com.unknown.Row", + "package com.unknown; public class Row { public boolean isString(int column) { return false; } }", + ), ), - ), - ) + ) + // left out of the AAR, as an optional class a library references and the app does not ship + .filterKeys { it != "com/unknown/Row" } val helperSource = FileProvider .projectRoot() @@ -155,6 +163,13 @@ class SqliteRewriteTransformTest { assertThat(result.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.task(":app:transformDemoDebugClassesWithAsm")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) + // isString(I)Z names no SQLite type, so the owner that cannot be resolved is only reported through Gradle's logger + val warning = + "x.lib.$daoName: com.unknown.Row cannot be resolved, so the rewrite cannot tell whether its isString(I)Z is " + + "SQLiteCursor.isString, which it changes or rejects; the call is left as it is, and fails at run time if the owner " + + "extends that class" + assertThat(result.output).contains(warning) + println("WARNING LINE: " + result.output.lines().first { warning in it }) // 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) } } From 6e16bdb7c20af833fef9da85819275fc6fa3a7e2 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 21:14:40 -0800 Subject: [PATCH 55/56] ADFA-7138 Hold CancelForwarders weakly so a dropped signal's forwarder is not kept SqliteRewriteRuntime.FORWARDERS mapped each Android CancellationSignal weakly to its CancelForwarder, but a WeakHashMap keeps the value of a cleared key until the next call on the map. So each signal the app dropped left its forwarder (72 B) reachable from the static map until the next conversion. LeakCanary and a heap count on the Note 20 (C6h probe) showed both forwarders of two dropped signals retained this way after the activity was destroyed. The value is now a WeakReference. While the signal lives, its own listener field keeps the forwarder alive; a cleared reference means a new forwarder. One accepted change: a caller that sets its own listener after a query lets that forwarder go, so a later conversion starts a new one and the queries from before the caller's listener no longer stop with the signal (they had stopped doing so when the caller replaced the listener). Tests in SqliteRewriteRuntimeTest: a dropped signal's forwarder clears after GC (fails without the fix: the stale entry keeps it), a live signal keeps its forwarder through GC and still cancels, and a signal whose forwarder was collected gets a working new one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 6 +- .../gradle/sqlite/SqliteRewriteRuntimeTest.kt | 72 +++++++++++++++++++ .../sqliterewrite/SqliteRewriteRuntime.java | 14 ++-- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 6061dc6f6b..703b2ccb46 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -239,6 +239,10 @@ dependency, and runs the script on both APKs. The release must pass. The debug A androidx.core.os.OperationCanceledException e)`, which works with the SQLite debugger on and off. - Android's `CancellationSignal` holds one listener: the conversion replaces any listener the caller set. Each query gets its own androidx.core signal, and cancelling the Android signal cancels every one still in use, so - two lazy queries that share it both stop (requery keeps a query's signal for its cursor's later reads). + two lazy queries that share it both stop (requery keeps a query's signal for its cursor's later reads). The + runtime's forwarder is held only as the Android signal's listener (`SqliteRewriteRuntime.FORWARDERS` holds signals + and forwarders weakly), so nothing of a signal the app dropped is kept: a strong value outlived its key until the + map's next call. A caller that sets its own listener after a query lets that forwarder go: the next query on the + signal gets a new one, and queries from before the caller's listener no longer stop with the signal. - 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/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 index 02324e9e6c..fec4064e9b 100644 --- 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 @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage import com.itsaky.androidide.utils.FileProvider import org.junit.jupiter.api.Test +import java.lang.ref.WeakReference /** * The runtime helper's functions that need no device, run on the JVM from its bytecode compiled against android.jar, @@ -42,6 +43,41 @@ class SqliteRewriteRuntimeTest { private fun isCanceled(signal: Any) = signal.javaClass.getMethod("isCanceled").invoke(signal) as Boolean + /** The signal's listener, read from the signal itself: a call on FORWARDERS would expunge its stale entries. */ + private fun listenerOf(signal: Any): Any? = androidSignal.getDeclaredField("listener").apply { isAccessible = true }.get(signal) + + private fun setListener( + signal: Any, + listener: Any?, + ) { + val type = loader.loadClass("android.os.CancellationSignal\$OnCancelListener") + androidSignal.getMethod("setOnCancelListener", type).invoke(signal, listener) + } + + /** Converts a signal that nothing else holds, in its own frame, and returns a weak reference to its forwarder. */ + private fun forwarderOfDroppedSignal(): WeakReference { + val signal = newAndroidSignal() + convert(signal) + return WeakReference(listenerOf(signal)!!) + } + + /** + * Requests GC until [ref] clears, at most [rounds] times. Timing-dependent only in the passing direction: HotSpot's + * System.gc() is a full collection and clears a weak reference in the first round, and the bound only ends a run in + * which it never clears. Nothing calls FORWARDERS meanwhile, as any call on it expunges the stale entries. + */ + private fun clearedAfterGc( + ref: WeakReference<*>, + rounds: Int = 50, + ): Boolean { + repeat(rounds) { + if (ref.get() == null) return true + System.gc() + Thread.sleep(20) + } + return ref.get() == null + } + private fun translate(flags: Int) = runtime.getMethod("translateOpenFlags", Int::class.javaPrimitiveType).invoke(null, flags) as Int private val readOnly = 1 @@ -89,6 +125,42 @@ class SqliteRewriteRuntimeTest { assertThat(isCanceled(convert(signal))).isTrue() } + @Test + fun `the forwarder of a signal the app dropped is not kept`() { + val forwarder = forwarderOfDroppedSignal() + + assertWithMessage("CancelForwarder of a dropped signal, kept by the value of FORWARDERS' stale entry") + .that(clearedAfterGc(forwarder)) + .isTrue() + } + + @Test + fun `a live signal keeps its forwarder through a GC and still cancels its queries`() { + val signal = newAndroidSignal() + val converted = convert(signal) + val forwarder = WeakReference(listenerOf(signal)) + + assertWithMessage("forwarder collected while its signal lives").that(clearedAfterGc(forwarder, rounds = 3)).isFalse() + cancel(signal) + + assertThat(isCanceled(converted)).isTrue() + } + + @Test + fun `a signal whose forwarder was collected gets a new one`() { + val signal = newAndroidSignal() + convert(signal) + val first = WeakReference(listenerOf(signal)) + // the caller sets its own listener, so nothing holds the first forwarder any more + setListener(signal, null) + assertWithMessage("first forwarder collected").that(clearedAfterGc(first)).isTrue() + + val converted = convert(signal) + cancel(signal) + + assertWithMessage("a query converted after the forwarder was collected").that(isCanceled(converted)).isTrue() + } + private companion object { /** Android's listener rules: one listener, a new one runs at once on a cancelled signal, the same one does not. */ val ANDROID_SIGNAL = 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 index 9fc44c855a..7917a9c3bb 100644 --- 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 @@ -46,8 +46,10 @@ public final class SqliteRewriteRuntime { private static final int CONTEXT_MODE_ENABLE_WRITE_AHEAD_LOGGING = 0x8; - /** The listener each Android signal forwards cancellation through; weak keys, so a signal the app dropped goes. */ - private static final Map FORWARDERS = new WeakHashMap<>(); + /** + * The listener each Android signal forwards cancellation through. Weak keys, so a signal the app dropped goes, and weak values: a WeakHashMap keeps the value of a cleared key until a later call on the map, so a strong value kept a dropped signal's forwarder alive until the next conversion. The signal's own listener field keeps its forwarder alive while the signal lives. + */ + private static final Map> FORWARDERS = new WeakHashMap<>(); /** * 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. @@ -140,7 +142,7 @@ public static String stringForQuery(SQLiteStatement prog, String[] selectionArgs } /** - * Android's signal for requery's. Each call returns a new requery signal, and cancelling Android's cancels every one of them still in use: requery keeps a query's signal for the cursor's later reads, so two lazy queries that share an Android signal must both stop. 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). + * Android's signal for requery's. Each call returns a new requery signal, and cancelling Android's cancels every one of them still in use: requery keeps a query's signal for the cursor's later reads, so two lazy queries that share an Android signal must both stop. 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). The forwarder lives only as long as it is that listener: if the caller sets its own listener after a query, the forwarder can be collected, the next call starts a new one, and queries converted before the caller's listener no longer stop with the signal. */ public static androidx.core.os.CancellationSignal toCancellationSignal( CancellationSignal signal) { @@ -150,12 +152,14 @@ public static androidx.core.os.CancellationSignal toCancellationSignal( final androidx.core.os.CancellationSignal converted = new androidx.core.os.CancellationSignal(); CancelForwarder forwarder; synchronized (FORWARDERS) { - forwarder = FORWARDERS.get(signal); + WeakReference ref = FORWARDERS.get(signal); + forwarder = ref == null ? null : ref.get(); if (forwarder == null) { forwarder = new CancelForwarder(); - FORWARDERS.put(signal, forwarder); + FORWARDERS.put(signal, new WeakReference<>(forwarder)); } } + // the local keeps the new forwarder alive until setOnCancelListener stores it in the signal forwarder.add(converted); signal.setOnCancelListener(forwarder); // setOnCancelListener does not call a listener that is already set, so a signal cancelled before this call From b0ceebe3e6d672255f72a5c6721994e70d758391 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 22:04:40 -0800 Subject: [PATCH 56/56] ADFA-7138 Warn, not fail, for an unresolvable owner's setWindow(CursorWindow) and mWindow, and say where android.database.CursorWindow is not an SQLite type. It carries every ContentProvider cursor across processes, and Android's kept AbstractWindowedCursor declares both setWindow(CursorWindow) and mWindow, so a library's own windowed cursor on an owner the build cannot resolve matched SQLiteCursor's two members and failed the build. CursorWindow leaves SQLITE_DESCRIPTOR_TOKENS, so those two matches now only warn: of the 34 inheritable rule members, 6 fail on an unresolvable owner and 28 warn. Cursor stays a token; it changes no outcome today. The unresolved-owner warning named only the class, so a user could not find the call. It now starts with class, method and line, formatted by the same site() the string-constant warnings use, and says "field access" instead of "call" for mWindow, the first field that reaches the warning. It is still logged once per message, and the message now holds the method and line: two calls on one line warn once, two lines warn twice. Tests: an unresolvable windowed cursor's setWindow and mWindow only warn; the warning names method and line, once per place; the pinned tokens and the 6/28 split; the TestKit log line. Docs: CursorWindow leaves the list of types that make a match fail, and the known limit on getWindow() says setWindow and mWindow now only warn. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- gradle-plugin/docs/sqlite-rewrite.md | 23 +++--- .../gradle/sqlite/SqliteRewriteRules.kt | 12 +++- .../gradle/sqlite/SqliteRewriter.kt | 47 +++++++----- .../gradle/SqliteRewriteTransformTest.kt | 5 +- .../gradle/sqlite/SqliteRewriteRulesTest.kt | 10 +-- .../gradle/sqlite/SqliteRewriterTest.kt | 72 +++++++++++++++++++ 6 files changed, 135 insertions(+), 34 deletions(-) diff --git a/gradle-plugin/docs/sqlite-rewrite.md b/gradle-plugin/docs/sqlite-rewrite.md index 703b2ccb46..c44aa062fe 100644 --- a/gradle-plugin/docs/sqlite-rewrite.md +++ b/gradle-plugin/docs/sqlite-rewrite.md @@ -93,10 +93,13 @@ cannot load, so the transform runs only on projects that opted in. Commenting th Likewise a call on an owner that AGP's class loader cannot resolve fails when its name and descriptor are those of a member a rule above changes or rejects in a renamed class another class can extend (not the final `SQLiteDatabase`, `SQLiteQuery` or `SQLiteStatement`) and the descriptor names an SQLite-related type (any - `android.database.sqlite` or requery type, `Cursor`, `CursorWindow`, `CancellationSignal`, - `DatabaseErrorHandler`): the owner may inherit that member, and its renamed ancestors are unknown. When the - descriptor names none (`isStrict()Z`, `isString(I)Z`), the call only logs a warning: libraries reference optional - classes the app does not ship, and failing every app build over them would be worse than missing the case. + `android.database.sqlite` or requery type, `Cursor`, `CancellationSignal`, `DatabaseErrorHandler`): the owner may + inherit that member, and its renamed ancestors are unknown. When the descriptor names none (`isStrict()Z`, + `isString(I)Z`, `setWindow(CursorWindow)`, `mWindow`), the call or field access only logs a warning with class, + method and line: libraries reference optional classes the app does not ship, and failing every app build over them + would be worse than missing the case. `CursorWindow` is not an SQLite type: it carries every ContentProvider cursor + across processes, and android's kept `AbstractWindowedCursor` declares `setWindow(CursorWindow)` and `mWindow` too, + so a library's own windowed cursor matches them. - 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 @@ -221,12 +224,14 @@ dependency, and runs the script on both APKs. The release must pass. The debug A - 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. -- On a `SQLiteCursor` subclass whose class the build cannot resolve, `getWindow()` is not detected, and the call fails - at run time: it is not in the rule table, and failing every unresolved call that returns a `CursorWindow` would fail - ordinary apps (android's kept `AbstractWindowedCursor` and `CrossProcessCursor` declare it too). On any class the +- On a `SQLiteCursor` subclass whose class the build cannot resolve, `getWindow()` is not detected, and + `setWindow(CursorWindow)` and `mWindow` only log a warning; each fails at run time. `getWindow()` is not in the rule + table, and failing every unresolved member that names a `CursorWindow` would fail ordinary apps (android's kept + `AbstractWindowedCursor` declares all three, and `CrossProcessCursor` declares `getWindow()`). On any class the build cannot resolve, a call that matches the rule table fails the build only when its descriptor names an - SQLite-related type (see Rules); any other match, such as `isString(I)Z` or `isStrict()Z`, logs a warning and is left - as it is, and fails at run time if the class does extend the renamed class. + SQLite-related type (see Rules); any other match, such as `isString(I)Z`, `isStrict()Z` or `setWindow(CursorWindow)`, + logs a warning with class, method and line and is left as it is, and fails at run time if the class does extend the + renamed class. - `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. 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 index 2cd4d0ef80..d06adc1c59 100644 --- 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 @@ -249,7 +249,8 @@ internal object SqliteRewriteRules { * constructor, nor a member of a final class), mapped to the renamed class that declares it. A class the build * cannot resolve may inherit one of these, so a call that matches one exactly cannot be checked. Such a call * fails the build only when its descriptor names an SQLite-related type ([SQLITE_DESCRIPTOR_TOKENS]); otherwise - * it is a warning, since a bare name and descriptor like isStrict()Z is common in unrelated classes. + * it is a warning, since a bare name and descriptor like isStrict()Z, or setWindow(CursorWindow) on any windowed + * cursor, is common in unrelated classes. */ val INHERITABLE_RULE_MEMBERS: Map = buildMap { @@ -301,9 +302,14 @@ internal object SqliteRewriteRules { /** How a renamed class appears in a descriptor. */ val RENAMED_DESCRIPTOR_TOKENS: List = RENAMES.keys.map { "L$it;" } - /** What marks a descriptor as SQLite-related: a class in either package, or one of the Cursor and signal types. */ + /** + * What marks a descriptor as SQLite-related: a class in either package, Cursor, the signals, or the error handler. + * Not android.database.CursorWindow, which is not an SQLite type: it carries every ContentProvider cursor across + * processes, and android's kept AbstractWindowedCursor declares setWindow(CursorWindow) and mWindow too, so a + * library's own windowed cursor on an owner the build cannot resolve would match SQLiteCursor's. + */ val SQLITE_DESCRIPTOR_TOKENS: List = - listOf("L$SQLITE_PKG", "L$REQUERY_DB_PKG", CUR, "L$CURSOR_WINDOW;", "L$ANDROID_SIGNAL;", "L$CORE_SIGNAL;", "L$ERROR_HANDLER;") + listOf("L$SQLITE_PKG", "L$REQUERY_DB_PKG", CUR, "L$ANDROID_SIGNAL;", "L$CORE_SIGNAL;", "L$ERROR_HANDLER;") val CONTEXT_FAMILY: Set = setOf( 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 5b2d369445..2baf2d0ddd 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 @@ -440,10 +440,9 @@ object SqliteRewriter { "SQL text uses COLLATE UNICODE, which does not exist in requery's SQLite (no ICU); the statement would fail", ) } - val where = if (line >= 0) "line $line" else "no line number" if (Rules.COLLATE_LOCALIZED.containsMatchIn(text)) { warn( - "${cn.name.replace('/', '.')}.${m.name} ($where): COLLATE LOCALIZED works in requery's SQLite but sorts by " + + "${site(m, line)}: COLLATE LOCALIZED works in requery's SQLite but sorts by " + "byte value (no ICU), not by the device locale", ) } @@ -451,12 +450,20 @@ object SqliteRewriter { // this name is Android's, which the rewritten code cannot mix with requery's. Rules.RENAMED_CLASS_NAME.find(text)?.let { warn( - "${cn.name.replace('/', '.')}.${m.name} ($where): the string \"${it.value}\" names a class the rewrite moves " + + "${site(m, line)}: the string \"${it.value}\" names a class the rewrite moves " + "to requery. Strings are not rewritten: Class.forName or reflection on it reaches Android's class, not requery's", ) } } + fun where(line: Int) = if (line >= 0) "line $line" else "no line number" + + /** Where a warning points: class, method and line, as `p.Dao.query (line 12)`. */ + fun site( + m: MethodNode, + line: Int, + ) = "${cn.name.replace('/', '.')}.${m.name} (${where(line)})" + fun invokeDynamic( m: MethodNode, insn: InvokeDynamicInsnNode, @@ -478,8 +485,7 @@ object SqliteRewriter { what: String, ) { if (!policy.fail) return - val where = if (line >= 0) "line $line" else "no line number" - violations += "${m.name}${m.desc} ($where): $what" + violations += "${m.name}${m.desc} (${where(line)}): $what" } fun fieldInsn( @@ -487,10 +493,13 @@ object SqliteRewriter { f: FieldInsnNode, line: Int, ) { - fieldProblem(f.owner, f.name, f.desc)?.let { violate(m, line, it) } + fieldProblem(m, line, f.owner, f.name, f.desc)?.let { violate(m, line, it) } } + /** [m] and [line] locate the access, for a warning. */ fun fieldProblem( + m: MethodNode, + line: Int, owner: String, name: String, desc: String, @@ -503,7 +512,7 @@ object SqliteRewriter { return renamedAncestors(owner) .firstOrNull { Rules.MISSING_MEMBERS["$it.$name"]?.contains(desc) == true } ?.let { "${simple(it)}.$name does not exist in requery" } - ?: unresolvedOwner(owner, name, desc) + ?: unresolvedOwner(m, line, owner, name, desc) } /** @@ -511,10 +520,13 @@ object SqliteRewriter { * renamed ancestors are unknown, the member has the name and descriptor of one a rule changes or rejects in a * renamed class another class can extend, and the descriptor names an SQLite-related type. Like a Context * database method on an unresolved owner, this fails under the full policy rather than pass a call that may - * break at run time. A match on name and descriptor alone (isStrict()Z, isString(I)Z) is only a warning: - * libraries reference optional classes the app does not ship, and most of them are not SQLite classes. + * break at run time. A match on name and descriptor alone (isStrict()Z, isString(I)Z, setWindow(CursorWindow)) + * is only a warning, naming class, method and [line] of [m]: libraries reference optional classes the app does + * not ship, and most of them are not SQLite classes. The same warning at the same place is logged once. */ fun unresolvedOwner( + m: MethodNode, + line: Int, owner: String, name: String, desc: String, @@ -529,9 +541,10 @@ object SqliteRewriter { return cannot + "${simple(declaring)}.$name, which it changes or rejects" } if (policy.fail) { + val use = if (desc.startsWith("(")) "call" else "field access" val message = - "${cn.name.replace('/', '.')}: $cannot${simple(declaring)}.$name, which it changes or rejects; " + - "the call is left as it is, and fails at run time if the owner extends that class" + "${site(m, line)}: $cannot${simple(declaring)}.$name, which it changes or rejects; " + + "the $use is left as it is, and fails at run time if the owner extends that class" if (warned.add(message)) warn(message) } return null @@ -587,9 +600,11 @@ object SqliteRewriter { /** * 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. + * policy allows; a [Action.Fail] is reported only when it fails. [m] and [line] locate the call, for a warning. */ fun decide( + m: MethodNode, + line: Int, opcode: Int, owner: String, name: String, @@ -628,7 +643,7 @@ object SqliteRewriter { "the platform method would not link", ) } - unresolvedOwner(owner, name, desc)?.let { return Action.Fail(it) } + unresolvedOwner(m, line, owner, name, desc)?.let { return Action.Fail(it) } return Action.None } for (c in ancestors) { @@ -656,7 +671,7 @@ object SqliteRewriter { mi: MethodInsnNode, line: Int, ) { - when (val action = decide(mi.opcode, mi.owner, mi.name, mi.desc)) { + when (val action = decide(m, line, mi.opcode, mi.owner, mi.name, mi.desc)) { Action.None -> {} is Action.Fail -> { @@ -699,7 +714,7 @@ object SqliteRewriter { val what = when (h.tag) { Opcodes.H_GETFIELD, Opcodes.H_GETSTATIC, Opcodes.H_PUTFIELD, Opcodes.H_PUTSTATIC -> { - fieldProblem(h.owner, h.name, h.desc) + fieldProblem(m, line, h.owner, h.name, h.desc) } else -> { @@ -710,7 +725,7 @@ object SqliteRewriter { Opcodes.H_INVOKEVIRTUAL -> Opcodes.INVOKEVIRTUAL else -> Opcodes.INVOKESPECIAL } - when (val action = decide(opcode, h.owner, h.name, h.desc)) { + when (val action = decide(m, line, opcode, h.owner, h.name, h.desc)) { Action.None -> null is Action.Fail -> action.what is Action.Redirect -> "the runtime takes this call over" 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 index 3d3ba57261..d05439852e 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/SqliteRewriteTransformTest.kt @@ -163,9 +163,10 @@ class SqliteRewriteTransformTest { assertThat(result.task(":app:checkDemoDebugSqliteRewriteClasspath")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.task(":app:transformDemoDebugClassesWithAsm")!!.outcome).isEqualTo(TaskOutcome.SUCCESS) - // isString(I)Z names no SQLite type, so the owner that cannot be resolved is only reported through Gradle's logger + // isString(I)Z names no SQLite type, so the owner that cannot be resolved is only reported through Gradle's logger, + // at the calling method and line (the library's source is one line) val warning = - "x.lib.$daoName: com.unknown.Row cannot be resolved, so the rewrite cannot tell whether its isString(I)Z is " + + "x.lib.$daoName.probe (line 1): com.unknown.Row cannot be resolved, so the rewrite cannot tell whether its isString(I)Z is " + "SQLiteCursor.isString, which it changes or rejects; the call is left as it is, and fails at run time if the owner " + "extends that class" assertThat(result.output).contains(warning) diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt index d8eb3a6042..e1b5ac1b14 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/sqlite/SqliteRewriteRulesTest.kt @@ -26,7 +26,6 @@ class SqliteRewriteRulesTest { "Landroid/database/sqlite/", "Lio/requery/android/database/", "Landroid/database/Cursor;", - "Landroid/database/CursorWindow;", "Landroid/os/CancellationSignal;", "Landroidx/core/os/CancellationSignal;", "Landroid/database/DatabaseErrorHandler;", @@ -35,9 +34,10 @@ class SqliteRewriteRulesTest { // a key is a name plus a descriptor, and no name holds a token val (failing, warned) = SqliteRewriteRules.INHERITABLE_RULE_MEMBERS.keys.partition { key -> tokens.any { key.contains(it) } } - assertThat(failing.map { it.substringBefore('(').removeSuffix("Landroid/database/CursorWindow;") }) - .containsExactly("setWindow", "mWindow", "setOpenParams", "getCursorFactory", "insert", "update", "delete", "query") - assertThat(warned).hasSize(26) + assertThat(failing.map { it.substringBefore('(') }) + .containsExactly("setOpenParams", "getCursorFactory", "insert", "update", "delete", "query") + // CursorWindow is not an SQLite type: any windowed cursor has setWindow(CursorWindow) and mWindow + assertThat(warned).hasSize(28) assertThat(warned).containsAtLeast( "isDistinct()Z", "isStrict()Z", @@ -47,6 +47,8 @@ class SqliteRewriteRulesTest { "isFloat(I)Z", "isBlob(I)Z", "getProjectionMap()Ljava/util/Map;", + "setWindow(Landroid/database/CursorWindow;)V", + "mWindowLandroid/database/CursorWindow;", ) } } 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 3dc4fb995f..f512ce5fe7 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 @@ -1507,6 +1507,78 @@ class SqliteRewriterTest { assertThat(warnings.single()).contains("isString(I)Z") } + @Test + fun `an unresolvable windowed cursor's setWindow(CursorWindow) and mWindow only warn`() { + val classes = + Compiler.compile( + listOf( + src( + "p.UsesWindowed", + """ + package p; + import android.database.CursorWindow; + public class UsesWindowed { + void reset(com.unknown.Windowed c, CursorWindow w) { c.setWindow(w); } + CursorWindow window(com.unknown.Windowed c) { return c.mWindow; } + } + """, + ), + // a library's own windowed cursor, as android's AbstractWindowedCursor declares both members; left + // out of the hierarchy, as an optional class the app does not ship is + src( + "com.unknown.Windowed", + """ + package com.unknown; + public class Windowed { + public android.database.CursorWindow mWindow; + public void setWindow(android.database.CursorWindow w) {} + } + """, + ), + ), + ) + val warnings = mutableListOf() + // CursorWindow is not an SQLite type: it carries every ContentProvider cursor across processes + SqliteRewriter.rewrite(classes["p/UsesWindowed"]!!, hierarchy, warn = warnings::add) + assertThat(warnings).hasSize(2) + assertThat(warnings[0]).contains("its setWindow(Landroid/database/CursorWindow;)V is SQLiteCursor.setWindow") + assertThat(warnings[0]).contains("the call is left as it is") + assertThat(warnings[1]).contains("its mWindowLandroid/database/CursorWindow; is SQLiteCursor.mWindow") + assertThat(warnings[1]).contains("the field access is left as it is") + } + + @Test + fun `an unresolved-owner warning names the method and line, once per place`() { + val source = + """ + package p; + public class Sites { + boolean one(com.unknown.Row r) { + return r.isString(0) && r.isString(1); + } + boolean two(com.unknown.Row r) { + return r.isString(2); + } + } + """ + val classes = + Compiler.compile( + listOf( + src("p.Sites", source), + src("com.unknown.Row", "package com.unknown; public class Row { public boolean isString(int i) { return false; } }"), + ), + ) + + fun lineOf(text: String) = source.lines().indexOfFirst { text in it } + 1 + val warnings = mutableListOf() + SqliteRewriter.rewrite(classes["p/Sites"]!!, hierarchy, warn = warnings::add) + // the two calls on one line make the same warning, logged once + assertThat(warnings.map { it.substringBefore(": ") }) + .containsExactly("p.Sites.one (line ${lineOf("r.isString(0)")})", "p.Sites.two (line ${lineOf("r.isString(2)")})") + .inOrder() + assertThat(warnings.map { it.substringAfter(": ") }.distinct().single()).startsWith("com.unknown.Row cannot be resolved") + } + @Test fun `an unresolvable owner whose member names an SQLite type still fails`() { val classes =