Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] ModuleNotFoundError: No module named 'androidx' #1693

Closed
Pairman opened this issue Mar 13, 2024 · 7 comments
Closed

[Android] ModuleNotFoundError: No module named 'androidx' #1693

Pairman opened this issue Mar 13, 2024 · 7 comments
Labels
android The issue relates to Android mobile support. awaiting details More details are needed before the issue can be triaged. bug A crash or error in behavior.

Comments

@Pairman
Copy link
Contributor

Pairman commented Mar 13, 2024

Describe the bug

After updating briefcase to 0.3.17 from 0.3.16, briefcase package android -p apk will show this warning:

[***] Generating application template...

*************************************************************************
** WARNING: App does not define build_gradle_dependencies              **
*************************************************************************

    The Android configuration for this app does not contain a
    `build_gradle_dependencies` definition. Briefcase will use a default
    value of:

        build_gradle_dependencies = [
            "androidx.appcompat:appcompat:1.0.2",
            "androidx.constraintlayout:constraintlayout:1.1.3",
            "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
        ]

    You should add this definition to the Android configuration
    of your project's pyproject.toml file. See:

        https://briefcase.readthedocs.io/en/stable/reference/platforms/android.html#build_gradle-dependencies

    for more information.

*************************************************************************

But when I add the dependencies to my pyproject.toml like this, my APP will crash:

[tool.briefcase.app.xdcheckin.android]
...
build_gradle_dependencies = [
	"androidx.appcompat:appcompat:1.6.1",
	"com.google.android.material:material:1.11.0",
]

Related logcat:

03-13 19:48:33.075 31157 31157 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity}: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'androidx'

Steps to reproduce

  1. Add gradle dependencies to pyproject.toml:
[tool.briefcase.app.xdcheckin.android]
build_gradle_dependencies = [
	"androidx.appcompat:appcompat:1.6.1",
	"com.google.android.material:material:1.11.0",
]
  1. Build APP:
briefcase package android -p apk
  1. Install.
  2. Open and the built APP will crash.

Expected behavior

The APP will run normally.

Screenshots

No response

Environment

  • Operating System: Android 13 arm64
  • Python version: 3.10
  • Software versions:
    • Briefcase: 0.3.17

Logs

Full logcat:

03-13 19:48:31.088  1698  3137 I ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity bnds=[859,1446][1135,1851]} from uid 10253

03-13 19:48:31.129  1698  2311 I ActivityManager: Start proc 31157:org.eu.pnxlr.git.xdcheckin/u0a449 for next-top-activity {org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity}

03-13 19:48:31.132  3958  7046 W SQLiteLog: (28) double-quoted string literal: "org.eu.pnxlr.git.xdcheckin"

03-13 19:48:31.280 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37645): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/zlib.so" dev="dm-0" ino=2278265 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.310 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37646): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/java/chaquopy.so" dev="dm-0" ino=2389957 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.320 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37647): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/_ctypes.so" dev="dm-0" ino=2447007 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.320 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37648): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/_struct.so" dev="dm-0" ino=2307231 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.340 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37649): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/_bz2.so" dev="dm-0" ino=2484086 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.350 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37650): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/_lzma.so" dev="dm-0" ino=2341836 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:31.370 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37651): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/bootstrap-native/arm64-v8a/math.so" dev="dm-0" ino=2397982 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:32.330 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37681): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/AssetFinder/requirements/Crypto/Cipher/_raw_cfb.so" dev="dm-0" ino=2470719 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:32.370 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37682): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/AssetFinder/requirements/Crypto/Cipher/_raw_ofb.so" dev="dm-0" ino=2440225 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:32.400 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37683): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/AssetFinder/requirements/Crypto/Cipher/_raw_ctr.so" dev="dm-0" ino=2399529 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:32.440 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37684): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/AssetFinder/requirements/Crypto/Util/_strxor.so" dev="dm-0" ino=2399255 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:32.470 31157 31157 W r.git.xdcheckin: type=1400 audit(0.0:37685): avc: granted { execute } for path="/data/data/org.eu.pnxlr.git.xdcheckin/files/chaquopy/AssetFinder/requirements/Crypto/Hash/_BLAKE2s.so" dev="dm-0" ino=2355505 scontext=u:r:untrusted_app:s0:c193,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c193,c257,c512,c768 tclass=file app=org.eu.pnxlr.git.xdcheckin

03-13 19:48:33.075 31157 31157 E AndroidRuntime: Process: org.eu.pnxlr.git.xdcheckin, PID: 31157

03-13 19:48:33.075 31157 31157 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity}: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'androidx'

03-13 19:48:33.077  1698  3930 W ActivityTaskManager:   Force finishing activity org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity

03-13 19:48:33.095  1698  3137 I ActivityManager: Process org.eu.pnxlr.git.xdcheckin (pid 31157) has died: fg  TOP 

03-13 19:48:33.125  1698  2304 W InputManager-JNI: Input channel object '3ee9105 Splash Screen org.eu.pnxlr.git.xdcheckin (client)' was disposed without first being removed with the input manager!

03-13 19:48:33.578  1698  2303 W ActivityTaskManager: Activity top resumed state loss timeout for ActivityRecord{fda949 u0 org.eu.pnxlr.git.xdcheckin/org.beeware.android.MainActivity} t-1 f}}


Additional context

No response

@Pairman Pairman added the bug A crash or error in behavior. label Mar 13, 2024
@rmartin16 rmartin16 added the android The issue relates to Android mobile support. label Mar 13, 2024
@rmartin16
Copy link
Member

