How can I add additional Android permissions for my UI test project only?

后端 未结 3 1040
再見小時候
再見小時候 2020-12-13 02:18

I try to write files to the external SD card in a from InstrumentationTestCase2 derived test case for pure testing purposes. This works all well when andr

3条回答
  •  [愿得一人]
    2020-12-13 02:52

    In short you should add the same android:sharedUserId for both application's manifest and test project's manifest and declare necessary permission for the test project.

    This workaround comes from the fact that Android actually assigns permissions to linux user accounts (uids) but not to application themselves (by default every application gets its own uid so it looks like permissions are set per an application).

    Applictions that are signed with the same certificate can however share the same uid. As a consequence they have a common set of permissions. For example, I can have application A that requests WRITE_EXTERNAL_STORAGE permission and application B that requests INTERNET permission. Both A and B are signed by the same certificate (let's say debug one). In AndroidManifest.xml files for A and B android:sharedUserId="test.shared.id" is declared in tag. Then both A and B can access network and write to sdcard even though they declare only part of needed permissions because permissions are assigned per uid. Of course, this works only if both A and B are actually installed.

    Here is an example of how to set up in case of the test project. The AndroidManifest.xml for application:

    
    
        
    
        
            
                
                        
                    
                
            
        
    
    
    

    And the AndroidManifest.xml for a test project

    
    
        
    
        
    
        
    
        
            
        
    
    
    

    The drawback of this solution is that the application is able to write to external storage too when test package is installed. If it accidentally writes something to a storage it may remain unnoticed until release when the package will be signed with a different key.

    Some additional information about shared UIDs can be found at http://developer.android.com/guide/topics/security/permissions.html#userid.

提交回复
热议问题