0

I'd like to get the size of .so library located in apk file. I was trying to get the size by using tellg() but it returns -1:

const char* file = "/data/app/com.example.app-wX7zOF4_h5XQiYI8shdLyg==/base.apk!/lib/arm64-v8a/libreader.so";
std::ifstream if(file, std::ios_base::binary | std::ios_base::ate);
return if.tellg();

I'm using C++11, so I can't use std::filesystem::file_size available in C++17.

Is there another way (probably not so straightforward as file_size) to get file size using C++11?

Alex
  • 1,641
  • 15
  • 26
  • 4
    Are you sure that you opened the file successfully? You're not checking that in the posted code. – Paul Sanders May 21 '21 at 16:30
  • 1
    What about using [`stat()`](https://man7.org/linux/man-pages/man2/stat.2.html) instead of `std::ifstream`? – Remy Lebeau May 21 '21 at 16:49
  • @PaulSanders yes, I have successfully loaded the library using the same URI. – Alex May 21 '21 at 18:43
  • @RemyLebeau tried to use the function `GetFileSize` suggested here: https://stackoverflow.com/a/6039648/1549256 rc equals -1 in my case – Alex May 21 '21 at 19:04
  • @Alex That goes back to Paul's comment. Either the file path you are using is wrong to begin with, or you don't have permission to access the file. What is the value of `errno` when `stat()` fails? – Remy Lebeau May 21 '21 at 19:25
  • @RemyLebeau errno is `No such file or directory`. As I mentioned in the post, the file definitely exists since it's loaded by the application successfully. I suspect the file can't be treated by `std::ifstream` or `stat()` correctly because it's located in a compressed apk archive and\or they can't treat the path with `!`. – Alex May 21 '21 at 19:49
  • 1
    @Alex "*errno is `No such file or directory`*" - well, there you go then. Your file path is wrong, as the file you want to access is not actually on the file system. so you need to fix your code accordingly. You can't just dip *inside* the contents of an `.apk` like you are trying to do. Why did you think that was ever a valid operation? You would have to actually open the `.apk` and extract the `.so` file, just like with any other kind of compressed zip archive. Try using Android's own Asset Manager APIs for that purpose. – Remy Lebeau May 21 '21 at 20:15

0 Answers0