SSL in Kivy for android development - android

I was developing a mobile app that can send mqtt messages to AWS Iot. It needs paho-mqtt library. It can be included in buildozer.spec requirements. My problem is in the SSL part, because I need to import SSL in the code which seems to have a problem with the python version running in python-for-android, which is 2.7.2.
The code is below, which works fine on the PC, but on th phone it is not working.
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.label import Label
import paho.mqtt.publish as mqtt
import paho.mqtt.client as mqttclient
#
class MqttTest(App):
def build(self):
topic = "topic1"
my_ca_cert = "RootCA.pem"
my_pri_cert = "my.cert.pem"
my_key_cert = "my.private.key"
try:
import ssl
mqttc = mqttclient.Client("Python_Ex_Pub")
mqttc.tls_set(my_ca_cert,
certfile=my_pri_cert,
keyfile=my_key_cert,
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)
mqttc.connect("myaddress", 8883)
mqttc.publish(topic, "This is a test pub from Python.")
return Label(text="Hi it works!")
except Exception as e:
import traceback
a=traceback.format_exc()
try:
f1=open("/storage/emulated/0/Download/err.txt","w")
f1.write(str(a))
f1.close()
except:
pass
return Label(text=str (a))
if __name__ == '__main__':
MqttTest().run()
without adding anything related to SSL in buildozer.spec requirements, I get the following error:
no module named _ssl
If I added openssl as one of the requirements, then I get the following error:
'module' object has no attribute 'PROTOCOL_TLSv1_2'

As noted PROTOCOL_TLSv1_2 was added in later Python 2 version than p4a provides.
You can try to build apk with Python 3:
Change your buildozer's requirements line replacing python2 with python3crystax
Download and unpack crystax ndk here
Change your buildozer's android.ndk_path to point unpacked crystax ndk directory
Run buildozer android debug
If you're lucky enough you'll be able to build apk with Python 3 without any other actions.

I encountered the similar issue. I am trying to import pydrive without doing anything at first. Below is from logcat.
I tried 3 os environment, osx, ubuntu, bulldozer vm. All give me the same error.
I/python (13323): File "/Users/macuser/test/.buildozer/android/platform/build/dists/myapp/private/lib/python2.7/site-packages/httplib2/init.py", line 960, in
I/python (13323): AttributeError: 'module' object has no attribute 'HTTPSConnection'
In httplib.py, i found below code.
try:
import ssl
except ImportError:
pass
else:
class HTTPSConnection(HTTPConnection):
"This class allows communication via SSL."
So I suspect ssl not import successfully. Then, check ssl.py and found PROTOCOL_SSLv3 cannot import successfully.
from _ssl import PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1
Until here i am not able to proceed further. I am using python3crystax and it still not work as claim in this post.
Since buildozer will download all library separately, i suspect python automatically downloaded by bulldozer is a python version that doesn't include ssl3 support.

Related

Python: Detect Android

I'm surprised to find very little info on this topic. I want to detect if a user is running Android. I'm using:
platform.dist()
This perfectly detects all all OSs and different Linux distros. However, when running this on Android, it system name is always returned as "Linux". Is there any way to detect Android? I do not want to include any 3rd party modules, since this has to be as portable as possible. Is there maybe some specific Android function that I can call in a try-catch?
You can do this with Kivy
from kivy.utils import platform
if platform == 'android':
# do something
EDIT:
As I said, I cannot use any 3rd party libs
Take a look at how Kivy implemented it.
def _get_platform():
# On Android sys.platform returns 'linux2', so prefer to check the
# presence of python-for-android environment variables (ANDROID_ARGUMENT
# or ANDROID_PRIVATE).
if 'ANDROID_ARGUMENT' in environ:
return 'android'
from os import environ
if 'ANDROID_BOOTLOGO' in environ:
print("Android!")
else:
print("!Android")
System name is returned 'Linux' because Android is also based on the Linux Kernel.Android is the name of the distro but deep down its linux.
This is a very easy you can do it without kivy or with kivy module using hpcomt module
import hpcomt
os_name = hpcomt.Name()
print(os_name)
# Output
# Android
As of Python 3.7, you can check for the existence of sys.getandroidapilevel():
import sys
if hasattr(sys, 'getandroidapilevel'):
# Yes, this is Android
else:
# No, some other OS

Building Python packages succeeds, but Scikit-learn is improperly built

