Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
104.37k stars 10.15k forks source link

Android 14 turn screen off issue #3927

Closed Da2MinuteNoodle closed 6 months ago

Da2MinuteNoodle commented 1 year ago

On the Android 14 beta, launching scrcpy from the lockscreen reboots the device, ~copy-paste is broken, and~ --turn-screen-off no longer works.

Launch logs:

PS C:\DATA\Utilities\scrcpy> .\scrcpy.exe --no-audio --turn-screen-off
scrcpy 2.0 <https://github.com/Genymobile/scrcpy>
C:\DATA\Utilities\scrcpy\scrcpy-server: 1 file pushed, 0 skipped. 79.3 MB/s (52867 bytes in 0.001s)
[server] INFO: Device: Google Pixel 6 (Android 13)
[server] ERROR: Could not invoke method
java.lang.NoSuchMethodException: android.content.IClipboard$Stub$Proxy.addPrimaryClipChangedListener [interface android.content.IOnPrimaryClipChangedListener, class java.lang.String, class java.lang.String, int]
        at java.lang.Class.getMethod(Class.java:2890)
        at java.lang.Class.getMethod(Class.java:2470)
        at com.genymobile.scrcpy.wrappers.ClipboardManager.getAddPrimaryClipChangedListener(ClipboardManager.java:129)
        at com.genymobile.scrcpy.wrappers.ClipboardManager.addPrimaryClipChangedListener(ClipboardManager.java:139)
        at com.genymobile.scrcpy.Device.<init>(Device.java:100)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:64)
        at com.genymobile.scrcpy.Server.main(Server.java:381)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:359)
[server] ERROR: Could not invoke method
java.lang.NoSuchMethodException: android.view.SurfaceControl.getPhysicalDisplayIds []
        at java.lang.Class.getMethod(Class.java:2890)
        at java.lang.Class.getMethod(Class.java:2470)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getGetPhysicalDisplayIdsMethod(SurfaceControl.java:137)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getPhysicalDisplayIds(SurfaceControl.java:144)
        at com.genymobile.scrcpy.Device.setScreenPowerMode(Device.java:282)
        at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:165)
        at com.genymobile.scrcpy.Controller.control(Controller.java:83)
        at com.genymobile.scrcpy.Controller.lambda$start$0$com-genymobile-scrcpy-Controller(Controller.java:90)
        at com.genymobile.scrcpy.Controller$$ExternalSyntheticLambda0.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:1012)
[server] ERROR: Could not get physical display ids
INFO: Renderer: direct3d
INFO: Initial texture: 1080x2400

Error after copy pasting:

[server] ERROR: Could not invoke method
java.lang.NoSuchMethodException: android.content.IClipboard$Stub$Proxy.getPrimaryClip [class java.lang.String, class java.lang.String, int]
        at java.lang.Class.getMethod(Class.java:2890)
        at java.lang.Class.getMethod(Class.java:2470)
        at com.genymobile.scrcpy.wrappers.ClipboardManager.getGetPrimaryClipMethod(ClipboardManager.java:35)
        at com.genymobile.scrcpy.wrappers.ClipboardManager.getText(ClipboardManager.java:83)
        at com.genymobile.scrcpy.Device.getClipboardText(Device.java:248)
        at com.genymobile.scrcpy.Device.setClipboardText(Device.java:261)
        at com.genymobile.scrcpy.Controller.setClipboard(Controller.java:394)
        at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:160)
        at com.genymobile.scrcpy.Controller.control(Controller.java:83)
        at com.genymobile.scrcpy.Controller.lambda$start$0$com-genymobile-scrcpy-Controller(Controller.java:90)
        at com.genymobile.scrcpy.Controller$$ExternalSyntheticLambda0.run(Unknown Source:2)
        at java.lang.Thread.run(Thread.java:1012)

I understand scrcpy isn't yet supported on Android 14 but I'm raising the issue as soon as possible to bring this to the projects attention.

rom1v commented 1 year ago

Thank you for your report.

