안드로이드 앱 실행 시 어떤 파일들에 접근할까
How to access file on Android
안드로이드 앱 실행 시 어떤 파일들에 접근할까
Introduction
안드로이드로 파일에 접근할 때 대부분 File
클래스를 통해 접근하고 그후에 stream 으로 접근하여 정보를 작성한다.
이때 사용하는 API 는 FileOutputStream
, FileInputStream
등이 있는데 이들 호출 구조를 보면서 앱 실행 시 어떤 파일이 접근하는지 확인하는 코드를 작성해보려고 한다.
What is that
각각의 특정 File 에 접근할 때 사용되는 코드들의 호출 구조를 보면 아래와 같다.
RandomAccessFile
1
2
3
3191 ms RandomAccessFile.$init("<instance: java.io.File>", "r")
3191 ms | open(path="/data/redacted/redacted/redacted.txt", oflag=0x0, ...)
3191 ms | open(=> fd=0xf2)
FileInputStream
1
2
3
2858 ms FileOutputStream.$init("<instance: java.io.File>", false)
2859 ms | open(path="/data/user/0/com.redacted.redacted.redacted.redacted/shared_prefs/redacted.redacted.redacted.redacted-redacted.xml", oflag=0x241, ...)
2859 ms | open(=> fd=0xa6)
FileOutputStream
1
2
3
1575 ms FileInputStream.$init("<instance: java.io.File>")
1576 ms | open(path="/storage/emulated/0/Android/data/com.redacted.redacted.redacted.redacted/cache/default/redacted/redacted.pro", oflag=0x0, ...)
1576 ms | open(=> fd=0xf8)
이를 확인해보면 결론적으로 최상위에는 네이티브 함수 중 저수준 open()
험수에 접근하게 된다.
open()
에 대한 함수는 아래와 같다.
1
int open(const char *pathname, int flags);
파일 디스크립터 번호를 반환하고 제일 첫번째에는 절대경로가 인자로 들어간다. 그 다음은 파일을 접근할 때 옵션이다.
Conclusion
해당 내용을 기반으로 앱 실행 시 접근하는 파일 이름을 출력하는 스니펫을 간단하게 작성하였다.
관련 저장소는 아래와 같다.
This post is licensed under CC BY 4.0 by the author.
If you find any errors, please let me know by comment or email. Thank you.
If you find any errors, please let me know by comment or email. Thank you.