chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
748 stars 127 forks source link

How to use Chaquopy #1111

Closed supergod100 closed 3 months ago

supergod100 commented 3 months ago

I added the code as per the documentation but it doesn't work, please tell me what's wrong. kotlin compiler version 1.9.0 language version 1.9 API version 1.9 target JVM version 1.8 Android Gradle Plugin version 8.3.0 Gradle version 7.6.4

build.gradles.kts(project)

plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.jetbrainsKotlinAndroid) apply false
    id("org.jetbrains.kotlin.android") version "1.9.23"
    id("com.chaquo.python") version "15.0.1" apply false
}

build.gradles.kts(Module:app)

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsKotlinAndroid)
    id("com.android.application")
    id("kotlin-android")
    id("com.chaquo.python")
}

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

    defaultConfig {
        applicationId = "com.example.ex01"
        minSdk = 21
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
        ndk {
            abiFilters += listOf("arm64-v8a", "x86_64")
        }
        python {
            version "3.12"
            buildPython "C:/Users/user/AppData/Local/Programs/Python/Python312/python.exe"
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}
}

setting.gradles(project settings)

pluginManagement {
    repositories {
        google {
            content {
                includeGroupByRegex("com\\.android.*")
                includeGroupByRegex("com\\.google.*")
                includeGroupByRegex("androidx.*")
            }
        }
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven {
            url = uri( "https://chaquo.com/maven" )
        }
        google()
        mavenCentral()
    }
}

rootProject.name = "ex01"
include(":app")

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Ex01"
        tools:targetApi="31"
        android:name="com.chaquo.python.android.PyApplication"//add
>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.Ex01">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Error description


FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\user\AndroidStudioProjects\ex01\build.gradle.kts' line: 2

* What went wrong:
Plugin with id 'org.jetbrains.kotlin.android' was already requested at line 2

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.plugin.management.internal.InvalidPluginRequestException: Plugin with id 'org.jetbrains.kotlin.android' was already requested at line 2
    at org.gradle.plugin.use.internal.PluginRequestCollector.listPluginRequests(PluginRequestCollector.java:97)
    at org.gradle.plugin.use.internal.PluginRequestCollector.getPluginRequests(PluginRequestCollector.java:70)
    at Program.execute(Unknown Source)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost.eval(Interpreter.kt:532)
    at org.gradle.kotlin.dsl.execution.Interpreter.eval(Interpreter.kt:184)
mhsmith commented 3 months ago

You've listed the Kotlin plugin twice:

    alias(libs.plugins.jetbrainsKotlinAndroid) apply false
    id("org.jetbrains.kotlin.android") version "1.9.23"

Remove the second line.

supergod100 commented 3 months ago

Thank you! Problems are resolved!