I created an app using Scikit-learn and Kivy and it was built with Buildozer.
This is the code of main.py :
# coding=utf-8
import kivy
import sys
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
try:
from sklearn import svm, datasets
except:
return Label(text=str(sys.exc_info()[1]))
else:
return Label(text='Scikit-learn OK')
if __name__ == '__main__':
MyApp().run()
I specified Scikit-learn in the requirements in buildozer.spec :
[app]
title = Kivyris
package.name = kivyris
package.domain = org.test
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 0.1
requirements = kivy,numpy,scipy,scikit-learn
orientation = landscape
fullscreen = 1
log_level = 2
warn_on_root = 1
I ran buildozer android_new debug deploy run (no error, APK file created and deployed) but I have the following error when the app is launched :
Cannot load library:
Contents of /data/data/org.test.kivyris/files/app/lib/python2.7/site-packages/sklearn/check_build:
__init.pyo setup.pyo _check_build.so
It seems that sckikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget to build the package before using it; run python setup.py install or make in the source directory.
If you have used an installer, please check that it is suited for your Python version, your operating system and your platform.
On Windows and Ubuntu using python main.py it works well :
Scikit-Learn OK
I installed Scikit-learn using sudo apt-get install python-scikits-learn on Ubuntu 16.04 LTS. This is some informations from the device on which I ran the app :
import platform failed; platform.platform() : I couldn't get this info (app failed to launch), but it's an Android 5.1.1
import sys OK; sys.version : 2.7.2 (default, Mar 6 2017, 06:05:36) [GCC 4.8]
import numpy OK; Numpy.version : 1.9.2
import scipy OK; Scipy.version : 0.18.1
import sklearn : error (see above).
I tried a few things unsuccessfully, so I looked online :
https://unix.stackexchange.com/questions/240239/building-python-packages-succeeds-but-package-is-improperly-built/240260 : didn't work for me.
Import scikit in C# application : not really what I was looking for.
https://github.com/scikit-learn/scikit-learn/issues/433 : works for Mac, not for Ubuntu.
Cannot import Scikit-Learn : scipy works for me.
I didn't find anything useful and I have no idea how to solve that. Any idea please ?
Thank you.
When you're trying to build your app for Android with module that uses pure Python, everything is ok: this module would be run by Python interpreter built for Android and shipped with your app.
Things much worse when your trying to build app for Android with module contains C extension or dependencies with C extensions (such as Scikit-learn). On Windows or Linux C Extensions would be compiled using distutils, but for Android it's problem: C Extensions can't be compiled this way.
python-for-android handles this problem providing mechanism of recipes:
recipe: A recipe is a file that defines how to compile a requirement.
Any libraries that have a Python extension must have a recipe in p4a,
or compilation will fail. If there is no recipe for a requirement, it
will be downloaded using pip.
You can see list of existing recipes here. As you can see no one did one for Scikit-learn, so I assume it can't be build for Android right now.
You can try to create recipe for this module manually or ask someone to help in kivy Google group. Note, that it's probably not trivial task.

python ImportError using PyAL with kivy

To be able to use 3D sound, I am trying to use the OpenAl sound library (PyAL) in kivy. Here's the python header of the main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import kivy
kivy.require('1.7.2')
from kivy.app import App
from kivy.uix.button import Button
from openal.loaders import load_wav_file
from openal.audio import SoundSink, SoundSource, SoundData, SoundListener
[...]
OpenAl is not included in the standard options provided by kivys python for android, so I created a pyal/recipe.sh for it.
#!/bin/bash
# version of your package
VERSION_pyal=${VERSION_pyal:-0.1.0}
# dependencies of this recipe
DEPS_pyal=()
# url of the package
URL_pyal=https://bitbucket.org/marcusva/py-al/downloads/PyAL-0.1.0.tar.gz
# md5 of the package
MD5_pyal=862a9345004c76651dcf91b7d990fe84
# default build path
BUILD_pyal=$BUILD_PATH/pyal/$(get_directory $URL_pyal)
# default recipe path
RECIPE_pyal=$RECIPES_PATH/pyal
# function called for preparing source code if needed
# (you can apply patch etc here.)
function prebuild_pyal() {
true
}
# function called to build the source code
function build_pyal() {
true
}
# function called after all the compile have been done
function postbuild_pyal() {
true
}
Compiling the toolchain works:
Download package for pyal
[...]
Call prebuild_pyal
[...]
Call build_pyal
[...]
Call postbuild_pyal
[...]
All done !
Building the apk works, and so does installing. But when running the app on the device (Nexus 4), it closes immediately. Netcat complains:
[...]
I/python (13175): ImportError: No module named openal.loaders
I/python (13175): Python for android ended.
[...]
Somehow the inclusion of PyAL in the toolchain went wrong. Any ideas what the problem could be?
On my Laptop (Ubuntu 14.04), everything works fine. Not a huge surprise, since it should use the python from the repository, not kivys python for android.
Your python-for-android recipe is for PyAL (Python bindings for OpenAL), but PyAL requires that OpenAL be installed to do anything. You probably don't need a recipe for PyAL - which appears to be a pure Python module - but you will have to create a recipe for OpenAL. Creating that recipe will require knowing how to build OpenAL, so you may need help from their maintainers.

