Profile
December 22, 2022

How to use Python Code In Android Easy Steps

Are you looking to work on python? and do not know how to integrate python code in your Android Studio Project? You should not worry about that I will explain you in very easy steps:

So, Today we are using Chequpy to use Python in Android and we should know the basics of it:

Chaquopy is a Python library that allows you to use Python in your Android app. It is an alternative to using Java or Kotlin to build Android apps, and it can be especially useful for developers who are already familiar with Python and want to use it to develop mobile apps.

With Chaquopy, you can use Python to write the code for your Android app, including the user interface and any custom logic. You can also use Python libraries and packages, such as NumPy and Pandas, to perform data processing tasks within your app.

Chaquopy includes a Python interpreter that runs within your Android app, so you can use it to execute Python code at runtime. It also includes tools for building and packaging your app, so you can easily distribute it to users.

Overall, Chaquopy can be a useful tool for Android app development, especially if you are already proficient in Python and want to use it to build mobile apps.


Step 1:

Download & Install Python 3.8 on your machine

 

Step 2:

Launch Android Studio and Create a new Project

 

Step 3:

Basic setup to use Chaquopy in Android:

Add this to your top-level build.gradle file, and sync the gradle

plugins {
    id 'com.chaquo.python' version '13.0.0' apply false
}

Also, add in the module-level build.gradle file (usually in the app directory), again sync the gradle

plugins {
    id 'com.android.application'
    id 'com.chaquo.python'
}

 

The Python interpreter is a native component, so you must use the abiFilters setting to specify which ABIs your app should support. The currently available ABIs are:

armeabi-v7a, supported by virtually all Android devices.

arm64-v8a, supported by most recent Android devices.

x86, for the Android emulator.

x86_64, for the Android emulator.

During development you’ll probably want to enable them all, i.e.:

Add this in your default config like below

defaultConfig {
	....
    ndk {
       abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
    }
}

Now everything is set up accurately and you must see a python folder inside the main folder of the project. should be looking like this:

 

Step 4:

Initialize Python in your project to use the python code:

if (!Python.isStarted()) {
    Python.start(AndroidPlatform(this));
}

 

Step 5:

create a python code file inside the python directory where you can write python code: I have created a “sample.py” as below

 

Step 6:

 write some python code in the ‘sample.py’ file:

def main():
    return "Hello From Python!"

 

Step 7:

how to call python code from Java or Kotlin:

val pyObject = Python.getInstance().getModule("sample")
val result = pyObject.callAttr("main").toString()

the result will contain the output of the main method from the python code.

 

The complete Android Project can be downloaded from this link.

 

Please share this article Thanks! 

 

Last edited on December 22, 2022 by Admin

Latest articles