We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4297

Summary: Android StrictMode policy violation
Product: SDL Reporter: Sylvain <sylvain.becker>
Component: *don't know*Assignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: frankinshtein85
Version: don't know   
Hardware: All   
OS: Android (All)   
Attachments: log of all non sdk api used violations
patch
updated patch
modified SDL_android.c
patch updated

Description Sylvain 2018-10-05 17:03:38 UTC
Created attachment 3342 [details]
log of all non sdk api used violations

On Android 9, there are restrictions on non-SDK interfaces.

So there are logcat warnings like:

D/StrictMode(11834): StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Ljava/io/FileDescriptor;->descriptor:I

see this webpage for more information

https://developer.android.com/about/versions/pie/restrictions-non-sdk-interfaces
Comment 1 Sylvain 2018-10-05 18:41:18 UTC
not sure all the log is relevant.

the stack trace of the previous warning is : 

StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Ljava/io/FileDescriptor;->descriptor:I
at android.os.StrictMode.lambda$static$1(StrictMode.java:428)
at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
at org.libsdl.app.SDLActivity.nativeRunMain(Native Method)
at org.libsdl.app.m.run(Unknown Source:52)
at java.lang.Thread.run(Thread.java:764)
Comment 2 Sam Lantinga 2018-10-05 23:56:46 UTC
That stack isn't super helpful, but if you'd like to take a scan through and propose patches, that would be wonderful.

Thanks!
Comment 3 Sylvain 2018-10-06 14:25:07 UTC
Created attachment 3351 [details]
patch

I have found out that this is the JNI open/read file parts that causes this Non Sdk Api Used violation.

Current code looks overcomplicated whereas using the JNI asset manager is very simple. 

I've quickly tested a patch (mostly remove code).
Loading image and files works (I mean open, read, seek, close, and filesize), from SDL_image and SDL_mixer.
The warning from google play console reports now disappears.

But this patch also drops some code to open files in APK expansion. Not tested if this still work or not.
Comment 4 dmuratshin 2018-10-22 11:04:50 UTC
Have same crash error on Android P (helloworld, emulator, x86):

2018-10-22 15:52:56.822 4578-4646/org.oxygine.GamePart4 W/ygine.GamePart: Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
2018-10-22 15:52:56.973 1875-1946/? W/InputDispatcher: channel '2b247d6 org.oxygine.GamePart4/org.oxygine.GamePart4.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2018-10-22 15:52:56.973 1875-1946/? E/InputDispatcher: channel '2b247d6 org.oxygine.GamePart4/org.oxygine.GamePart4.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

not sure.. will my app (build for v7a work on real device)?
Comment 5 dmuratshin 2018-10-22 11:32:43 UTC
btw, I've found FileDescriptor has

    /**
     * Returns the int descriptor. It's highly unlikely you should be calling this. Please discuss
     * your needs with a libcore maintainer before using this method.
     * @hide internal use only
     */
    // Android-added.
    public final int getInt$() {
        return descriptor;
    }
Comment 6 Sam Lantinga 2018-10-22 19:00:41 UTC
Sylvain, can you make sure APK expansion files still work?
What's the earliest version of Android that supports these NDK functions?
Comment 7 Sylvain 2018-10-22 19:37:04 UTC
I really don't know how to build expansion apk nor to include it. Have to find out how to get it working first.


According to NDK release note, those asset API appeared with r5 : 
https://developer.android.com/ndk/downloads/revision_history

"Android NDK, Revision 5 (December 2010)

This release of the NDK includes many new APIs, most of which are introduced to support the development of games and similar applications that make extensive use of native code. Using the APIs, developers have direct native access to events, audio, graphics and window management, assets, and storage"



Btw, I don't have a crash. Only a warning when it's run on android p on google pre-launch. On the emulator, it didn't crash.
@dmuratshin: do you activate yourself StricMode ? or target specific sdk api ?
Comment 8 dmuratshin 2018-10-23 09:51:09 UTC
> Btw, I don't have a crash. Only a warning when it's run on android p on
> google pre-launch. On the emulator, it didn't crash.
> @dmuratshin: do you activate yourself StricMode ? or target specific sdk api
> ?

