pythonandroidchaquopy

Problems with build.gradle file when trying to integrate chaquopy into Android Studio Jellyfish


I am trying to integrate chaquopy into Android Studio Jellyfish to allow for the use of Python but there are problems with the module-level build.gradle.kts file and I get these errors when I sync the gradle files:

Unable to load class 'com.android.build.api.variant.Variant'
com.android.build.api.variant.Variant

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

This is my top-level build.gradle.kts file:

plugins {
        id("com.chaquo.python") version "15.0.1" apply false
}

This is my module-level build.gradle.kts file:

plugins {
    alias(libs.plugins.android.application)
    id("com.chaquo.python")
}

android {
    namespace = "com.example.pythontestwithjava"
    compileSdk = 34

    flavorDimensions += "pyVersion"
    productFlavors {
        create("py310") { dimension = "pyVersion" }
        create("py311") { dimension = "pyVersion" }
    }

    defaultConfig {
        applicationId = "com.example.pythontestwithjava"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            // On Apple silicon, you can omit x86_64.
            abiFilters += listOf("arm64-v8a", "x86_64")
        }

        python {
            version "3.11"
            pip {
                // A requirement specifier, with or without a version number:
                install "scipy"
                install "requests==2.24.0"

                // An sdist or wheel filename, relative to the project directory:
               // install "MyPackage-1.2.3-py2.py3-none-any.whl"

                // A directory containing a setup.py, relative to the project
                // directory (must contain at least one slash):
             //   install "./MyPackage"

                // "-r"` followed by a requirements filename, relative to the
                // project directory:
            //    install "-r", "requirements.txt"
            }
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding = true
    }


}

chaquopy {
    defaultConfig { }
    productFlavors { }
    sourceSets { }

    productFlavors {
        getByName("py310") { version = "3.10" }
        getByName("py311") { version = "3.11" }
    }
}

dependencies {

    implementation(libs.appcompat)
    implementation(libs.material)
    implementation(libs.constraintlayout)
    implementation(libs.navigation.fragment)
    implementation(libs.navigation.ui)
    testImplementation(libs.junit)
    androidTestImplementation(libs.ext.junit)
    androidTestImplementation(libs.espresso.core)
}

My specific errors (highlighted red in Android Studio) in the module-level build.gradle.kts file are:

undefined reference: python
undefined reference: pip
undefined reference: install

Please let me know how I can fix these errors. TIA.


Solution

  • The python DSL inside the android block doesn't have full IDE support, so that's why it will be highlighted in red. But it should still work anyway when you build your app. I'm not sure if the error at the top of your question is related.

    The top-level chaquopy block has better IDE integration, so that is now recommended instead.