After upgrading Briefcase, it can be necessary to run briefcase create android so any related updates are incorporated in to your app's build. After that, run briefcase run android --log and upload the created logfile if the issue persists.

@Pairman
Copy link
Contributor Author

Pairman commented Mar 13, 2024

After upgrading Briefcase, it can be necessary to run briefcase create android so any related updates are incorporated in to your app's build. After that, run briefcase run android --log and upload the created logfile if the issue persists.

I delete and re-clone my repo before packaging every time, so I think this does not relate to the previous cache.

@freakboy3742
Copy link
Member

I'm not sure which repo you're referring to - but the only way I'm aware that this issue could manifest is if:

  1. you haven't saved your pyproject.toml file before running briefcase create android
  2. the build_gradle_dependencies setting has been added to the wrong section of your app config
  3. you've somehow preserved your build folder, so a new template app isn't being generated.

If you're certain that you're doing all these things, please provide the log file as requested and we can investigate further.

@freakboy3742 freakboy3742 added the awaiting details More details are needed before the issue can be triaged. label Mar 14, 2024
@Pairman
Copy link
Contributor Author

Pairman commented Mar 14, 2024

I'm sure that I've properly done the steps before packaging.

With the dependencies suggested in the original warning, the APP will not crash:

build_gradle_dependencies = [
	"androidx.appcompat:appcompat:1.0.2",
	"androidx.constraintlayout:constraintlayout:1.1.3",
	"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]

Yet with these, the APP will crash:

build_gradle_dependencies = [
	"androidx.appcompat:appcompat:1.6.1",
	"com.google.android.material:material:1.11.0",
]

briefcase.2024_03_14-11_13_19.package.log
logcat.log

@freakboy3742
Copy link
Member

So - reading this more closely... there's no bug here.

When you updated your project, you were told to add:

        build_gradle_dependencies = [
            "androidx.appcompat:appcompat:1.0.2",
            "androidx.constraintlayout:constraintlayout:1.1.3",
            "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
        ]

to your project. You have then added:

build_gradle_dependencies = [
	"androidx.appcompat:appcompat:1.6.1",
	"com.google.android.material:material:1.11.0",
]

which... isn't what you were told to add. As a result, your app doesn't work. Based on your most recent post - if you do what the warning message asked you to do... it works. So - there's no bug here.

The underlying problem here is that "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0", is a required component. The way it manifests is "can't import androidx" - this is admittedly a confusing error, especially given that androidx.appcompat is included, but it's an artefact of how the Java imports work. Unfortunately, Chaquopy doesn't have access to the "tail" of the import path that fails; it only knows the "head" of the import failed - so it reports that androidx is the problem.

The logcat extract you've provided doesn't include any of the Python logs, and Briefcase can't capture Android's adb console output, so the log files you've provided in your most recent post don't show this - but I'll bet that if you look at the full error generated by Python when you run the app with Briefcase, you'll have a stack trace that indicates the problem is on line 9 of toga_android/widgets/detailedlist.py.

I'm guessing you got to this position by generating a clean Briefcase project, noticing that the "SwipeRrefreshLayout" was listed as "needed by DetailedList", and omitted it as your app doesn't use DetailedList. As of Toga 0.4.2, you need to have the SwipeRefreshLayout library in your app, even if you're not using DetailedList. You'll note that in a fresh project, the component isn't commented out - unlike other optional libraries (like the WebView libraries on Linux projects).

The "SwipeRefreshLayout" requirement is is a requirement that could be relaxed - I've logged beeware/toga#2454 to describe the change that is required - but until a fix for that is released, you'll need to include SwipeRefreshLayout in your app's gradle dependencies.

@shula
Copy link

shula commented Jul 3, 2024

  1. this step (SwipeRefreshLayout) is documented in TOGA docs, and here in the "issues"
    -- but it doesn't appear during the briefcase build/run workflow,
    and no explanation is issued, besides the java exception.

Is there a step in the workflow We / or I could add a source analysis, and warn the developer in case "DetailedList(" is used in the code, while the proper extension is not defined in the TOML file?

  1. What's the "price" for enabling SwipeRefreshLayout by default, for all projects?
    it can always be turned off.
    Enabling it by default will save lots of time to everyone going this path.

  2. tip for debuggers:
    Even after including SwipeRefreshLayout in the TOML file, the project won't run:
    the solution is the delete existing "BUILD" folders. A simple "update" / -u isn't enough.

@mhsmith
Copy link
Member

mhsmith commented Jul 8, 2024

could add a source analysis, and warn the developer in case "DetailedList(" is used in the code, while the proper extension is not defined in the TOML file?

This has already been done in the current version of Toga: beeware/toga#2595.

What's the "price" for enabling SwipeRefreshLayout by default, for all projects?
it can always be turned off.
Enabling it by default will save lots of time to everyone going this path.

I agree: we discussed this recently at #1845 (comment), and I'm still inclined towards including everything that's needed by Toga. 24.1 MB vs 24.6 MB is not a significant difference.

At the very least we should include SwipeRefreshLayout, because DetailedList will be much more commonly used than MapView.

Even after including SwipeRefreshLayout in the TOML file, the project won't run:
the solution is the delete existing "BUILD" folders. A simple "update" / -u isn't enough.

Re-running briefcase create android would also solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android The issue relates to Android mobile support. awaiting details More details are needed before the issue can be triaged. bug A crash or error in behavior.
Projects
None yet
Development

No branches or pull requests

5 participants