How to Verify the Integrity of Dynamically Loaded Android Code Before Loading
Dynamically loaded Android code can be modified or replaced. This creates a risk of access to sensitive data and malicious actions. Integrity verification must happen before the application loads the code: detecting a mismatch after execution is too late. We explain how to define an acceptance condition for one module, protect the expected hash, and test rejection using your own fictional data.
First, establish the need and the acceptance condition
Android recommends avoiding dynamic code loading unless there is a business need for it. The first task for the product owner and developer is therefore to determine whether it is necessary. If it is, define the acceptance rule for the specific module and the observable rejection outcome in advance.
For this approach, the rule is as follows: the application calculates the file’s hash and compares it with an expected value from a trusted source. Only a successful comparison allows loading to proceed. A mismatch or an inability to complete verification must stop this path before the loader is called. This is a proposed implementation condition that must then be confirmed through testing.
The trust boundary lies between the file and the reference
The file being checked is input data, while the expected hash is the basis for the acceptance decision. These roles must remain distinct. In the documentation’s Java example, the verification result depends on comparing the calculated actualHash with the supplied expectedHash. The origin of expectedHash is therefore part of the acceptance condition.
If a new hash is accepted alongside a modified file and made the reference, the comparison answers a different question: whether the file matches the value accompanying it. In the proposed implementation, the expected hash comes from a trusted reference prepared in advance, separately from the input file. The documentation recommends protecting hashes, favoring encryption and the application’s internal storage.
How to organize verification before loading
Make the sequence explicit: obtain the trusted expected value, calculate the hash of the file being checked, compare the values, decide whether to accept the file, and only then load it. A mismatch ends this sequence with rejection. The proposed rule for an error reading the file or reference is the same: loading cannot proceed without completed verification.
For the code itself, Android recommends trusted storage locations. The local options listed include the application’s internal storage and scoped storage on Android 10 and later. Storage selection complements integrity verification. Another documented option is code signing, with signature verification before loading; the remainder of this article focuses on hash comparison.
A fictional module and a positive control
Consider DemoBadge, an entirely fictional module that you own. Its only test task is to write the synthetic marker demo-module-ran to the application’s test state when its entry point is called. The approved file and its trusted expected hash are prepared in advance; the reference is stored separately in protected internal storage.
The following checks are the author’s proposals for this example, not an official Android procedure. Reset the marker before each run. For the approved file, record the event sequence: hash calculated, comparison successful, loader called, entry point executed. The positive control criterion is the appearance of a new marker after this sequence. A verification success message alone does not confirm that the module executed.
Test rejection of a modified file and a substituted reference
Create a modified copy of your own DemoBadge whose calculated hash differs from the original trusted value. Keep the reference unchanged. Reset the marker and run verification: the log should show a mismatch and rejection before the loader is called. Additional criteria are that the entry point is not called and no new synthetic marker appears.
In a separate run, supply the modified file together with its matching new hash as untrusted input. The expected result is that the application retains the protected reference and again rejects the file before loading. Assess the absence of the marker alongside the log: on its own, it does not show which stage stopped the process.
Test when verification cannot complete and set priorities
Using the same fictional data that you own, make the file unavailable in one run and the trusted reference unavailable in another. In both cases, the proposed fail-closed criterion is the same: verification is incomplete, neither the loader nor the entry point is called, and the synthetic marker is absent. Clear the previous test state before each run.
First, eliminate any path that allows loading after a mismatch or verification error. Then check the origin of the expected value and its protected storage. Next, establish the positive and negative scenarios as acceptance criteria. A success message or a successful HTTP response, if one is present in the test environment, does not replace observing loading, execution, and changes to the test state.
What the result confirms and where verification ends
A matching hash confirms that the file matches the selected reference, but does not establish that its contents are safe. An unsafe library itself reduces the application’s security. Successful runs of the described scenarios show that the defined criteria were met for these data; they do not provide a statistical guarantee of security.
This article covers file integrity before dynamic loading. Other protection mechanisms require separate tasks and checks. If the product has a website or web component, you can start with a free website or web-product audit; it covers a separate area and does not confirm that the Android module’s acceptance checks work correctly.