The unexpected reboot is probably a system bug in Android 14 beta. The clipboard issue is fixed on dev. I have no solution for the "turn screen off" feature yet: https://github.com/Genymobile/scrcpy/issues/3784#issuecomment-1475755758

Mikachu commented 1 year ago

Google removed the SurfaceControl methods used to turn the screen off and moved them to internal classes in system_server, which I assume are not accessible by scrcpy. On (some?) qualcomm devices you can use the surfaceflinger backdoor code 20000 to turn the screen off, eg service call SurfaceFlinger 20000 i64 130 i32 2 where 130 is the id of the physical display (can be seen in dumpsys SurfaceFlinger --displays output)

Pawloland commented 9 months ago

Google must be having the same issue with turning screen off. I found this in AS setting, for their scrcpy clone baked into it. I haven't found any thread regarding it in their issue tracer, although, it may be just me not guessing correct magic words for searching. obraz

rom1v commented 8 months ago

Refs Android issue: https://issuetracker.google.com/issues/303565669

Refs another issue related to crop and video orientation in Android 14: https://github.com/Genymobile/scrcpy/issues/4011

shanlanCoding commented 7 months ago

Thank you for your report.

The unexpected reboot is probably a system bug in Android 14 beta. The clipboard issue is fixed on dev. I have no solution for the "turn screen off" feature yet: #3784 (comment)

Is there still no way to solve it now? My phone is Xiaomi 13, which has just updated its new MIUI14 system. The kernel is already Android 14, and I found that I cannot use Scrcpy's screen shutdown control function.

huangsijun17 commented 7 months ago

感谢您报告的信息。 意外重启可能是 Android 14 测试版中的系统错误。剪贴板问题已在 上修复。我还没有“关闭屏幕”功能的解决方案:#3784(评论)dev

Is there still no way to solve it now?

Please turn off the full-page translation of the browser before clicking the quote reply on the GitHub issues interface.

shanlanCoding commented 7 months ago

感谢您报告的信息。 意外重启可能是 Android 14 测试版中的系统错误。剪贴板问题已在 上修复。我还没有“关闭屏幕”功能的解决方案:#3784(评论)dev

Is there still no way to solve it now?

Please turn off the full-page translation of the browser before clicking the quote reply on the GitHub issues interface.

Is there still no way to solve it now? My phone is Xiaomi 13, which has just updated its new MIUI14 system. The kernel is already Android 14, and I found that I cannot use Scrcpy's screen shutdown control function.

nikolaiqa commented 7 months ago

Hi there! Got this issue on Pixel 7/Android 14, though updated scrcpy to the newest version. Glad if the issue will be solved because I really like using the app. Thanks anyway :)

Log:

[server] INFO: Device: [Google] google Pixel 7 (Android 14) INFO: Renderer: direct3d INFO: Textu[server] ERROR: Could not invoke method java.lang.NoSuchMethodException: android.view.SurfaceControl.getPhysicalDisplayIds [] at java.lang.Class.getMethod(Class.java:2937) at java.lang.Class.getMethod(Class.java:2449) at com.genymobile.scrcpy.wrappers.SurfaceControl.getGetPhysicalDisplayIdsMethod(SurfaceControl.java:137) at com.genymobile.scrcpy.wrappers.SurfaceControl.getPhysicalDisplayIds(SurfaceControl.java:144) at com.genymobile.scrcpy.Device.setScreenPowerMode(Device.java:319) at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:169) at com.genymobile.scrcpy.Controller.control(Controller.java:83) at com.genymobile.scrcpy.Controller.lambda$start$0$com-genymobile-scrcpy-Controller(Controller.java:91) at com.genymobile.scrcpy.Controller$$ExternalSyntheticLambda1.run(Unknown Source:4) at java.lang.Thread.run(Thread.java:1012) [server] ERROR: Could not get physical display ids re: 1080x2400

consp1racy commented 7 months ago

You can use this to get the system server class loader and get DisplayControl from it using reflection

