androidandroid-jetpack-security

Android Jetpack security - sample code won't compile "no suitable constructor found for FileReader(EncryptedFile)"


I'm working on testing the beta version of Google's Android Jetpack for security.

The sample code here is simple enough, but it will not compile:

foo.java:274: error: no suitable constructor found for FileReader(EncryptedFile)
             new BufferedReader(new FileReader(encryptedFile))) {

I have included the dependency in my gradle:

dependencies {
    implementation "androidx.security:security-crypto:1.0.0-rc01"
}

Searching the FileReader documentation here I don't see how to get this sample code working.

For completeness, here is my function:

259   void jetpackTest()
260   {
261     KeyGenParameterSpec keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC;
262     String masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec);
263     
264     String fileToRead = "my_sensitive_data.txt";
265     EncryptedFile encryptedFile = new EncryptedFile.Builder(
266             new File("private", fileToRead),
267             this,
268             masterKeyAlias,
269             EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
270     ).build();
271 
272     StringBuffer stringBuffer = new StringBuffer();
273     try (BufferedReader reader =
274                  new BufferedReader(new FileReader(encryptedFile))) {
275 
276         String line = reader.readLine();
277         while (line != null) {
278             stringBuffer.append(line).append('\n');
279             line = reader.readLine();
280         }
281     } catch (IOException e) {
282         // Error occurred opening raw file for reading.
283     } finally {
284         String contents = stringBuffer.toString();
285     }
286   }

Solution

  • The Kotlin version also have some error, I think there has been a mistake or a typo.

    Based on the code for writing files in the sample code, can you try as:

    import java.io.InputStreamReader;
    
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(encryptedFile.openFileInput()))