okio icon indicating copy to clipboard operation
okio copied to clipboard

Add androidNative targets.

Open MohammadKHC opened this issue 1 month ago • 6 comments

This adds support for androidNative targets.

This is the second attempt at trying to do this. Now I can confirm that it does compile and work.

The problem with the first attempt (#1726) was that it didn't account for the fact that platform.posix.DIR doesn't exist on androidNative. Therefore, opendir, readdir, and closedir can't be used from common code. This fixes this by using expect/actual for all this stuff.

To run the tests. Run the following commands in order. (You need to have an Android Emulator running first.)

./gradlew androidNativeX64TestBinaries
adb push /path/to/okio/okio/build/bin/androidNativeX64/debugTest/test.kexe /data/local/tmp
rm -r /path/to/okio/build
adb push /path/to/okio /data/local/tmp/okio
adb shell
export OKIO_ROOT=/data/local/tmp/okio
/data/local/tmp/test.kexe

Closes: #1692, Closes #1242

MohammadKHC avatar Nov 15 '25 11:11 MohammadKHC

Outdated.. To run the tests on the Android Emulator run ``` ./gradlew androidNativeX64TestBinaries && adb push okio/build/bin/androidNativeX64/debugTest/test.kexe /data/local/tmp && adb shell /data/local/tmp/test.kexe ```

For me all the tests passed except those:

[  FAILED  ] 23 tests, listed below:
[  FAILED  ] okio.NativeSystemFileSystemTest.listOnRelativePathWhichIsNotDotReturnsRelativePaths
[  FAILED  ] okio.NativeSystemFileSystemTest.listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths
[  FAILED  ] okio.ZipFileSystemGoTest.timeWinzip
[  FAILED  ] okio.ZipFileSystemTest.emptyZip
[  FAILED  ] okio.ZipFileSystemTest.emptyZipWithPrependedData
[  FAILED  ] okio.ZipFileSystemTest.zipWithFiles
[  FAILED  ] okio.ZipFileSystemTest.zipWithDeflate
[  FAILED  ] okio.ZipFileSystemTest.zipWithStore
[  FAILED  ] okio.ZipFileSystemTest.zipWithFileComments
[  FAILED  ] okio.ZipFileSystemTest.zipWithFileModifiedDate
[  FAILED  ] okio.ZipFileSystemTest.zipWithFileOutOfBoundsModifiedDate
[  FAILED  ] okio.ZipFileSystemTest.zipWithDirectoryModifiedDate
[  FAILED  ] okio.ZipFileSystemTest.zipWithModifiedDate
[  FAILED  ] okio.ZipFileSystemTest.zipWithEmptyDirectory
[  FAILED  ] okio.ZipFileSystemTest.zipWithSyntheticDirectory
[  FAILED  ] okio.ZipFileSystemTest.zip64
[  FAILED  ] okio.ZipFileSystemTest.zipWithArchiveComment
[  FAILED  ] okio.ZipFileSystemTest.cannotReadZipWithSpanning
[  FAILED  ] okio.ZipFileSystemTest.cannotReadZipWithEncryption
[  FAILED  ] okio.ZipFileSystemTest.zipTooShort
[  FAILED  ] okio.ZipFileSystemTest.filesOverlap
[  FAILED  ] okio.ZipFileSystemTest.canonicalizationValid
[  FAILED  ] okio.ZipFileSystemTest.canonicalizationInvalidThrows

Will investigate further and update you.

MohammadKHC avatar Nov 15 '25 13:11 MohammadKHC

Also, Outdated UPDATE: I got most tests running. Those are the only failing tests. ``` [ FAILED ] 3 tests, listed below: [ FAILED ] okio.NativeSystemFileSystemTest.canonicalizeDotReturnsCurrentWorkingDirectory [ FAILED ] okio.NativeSystemFileSystemTest.listOnRelativePathWhichIsNotDotReturnsRelativePaths [ FAILED ] okio.NativeSystemFileSystemTest.listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths ```

To run tests now, do the following on the terminal.

./gradlew androidNativeX64TestBinaries
adb push /path/to/okio/okio/build/bin/androidNativeX64/debugTest/test.kexe /data/local/tmp
rm -r /path/to/okio/build
adb push /path/to/okio /data/local/tmp/okio
adb shell
cd /data/local/tmp
export OKIO_ROOT=/data/local/tmp/okio
./test.kexe

MohammadKHC avatar Nov 15 '25 13:11 MohammadKHC

UPDATE again. canonicalizeDotReturnsCurrentWorkingDirectory fails because i changed the cwd to /data/local/tmp.. The test expects it to be "/". And it does not fail if i don't do the cd.

Now the only failling tests are listOnRelativePathWhichIsNotDotReturnsRelativePaths and listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths

Best way to run the tests as of now.

./gradlew androidNativeX64TestBinaries
adb push /path/to/okio/okio/build/bin/androidNativeX64/debugTest/test.kexe /data/local/tmp
rm -r /path/to/okio/build
adb push /path/to/okio /data/local/tmp/okio
adb shell
export OKIO_ROOT=/data/local/tmp/okio
/data/local/tmp/test.kexe

MohammadKHC avatar Nov 15 '25 13:11 MohammadKHC

After reading the listOnRelativePathWhichIsNotDotReturnsRelativePaths code, it seems that the test itself is flaky. It only creates the required okio.api test file when one of the following is true: isFakeFileSystem || isWrappingJimFileSystem || isWasiFileSystem

Knowing that, I can say that Okio does work as intended on Android native, which is useful for a bunch of Android tech apps such as Termux or Android IDEs.

MohammadKHC avatar Nov 15 '25 13:11 MohammadKHC

@JakeWharton should i edit listOnRelativePathWhichIsNotDotReturnsRelativePaths test or just ignore it on Android. And do you think that the approach of using actual/expect is fine with opendir and this stuff? Maybe there might be a better way?

MohammadKHC avatar Nov 17 '25 18:11 MohammadKHC

@JakeWharton are you free to look at this?

I think there are two ways to make opendir/closedir and this stuff on Android native

  1. Use expect/actual with a typealias and top level functions.
  2. Use expect/actual class with a name like DirEntry or something

And also it would be nice if we could create a new nativeNonAndroid sourceset. Although if we made it then we can't use the option 1, as the platform.posix.DIR is a typealias on Linux and we can't create a typealias to a typealias. And this would kina complicate the targets hierarchy.

All the tests succeeded expect listOnRelativePathWhichIsNotDotReturnsRelativePaths and listOrNullOnRelativePathWhichIsNotDotReturnsRelativePaths

I can probably fix this tests and edit the github workflow to run them all for androidNative.

What do you think?

Info: I really need this to be merged as I plan to use it in my Rustroid Rust IDE (Iam writing some native tool that should run on it in Kotlin)

MohammadKHC avatar Dec 01 '25 05:12 MohammadKHC