val classLoader = ClassLoader.getSystemClassLoader()
val systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH")
val systemServerClassLoader = PathClassLoader(systemServerClasspath, null, classLoader) // classLoader may be null since API 24

Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/com/android/internal/os/ZygoteInit.java#442

According to the commit linked in the android issue it accesses SurfaceComposerClient in cpp same as before. This could work unless there's a permission issue.

yume-chan commented 7 months ago

Someone on Android 14 please test this:

  1. Download and unzip test.zip (source code at https://github.com/yume-chan/turn-off-screen)
  2. Run the following commands:
adb push ./test.jar /data/local/tmp
adb shell
LD_PRELOAD="/apex/com.android.adbd/lib64/libadb_pairing_server.so /apex/com.android.adbd/lib64/libadb_pairing_connection.so /apex/com.android.os.statsd/lib64/libstatspull.so /apex/com.android.os.statsd/lib64/libstatssocket.so /apex/com.android.runtime/lib64/bionic/libdl_android.so /apex/com.android.i18n/lib64/libandroidicu.so /system/lib64/libandroid_servers.so" app_process -cp /data/local/tmp/test.jar:/system/framework/services.jar / com.example.test.Main

Please check:

  1. Does it turn off your screen?
  2. Use Scrcpy when screen is off, does touch and keyboard work?

If it doesn't work, please attach its console output and logcat logs.

To turn the screen back on, press power button twice, or add a 2 at the end of the command (...services.jar / com.example.test.Main 2)

NicholasBuse commented 7 months ago

Seems to work for me on a Pixel 7. I tried the above without scrcpy running (initially) and with scrcpy running already. Both scenarios had screen touch and keyboard access and the screen was off. ADB shell output:

...cu.so /system/lib64/libandroid_servers.so" app_process -cp /data/local/tmp/test.jar:/system/framework/services.jar / com.example.test.Main Start Mode: 0 Loaded libandroiod_servers.so displayControlClass: class com.android.server.display.DisplayControl getPhysicalDisplayIdsMethod: public static long[] com.android.server.display.DisplayControl.getPhysicalDisplayIds() getPhysicalDisplayTokenMethod: public static android.os.IBinder com.android.server.display.DisplayControl.getPhysicalDisplayToken(long) surfaceControlClass: class android.view.SurfaceControl setDisplayPowerModeMethod: public static void android.view.SurfaceControl.setDisplayPowerMode(android.os.IBinder,int) displayIds.length: 1 displayId: 4619827677550801152 token: android.os.BinderProxy@87b3a85 setDisplayPowerMode success

...u.so /system/lib64/libandroid_servers.so" app_process -cp /data/local/tmp/test.jar:/system/framework/services.jar / com.example.test.Main 2 Start Mode: 2 Loaded libandroiod_servers.so displayControlClass: class com.android.server.display.DisplayControl getPhysicalDisplayIdsMethod: public static long[] com.android.server.display.DisplayControl.getPhysicalDisplayIds() getPhysicalDisplayTokenMethod: public static android.os.IBinder com.android.server.display.DisplayControl.getPhysicalDisplayToken(long) surfaceControlClass: class android.view.SurfaceControl setDisplayPowerModeMethod: public static void android.view.SurfaceControl.setDisplayPowerMode(android.os.IBinder,int) displayIds.length: 1 displayId: 4619827677550801152 token: android.os.BinderProxy@fd9508b setDisplayPowerMode success

msccreater commented 7 months ago

Someone on Android 14 please test this:

  1. Download and unzip test.zip (source code at https://github.com/yume-chan/turn-off-screen)
  2. Run the following commands:
adb push ./test.jar /data/local/tmp
adb shell
LD_PRELOAD="/apex/com.android.adbd/lib64/libadb_pairing_server.so /apex/com.android.adbd/lib64/libadb_pairing_connection.so /apex/com.android.os.statsd/lib64/libstatspull.so /apex/com.android.os.statsd/lib64/libstatssocket.so /apex/com.android.runtime/lib64/bionic/libdl_android.so /apex/com.android.i18n/lib64/libandroidicu.so /system/lib64/libandroid_servers.so" app_process -cp /data/local/tmp/test.jar:/system/framework/services.jar / com.example.test.Main

Please check:

  1. Does it turn off your screen?
  2. Use Scrcpy when screen is off, does touch and keyboard work?

If it doesn't work, please attach its console output and logcat logs.

To turn the screen back on, press power button twice, or add a 2 at the end of the command (...services.jar / com.example.test.Main 2)

thans, this is work for me, the output:

Start Mode: 0 Loaded libandroiod_servers.so displayControlClass: class com.android.server.display.DisplayControl getPhysicalDisplayIdsMethod: public static long[] com.android.server.display.DisplayControl.getPhysicalDisplayIds() getPhysicalDisplayTokenMethod: public static android.os.IBinder com.android.server.display.DisplayControl.getPhysicalDisplayToken(long) surfaceControlClass: class android.view.SurfaceControl setDisplayPowerModeMethod: public static void android.view.SurfaceControl.setDisplayPowerMode(android.os.IBinder,int) displayIds.length: 1 displayId: 4630947082089526659 token: android.os.BinderProxy@798ef88 setDisplayPowerMode success

LollosoSi commented 7 months ago

Someone on Android 14 please test this

Confirmed, works

shanlanCoding commented 7 months ago

在小米13,安卓内核14工作正常

  1. 能够关闭屏幕
  2. 能够正常使用键盘,鼠标控制

    At Xiaomi 13, Android Core 14 works normally

  3. Ability to turn off the screen
  4. Able to use keyboard and mouse control normally
ballo commented 7 months ago

Hello. I also have the dreaded Pixel 7 with the horrible Android 14 (bought this used and it's just been a long series of excruciating headaches).

Is this the bug that is causing the screen to go blank at the unlock screen? Is there a work-around, perhaps a way to bypass the unlock screen? Thanks..

TylerRose commented 7 months ago

Hello. I also have the dreaded Pixel 7 with the horrible Android 14 (bought this used and it's just been a long series of excruciating headaches).

Is this the bug that is causing the screen to go blank at the unlock screen? Is there a work-around, perhaps a way to bypass the unlock screen? Thanks..

No, that's a security feature. Scrcpy will always display black on the lock screen. Only "bypass" would be a swipe lock (no security). This relates to Scrcpy's ability to turn off the display while still being able to manipulate/see the device on your PC.

dacs6 commented 7 months ago

Not working for my XiaoMi14 with Android 14.

Output from adb:

PS D:\App\scrcpy-win64> ./adb push ./test.jar /data/local/tmp
./test.jar: 1 file pushed, 0 skipped. 110.4 MB/s (3907362 bytes in 0.034s)
PS D:\App\scrcpy-win64> ./adb shell
rocess -cp /data/local/tmp/test.jar:/system/framework/services.jar / com.example.test.Main                            <
Start
Mode: 0
Loaded libandroiod_servers.so
displayControlClass: class com.android.server.display.DisplayControl
getPhysicalDisplayIdsMethod: public static long[] com.android.server.display.DisplayControl.getPhysicalDisplayIds()
getPhysicalDisplayTokenMethod: public static android.os.IBinder com.android.server.display.DisplayControl.getPhysicalDisplayToken(long)
surfaceControlClass: class android.view.SurfaceControl
setDisplayPowerModeMethod: public static void android.view.SurfaceControl.setDisplayPowerMode(android.os.IBinder,int)
displayIds.length: 1
displayId: 4630947082089526659
token: android.os.BinderProxy@ac3fcee
setDisplayPowerMode success

But Alt+O in scrcpy has the same error as before:

[server] ERROR: Could not invoke method
java.lang.NoSuchMethodException: android.view.SurfaceControl.getPhysicalDisplayIds []
        at java.lang.Class.getMethod(Class.java:2937)
        at java.lang.Class.getMethod(Class.java:2449)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getGetPhysicalDisplayIdsMethod(SurfaceControl.java:137)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getPhysicalDisplayIds(SurfaceControl.java:144)
        at com.genymobile.scrcpy.Device.setScreenPowerMode(Device.java:319)
        at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:169)
        at com.genymobile.scrcpy.Controller.control(Controller.java:83)
        at com.genymobile.scrcpy.Controller.lambda$start$0$com-genymobile-scrcpy-Controller(Controller.java:91)
        at com.genymobile.scrcpy.Controller$$ExternalSyntheticLambda1.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:1012)
[server] ERROR: Could not get physical display ids
ballo commented 7 months ago

Hello. I also have the dreaded Pixel 7 with the horrible Android 14 (bought this used and it's just been a long series of excruciating headaches). Is this the bug that is causing the screen to go blank at the unlock screen? Is there a work-around, perhaps a way to bypass the unlock screen? Thanks..

No, that's a security feature. Scrcpy will always display black on the lock screen. Only "bypass" would be a swipe lock (no security). This relates to Scrcpy's ability to turn off the display while still being able to manipulate/see the device on your PC.

There isn't a flag in scrcpy to disable this? Honestly, it's false security. Either the connection is encrypted or it isn't.

Edit: even better, if there is a way to bypass the swipe lock when using scrcpy that would be even better.

rom1v commented 7 months ago

There isn't a flag in scrcpy to disable this? Honestly, it's false security.

This is the decision of the lock screen developers to set the "secure flag", and from Android developer to enforce the secure flag even with shell permissions (#2129).

Edit: even better, if there is a way to bypass the swipe lock when using scrcpy that would be even better.

MOD+m (shortcuts)

xueshiji commented 7 months ago

I have rooted my device, is there an easy way to temporarily turn off the screen?

ballo commented 7 months ago

Edit: even better, if there is a way to bypass the swipe lock when using scrcpy that would be even better.

MOD+m (shortcuts)

Your MacOS app has Mod-M bound to miniaturize screen

rom1v commented 7 months ago

By default, MOD is Alt or Super (and you can change it).

ballo commented 7 months ago

Welp all it does is make the screen go blank. scrcpy is still 100% unusable because the only way to use it is to physically unlock the phone which defeats the entire purpose.

ballo commented 7 months ago

The only work-around I can see is to BLINDLY swipe the phone open. The process is, hopefully needless to say, dumb.

WMOH-DEV commented 6 months ago

v2.2 works with me samsung android 14

[server] INFO: Device: [samsung] samsung SM-S908B (Android 14)
INFO: Renderer: direct3d
INFO: Texture: 1080x2312
[server] INFO: Device screen turned off
Felindril commented 6 months ago

v2.2 works with me samsung android 14

[server] INFO: Device: [samsung] samsung SM-S908B (Android 14)
INFO: Renderer: direct3d
INFO: Texture: 1080x2312
[server] INFO: Device screen turned off

No change here with v2.2 on a Pixel 7 / Android 14

scrcpy v2.2 <https://github.com/Genymobile/scrcpy>

[server] INFO: Device: [Google] google Pixel 7 (Android 14)
INFO: Renderer: direct3d
I[server] ERROR: Could not invoke method
java.lang.NoSuchMethodException: android.view.SurfaceControl.getPhysicalDisplayIds []
        at java.lang.Class.getMethod(Class.java:2937)
        at java.lang.Class.getMethod(Class.java:2449)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getGetPhysicalDisplayIdsMethod(SurfaceControl.java:137)
        at com.genymobile.scrcpy.wrappers.SurfaceControl.getPhysicalDisplayIds(SurfaceControl.java:144)
        at com.genymobile.scrcpy.Device.setScreenPowerMode(Device.java:319)
        at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:169)
        at com.genymobile.scrcpy.Controller.control(Controller.java:83)
        at com.genymobile.scrcpy.Controller.lambda$start$0$com-genymobile-scrcpy-Controller(Controller.java:91)
        at com.genymobile.scrcpy.Controller$$ExternalSyntheticLambda1.run(Unknown Source:4)
        at java.lang.Thread.run(Thread.java:1012)
[server] ERROR: Could not get physical display ids
NFO: Texture: 1080x2400
rom1v commented 6 months ago

Please test #4446.

hooligeek commented 6 months ago

Please test #4446.

Pixel 5, Android 14. This works. Phone screen turns off.

C:\scrcpy\scrcpy-win64-v2.2-42-g623bf73eb>scrcpy --window-title=hooligeek --turn-screen-off --stay-awake --window-x=1 --window-y=31 --window-height=1008 scrcpy v2.2 https://github.com/Genymobile/scrcpy INFO: ADB device found: INFO: --> (usb) 0C101FDD4003AV device Pixel_5 C:\scrcpy\scrcpy-win64-v2.2-42-g623bf73eb\scrcpy-server: 1 file pushed, 0 skipped. 70.8 MB/s (66719 bytes in 0.001s) [server] INFO: Device: [Google] google Pixel 5 (Android 14) INFO: Renderer: direct3d INFO: Texture: 1080x2336 [server] INFO: Device screen turned off

AndroidDeveloperLB commented 6 months ago

Please test #4446.

Hey this one works nicely on my Pixel 6 with Android 14! Command is:

.../scrcpy-noconsole.vbs -S --always-on-top --stay-awake --no-audio

OS Build is: UP1A.231105.003

NicholasBuse commented 6 months ago

Yes, this PR (4446) into ScrCpy works as well as the proof of concept by @yume-chan above for me.

Pixel 7/Android 14/Nov 2023 patches

rom1v commented 6 months ago

Please test the new binary on #4446 (it should take less time to turn the screen off or on with shortcuts).

hooligeek commented 6 months ago

Please test the new binary on #4446 (it should take less time to turn the screen off or on with shortcuts).

Tested and it does seem to turn off the screen faster.

C:\scrcpy\scrcpy-win64-v2.2-43-gc5c2734db>scrcpy --window-title=hooligeek --turn-screen-off --stay-awake --window-x=1 --window-y=31 --window-height=1008 scrcpy v2.2 https://github.com/Genymobile/scrcpy INFO: ADB device found: INFO: --> (usb) 0C101FDD4003AV device Pixel_5 C:\scrcpy\scrcpy-win64-v2.2-43-gc5c2734db\scrcpy-server: 1 file pushed, 0 skipped. 66.7 MB/s (66783 bytes in 0.001s) [server] INFO: Device: [Google] google Pixel 5 (Android 14) INFO: Renderer: direct3d INFO: Texture: 1080x2336 [server] INFO: Device screen turned off

dacs6 commented 6 months ago

Please test #4446.

Worked on my XiaoMi14 Android 14, Bravo!!!

rom1v commented 6 months ago

A simpler and faster alternative (thanks to @yume-chan): please test #4456.

AndroidDeveloperLB commented 6 months ago

@rom1v Why is it better? Faster ? More stable?

rom1v commented 6 months ago

It is faster because it runs the code in the scrcpy server process, without spawning a new dedicated Java process (which takes a few hundred milliseconds, like in #4446) to change the display power mode. And the code is much simpler.

AndroidDeveloperLB commented 6 months ago

@rom1v Thank you . Hope will work well.

rom1v commented 6 months ago

Thank you all for your tests.

I will probably publish a new version soon. Could some of you check that the fix that will be merged (#4456) works correctly for you please?

AndroidDeveloperLB commented 6 months ago

I might be wrong, but I think that sometimes, after some long time of it being used, ScrCpy is closed on its own, and the display gets turned on again (which is good in general in case the tool is closed).

Can anyone confirm?

rom1v commented 6 months ago

I might be wrong, but I think that sometimes, after some long time of it being used, ScrCpy is closed on its own

In the console, do you get WARN: Device disconnected? It yes, it is an adb disconnection (if you run adb shell or adb logcat separately, they will be closed at the same time). It's not related to the "turn screen off" feature.

https://github.com/Genymobile/scrcpy/blob/master/FAQ.md#device-disconnected

AndroidDeveloperLB commented 6 months ago

@rom1v I don't use the console as I prefer to have a single window. Any way to send you logs when it occurs? If so, where is the logs file, or how do I tell it to generate them? About console, wouldn't it be closed when the window gets closed and/or when the device is disconnected ?

AlexBurdu commented 6 months ago

Thank you all for fixing this! I can confirm it's working on Pixel 6 / Android 14 / November 23 update

thiendn160794 commented 1 month ago

Hello @yume-chan,

I hope this message finds you well. I noticed your expertise in building JAR files and I'm seeking guidance on a particular issue I'm facing.

I'm currently working on a project where I need to custom from your source code. I've attempted to create the JAR using the following Gradle kotlin DSL script:

tasks.register<Jar>("createJar") { val mainClass = "com.example.test.Main" manifest { attributes["Main-Class"] = mainClass } from("build/classes/kotlin/main") from("build/classes/java/main") from("build/intermediates/javac/debug/classes") archiveFileName.set("main.jar") }

While I can run the generated JAR file successfully using java -jar main.jar, I encounter a ClassNotFoundException when attempting to run it on an Android device by run

adb shell app_process -cp /data/local/tmp/main.jar:/system/framework/services.jar / com.example.test.Main

Could you please provide guidance on how to modify the Gradle script or any additional steps needed to ensure compatibility with Android devices?

Any assistance or suggestions you can provide would be greatly appreciated.

Thank you in advance for your help!

yume-chan commented 1 month ago

@thiendn160794 I know absolutely nothing about Gradle.

My projects start from the "No Activity" template in Android Studio.

Then I removed everything in AndroidManifest.xml (https://github.com/yume-chan/turn-off-screen/blob/main/app/src/main/AndroidManifest.xml) and res folder (I think it doesn't affect the result, only to make the bundle smaller).

Dependencies and source files are added like normal Android apps.

It can be built directly in Android Studio, or by running ./gradlew :app:assembleDebug/./gradlew :app:assembleRelease

thiendn160794 commented 1 month ago

Thank you, @yume-chan, for your response.

It took me three days to realize that simply building the .apk and then renaming it to .jar was the solution. However, the issue lies in the large size of the .jar file, which is approximately 4MB, due to its contents:

META-INF/androidx.annotation_annotation-experimental.version
META-INF/androidx.arch.core_core-runtime.version
META-INF/androidx.core_core-ktx.version
META-INF/androidx.core_core.version
META-INF/androidx.interpolator_interpolator.version
META-INF/androidx.lifecycle_lifecycle-runtime.version
META-INF/androidx.versionedparcelable_versionedparcelable.version
kotlin/annotation/annotation.kotlin_builtins
kotlin/collections/collections.kotlin_builtins
kotlin/coroutines/coroutines.kotlin_builtins
kotlin/internal/internal.kotlin_builtins
kotlin/kotlin.kotlin_builtins
kotlin/ranges/ranges.kotlin_builtins
kotlin/reflect/reflect.kotlin_builtins
META-INF/com/android/build/gradle/app-metadata.properties
classes2.dex
classes3.dex
AndroidManifest.xml
res/drawable-v21/notification_action_background.xml
res/drawable/notification_bg.xml
res/drawable/notification_bg_low.xml
res/drawable/notification_icon_background.xml
res/drawable/notification_tile_bg.xml
res/drawable-ldpi-v4/ic_call_answer.png
res/drawable-ldpi-v4/ic_call_answer_low.png
res/drawable-ldpi-v4/ic_call_answer_video.png
res/drawable-ldpi-v4/ic_call_answer_video_low.png
res/drawable-ldpi-v4/ic_call_decline.png
res/drawable-ldpi-v4/ic_call_decline_low.png
res/drawable-mdpi-v4/ic_call_answer.png
res/drawable-mdpi-v4/ic_call_answer_low.png
res/drawable-mdpi-v4/ic_call_answer_video.png
res/drawable-mdpi-v4/ic_call_answer_video_low.png
res/drawable-mdpi-v4/ic_call_decline.png
res/drawable-mdpi-v4/ic_call_decline_low.png
res/drawable-mdpi-v4/notification_bg_low_normal.9.png
res/drawable-mdpi-v4/notification_bg_low_pressed.9.png
res/drawable-mdpi-v4/notification_bg_normal.9.png
res/drawable-mdpi-v4/notification_bg_normal_pressed.9.png
res/drawable-mdpi-v4/notify_panel_notification_icon_bg.png
res/drawable-hdpi-v4/ic_call_answer.png
res/drawable-hdpi-v4/ic_call_answer_low.png
res/drawable-hdpi-v4/ic_call_answer_video.png
res/drawable-hdpi-v4/ic_call_answer_video_low.png
res/drawable-hdpi-v4/ic_call_decline.png
res/drawable-hdpi-v4/ic_call_decline_low.png
res/drawable-hdpi-v4/notification_bg_low_normal.9.png
res/drawable-hdpi-v4/notification_bg_low_pressed.9.png
res/drawable-hdpi-v4/notification_bg_normal.9.png
res/drawable-hdpi-v4/notification_bg_normal_pressed.9.png
res/drawable-hdpi-v4/notification_oversize_large_icon_bg.png
res/drawable-hdpi-v4/notify_panel_notification_icon_bg.png
res/drawable-xhdpi-v4/ic_call_answer.png
res/drawable-xhdpi-v4/ic_call_answer_low.png
res/drawable-xhdpi-v4/ic_call_answer_video.png
res/drawable-xhdpi-v4/ic_call_answer_video_low.png
res/drawable-xhdpi-v4/ic_call_decline.png
res/drawable-xhdpi-v4/ic_call_decline_low.png
res/drawable-xhdpi-v4/notification_bg_low_normal.9.png
res/drawable-xhdpi-v4/notification_bg_low_pressed.9.png
res/drawable-xhdpi-v4/notification_bg_normal.9.png
res/drawable-xhdpi-v4/notification_bg_normal_pressed.9.png
res/drawable-xhdpi-v4/notify_panel_notification_icon_bg.png
res/drawable-xxhdpi-v4/ic_call_answer.png
res/drawable-xxhdpi-v4/ic_call_answer_low.png
res/drawable-xxhdpi-v4/ic_call_answer_video.png
res/drawable-xxhdpi-v4/ic_call_answer_video_low.png
res/drawable-xxhdpi-v4/ic_call_decline.png
res/drawable-xxhdpi-v4/ic_call_decline_low.png
res/drawable-xxxhdpi-v4/ic_call_answer.png
res/drawable-xxxhdpi-v4/ic_call_answer_low.png
res/drawable-xxxhdpi-v4/ic_call_answer_video.png
res/drawable-xxxhdpi-v4/ic_call_answer_video_low.png
res/drawable-xxxhdpi-v4/ic_call_decline.png
res/drawable-xxxhdpi-v4/ic_call_decline_low.png
res/drawable-anydpi-v21/ic_call_answer.xml
res/drawable-anydpi-v21/ic_call_answer_low.xml
res/drawable-anydpi-v21/ic_call_answer_video.xml
res/drawable-anydpi-v21/ic_call_answer_video_low.xml
res/drawable-anydpi-v21/ic_call_decline.xml
res/drawable-anydpi-v21/ic_call_decline_low.xml
res/layout/custom_dialog.xml
res/layout/ime_base_split_test_activity.xml
res/layout/ime_secondary_split_test_activity.xml
res/layout-v21/notification_action.xml
res/layout-v21/notification_action_tombstone.xml
res/layout-v21/notification_template_custom_big.xml
res/layout-v21/notification_template_icon_group.xml
res/layout/notification_template_part_chronometer.xml
res/layout/notification_template_part_time.xml
resources.arsc
classes.dex