packaging kivy application to android

I never had a problem compiling my kivy applications before(done it many times) but i have one now : it throws the application normally on my Phone but when i open it it freezes on the presplash icon and gives me a message : "could not extract public data". i thought it was my sqlite3 file.
So i have changed its name and added to ./distribute.sh -m "sqlite3 kivy" and still have the problem.my build.py looks like this:
./build.py --dir /home/toufic/Desktop/Pay4 --name "Lebanese PayCalc" --package org.demo.PayCalc --version 1.2.4 --icon /home/toufic/Desktop/Pay4/lp.png --presplash /home/toufic/Desktop/Pay4/onmyown.png --permission INTERNET --permission WRITE_EXTERNAL_STORAGE --window debug installd
if i try to compile it with buildozer i have the following error :
File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in <lambda>
return _DeadlockWrap(lambda: self.db[key]) # self.db[key]
bsddb.db.DBPageNotFoundError: (-30985, 'DB_PAGE_NOTFOUND: Requested page not found')
in my main.py file i have the following dependencies:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from net_pay_oop import * # it's a class i have written and using its instance in my code
from kivy.uix.popup import Popup
from kivy.uix.label import Label
import sqlite3
import datetime
import os
import math
import smtplib
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager,Screen
import sys
long story short i need the right way to compile it either with python-for-android or the Buildozer utility(i'm new to this). can anyone please help according to the modules that i am using or is there a way to debug the build and know where the problem hides?
Edit: so I kind of made the app open but SMTP services at not working. Should I do ./distribute.sh "openssl sqlit3 kivy"?
the solution to smtp services to be enabled was to includ in distribute.sh the openssl option but still the error dispayed by my device : "could not extract public data" was not solved. i guess it is an sqlite3 issue but the thing is that this only happens upon first installation and my application loads normally afterwards and displays all saved data and browses them normally.

Issues using a variable in monkeyrunner's ".installPackage()"

I haven't been able to find much on this topic.
I am trying to automate application testing, to where I place an app in a particular folder and I run the script: monkeyrunner.bat -v ALL myscript.py, and the script executes on whatever apk is in the folder called apkrepository. This makes it to where I do not have to alter my python script every time I test a new application.
The part where I am running into trouble is I am trying to use a variable for device1.installPackage()
See below for the code leading upto it.
installme = os.popen(r'dir C:\users\uname\desktop\apkrepository /A:-d /B').read()
print installme
# => com.application.android.apk #or whatever the package name is
filepath = r'C:\users\uname\desktop\apkrepository'
androidapp = filepath + '\\\' + installme
print androidapp
# => C:\users\uname\desktop\apkrepository\com.application.android.apk
#This exactly what I type below manually to get it to work
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
#Emulator was started in previous section of code, which is not shown here.
device1.installPackage(androidapp)
#DOES NOT WORK!!
device1.installPackage('c:\users\uname\desktop\apkrepository\com.application.android.apk')
#The only way it works seems to be to write the path in manually everytime.
I have tried many different ways to get this to work correctly, and I wasn't sure if it was something in the way(s) I was/were trying to do it. If the variable prints the correct file path I do not see how it would have issues working. This is probably something really easy, but this is where I am stuck. The error it gives:
E/Device: Error dyring Sync: Local Path does not exist. Error installing package C:\users\uname\desktop\apkrepository\com.application.android.apk
I am using windows 7 64 bit with python2.7 and the android sdk.
Thank you for any input/assistance provided! I have been stumped by this for a couple days.
Variables DO work for other places (monkeyrunner affiliated classes), like the device1.startActivity(component=runcomponent), where runcomponent is a combination of package + activity variables. Also note: I showed both device1.installPackages side by side for easy viewing. I do not run both in a row on my script.
I tried the same in linux machine, it works. Please have a look at the monkeyrunner script.
#! /usr/bin/env monkeyrunner
import re
import sys
import os
import java
import glob
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
mydir="/home/user/apk"
os.chdir(mydir)
for files in glob.glob("*.apk"):
print files
print "path " ,os.path.abspath(mydir+"/"+files)
device1.installPackage(mydir+"/"+files)

Categories

Resources