Skip to content

Android: Standardize the use of application id and namespace #5210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

vaslabs
Copy link
Contributor

@vaslabs vaslabs commented May 27, 2025

This PR standardises the use of application id and namespace. It is also one of the pre-requisites of making hilt instrumented tests to work.

The application id is a unique id for every android app , so the android manifest package depends on it.

The android manifest package is also used whenever there's a relative path for Applications and activities so

<manifest package="com.foo">
    <application android:name=".TodoApplication"/>
</manifest>

Becomes

<manifest package="com.foo">
    <application android:name="com.foo.TodoApplication"/>
</manifest>

During manifest merging. Because this happens when the manifest is loaded (before everything is merged) we start with the manifest package being the android namespace. We then pass the package property to the merger to be the application id (the package for each up is unique). This is what appears to be happening in AGP as well. (It is also the reason why I was confused when reverse engineering the AGP behaviour and was alternating between namespace and application id)

I've also cleaned up some of the xml manual modifications that are not needed anymore as are handled by the merger tool

@vaslabs
Copy link
Contributor Author

vaslabs commented May 27, 2025

I think the job failures are not related to these changes

@vaslabs vaslabs marked this pull request as ready for review May 27, 2025 10:35
@vaslabs vaslabs force-pushed the android/sort-out-application-namespace-and-id branch from 83932dd to 646b429 Compare May 27, 2025 11:41
Copy link
Member

@lihaoyi lihaoyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, happy to merge when you;re done with this

@vaslabs
Copy link
Contributor Author

vaslabs commented May 28, 2025

Looks good to me, happy to merge when you;re done with this

Yes, it's ready

@lihaoyi lihaoyi merged commit ce62087 into com-lihaoyi:main May 28, 2025
36 of 38 checks passed
@lefou lefou added this to the 1.0.0-RC2 milestone May 28, 2025
lihaoyi pushed a commit that referenced this pull request May 31, 2025
Completes the missing features to allow for android tests (instrumented
tests) to run for architecture-samples (hilt android Todo application)

## Changes

These were the necessary changes needed to make instrumented tests for
architecture samples work.

### Instrumented manifest

AGP seems to be creating a hardcoded temporary file (which is easy to
miss as the task is quite fast and the file is deleted once is
finished), but I've managed to pick it up with some hackery and it looks
like this

```XML
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.architecture.blueprints.main.test">
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="35"/>
  <application>
    <uses-library android:name="android.test.runner"/>
  </application>
  <instrumentation android:name="com.example.android.architecture.blueprints.todoapp.CustomTestRunner" android:targetPackage="com.example.android.architecture.blueprints.main" android:handleProfiling="false" android:functionalTest="false" android:label="Tests for com.example.android.architecture.blueprints.main"/>
</manifest>
```

The manifest merging for instrumented tests, seem to only be picking
manifest files from androidx.test packages, so I hardcoded that in and I
ended up with the same manifest as in AGP . The task to get is is
```bash
./gradlew :app:processDebugAndroidTestManifest
```

### Debug sources

I've added the debug sources as part of the AndroidAppModule, to keep
the scope of this PR small. I'll separate the debug from release
similarly to how R8 was done in this PR in the future. In order to avoid
making a lot of changes for the debug sources, I've included the debug
sources as part of the build.mill, which will probably be removed when
the release/debug separation work is completed.

### R8

I haven't managed to make the architecture-samples work with D8, whether
some meta-inf is missing or not, with R8 and the same configuration
choices as in the architecture-samples repo, everything works properly
(app, unit tests, instrumented tests). I've then separated the R8
functionality into its own trait.

It seems that by using R8 we can drop some of the manual code we have
(like picking the meta-inf files manually) and creating other needed
directories, as R8 seems to be doing all of that work. We can keep
purely d8 builds for legacy purposes if we want to of course, but we'll
see how it goes in the future when we integrate more apps.

For R8, desugaring being enabled is also the sane default, so I switched
that too, following up to failed builds of architecture-samples with R8.

### Cleanups

- Some manifest generated values are not handled by the merger tool
instead of manually constructing them
- Printing out ignored for stream output of instrumented tests along
with each line was confusing when dealing with failed tests, which I had
initially, so I removed it.

### Fixes
- Instrumented tests should run with androidApplicationId as the
package, as that identifies the android app, was missed [from the
previous PR](#5210)
- Instrumented packaged class files, also need to include the classfiles
of the modules they depend on (e.g. shared-tests in case of the
architecture-samples).

## Running experience

Performance-wise, this feels similar to AGP (I didn't do any benchmarks)


https://github.com/user-attachments/assets/94e29098-91c8-4fb2-af62-8294833d261a


## Summary

All in all, this is another milestone, where we have a fairly complex
android app, which uses KSP and R8, can be built, run and tested with
both unit and instrumented tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants