在虚拟环境中启动新Kivy项目时重复下载依赖求助
Hey there, I’ve dealt with this exact frustrating issue before—having to wait for SDK/NDK downloads every time you start a new Kivy project in a fresh virtual environment is a huge waste of time. Let’s walk through actionable solutions to stop this from happening:
1. Set Global Environment Variables for SDK/NDK
Kivy’s build tools (like Buildozer) check for system-wide environment variables first before downloading dependencies. Here’s how to set them:
- On Linux/macOS: Add these lines to your shell config file (
.bashrc,.zshrc, or.profile), or directly in your virtual environment’sactivatescript to keep it isolated:export ANDROID_SDK_ROOT="/path/to/your/pre-downloaded/sdk" export ANDROID_NDK_ROOT="/path/to/your/pre-downloaded/ndk" export ANDROID_BUILD_TOOLS_VERSION="33.0.2" # Use the version Kivy requires - On Windows: Open System Properties → Advanced → Environment Variables, add new system/user variables with the same keys and paths to your pre-downloaded SDK/NDK.
Once set, any new Kivy project (even in a fresh virtual env) will use these existing paths instead of downloading everything again.
2. Configure Buildozer’s Global Settings
Buildozer uses a per-project buildozer.spec by default, but you can create a global config to apply settings across all projects:
- Create a global config file at
~/.buildozer/config(Linux/macOS) or%USERPROFILE%\.buildozer\config(Windows). - Add these lines to the file, replacing paths with your existing SDK/NDK locations:
[app] android.sdk_path = /path/to/your/sdk android.ndk_path = /path/to/your/ndk android.build_tools_version = 33.0.2
Now every new Buildozer project you initialize will inherit these settings, skipping the download step.
3. Reuse a Preconfigured Base Virtual Environment
Instead of creating a new virtual environment from scratch every time:
- Create a base virtual environment, install Kivy, and configure all the SDK/NDK paths as above.
- For new projects, either:
- Clone the base environment (use
cp -r /path/to/base/env /path/to/new/envon Linux/macOS, or copy the folder on Windows), then activate it and install any project-specific dependencies. - Export the base environment’s dependencies to a
requirements.txtwithpip freeze > requirements.txt, then create a new env and runpip install -r requirements.txt—just make sure the SDK/NDK environment variables are set in the new env too.
- Clone the base environment (use
4. Manually Pre-Download Dependencies
If you haven’t already downloaded the SDK/NDK:
- Grab the compatible versions (check Kivy’s docs for exact NDK/SDK versions required for your Kivy version—e.g., Kivy 2.2.0 often uses NDK r25b and SDK API level 33).
- Download the Android SDK Command Line Tools, extract it to a fixed directory, then run
sdkmanagerto install required platform tools and build tools. - Download the matching NDK version, extract it to a fixed directory.
- Point your environment variables or Buildozer config to these paths as outlined in steps 1 and 2.
Pro Tip
Always double-check that the SDK/NDK versions match what your Kivy version expects—using an incompatible version can lead to build errors, even if you skip the download.
内容的提问来源于stack exchange,提问作者John Albert