sorry, nevermind it was my fault
Comment 9 Sylvain 2018-10-24 20:12:34 UTC
Sam, I looked at the APK expansion. And the patch clearly removed it, as it's very interleaved with the code removed by this issue.

In the NDK, there is "storage_manager.h" which would allow to implement it by mounting a main or patch expansion APK (AStorageManager_mountobb()).

But, a (not so) quick try shows that it's broken: 
- If you mount an OBB with no encryption key (current case in SDL), then it crashes (https://issuetracker.google.com/issues/63624928)
- If you set an encryption key, you can mount it. But in my case it appears to empty. (OBB created with https://developer.android.com/studio/command-line/jobb)

After looking into other projects, it seems to use those APK expansion in Android nobody achieve to use ndk/storage_manager, but they use mere Zip tools instead.

In fact, OBB are just specific files, can be zip renamed as .obb, that are served by google play, and stored in a obb phone directory.
SDL currently only provide a very small part to use OBB and there are more things left to the user: https://developer.android.com/google/play/expansion-files).


I am not sure, but it looks very similar to PhysicsFS: providing access to assest files under one or several zip files. 
In short, people who want to use APK expansion could rely on PhysicsFS.
SDLActiviy.java could only provide helper function to have paths to those main/patch obb.
Comment 10 Sam Lantinga 2019-07-02 16:02:59 UTC
Sylvain, can you update this patch to apply cleanly with the latest SDL?

Thanks!
Comment 11 Sylvain 2019-07-02 17:07:09 UTC
I'll do that a little bit later, but this remove the APK Expansion features, is this ok for commit ?
Comment 12 Sam Lantinga 2019-07-02 19:04:00 UTC
Is there a way to avoid that?
Comment 13 Sylvain 2019-07-02 20:04:23 UTC
Created attachment 3854 [details]
updated patch

Updated patch.

I don't really remember the details but I couldn't get the APK Expansion features working. See the previous message for the record.

I had some test code if you want to try ...
Comment 14 Sylvain 2019-07-02 20:08:24 UTC
Created attachment 3855 [details]
modified SDL_android.c

This SDL_android.c contains some sample code for the APK feature .. (look for obb ...)
Comment 15 Ryan C. Gordon 2019-07-30 17:52:21 UTC
Moving all the remaining target-2.0.10 bugs to target-2.0.11. If these still aren't resolved when 2.0.11 ships, we should probably remove the keyword altogether.

--ryan.
Comment 16 Ryan C. Gordon 2019-09-20 20:47:34 UTC
We're changing how we do SDL release versions; now releases will be even numbers (2.0.10, 2.0.12, etc), and as soon as we tag a release, we'll move the internal version number to an odd number (2.0.12 ships, we tag the latest in revision control as 2.0.13 immediately, which will become 2.0.14 on release, etc).

As such, I'm moving the bugs tagged with target-2.0.11 to target 2.0.12. Sorry if you get a lot of email from this change!

Thanks,
--ryan.
Comment 17 Ryan C. Gordon 2019-09-20 20:48:40 UTC
We're changing how we do SDL release versions; now releases will be even numbers (2.0.10, 2.0.12, etc), and as soon as we tag a release, we'll move the internal version number to an odd number (2.0.12 ships, we tag the latest in revision control as 2.0.13 immediately, which will become 2.0.14 on release, etc).

As such, I'm moving the bugs tagged with target-2.0.11 to target 2.0.12. Sorry if you get a lot of email from this change!

Thanks,
--ryan.
Comment 18 Sylvain 2020-05-08 09:09:01 UTC
Created attachment 4334 [details]
patch updated

This is the patch updated since asset manager code is used with SDL_GetPreferredLocales(). (https://hg.libsdl.org/SDL/rev/458e052261cb)

the diff appears much cleaner

(NB: the patch should also remove some obvious java method)
Comment 19 Sylvain 2020-08-17 17:53:02 UTC
https://hg.libsdl.org/SDL/rev/9668efdaef72

Fixed bug 4297 - Android StrictMode policy. Remove APK expansion support

"In the second half of 2021, new apps will be required to publish with the Android App Bundle on Google Play"
(see https://developer.android.com/guide/app-bundle)
And "Android App Bundles don't support APK expansion (*.obb) files".