Skip to content

Commit

Permalink
API30 - Update the Android sample app to handle the new library. Not …
Browse files Browse the repository at this point in the history
…sure where I broke requests, but it's not working and I'm not spending a ton of time to figure it out. I import something else now.
  • Loading branch information
Brian Spratke committed Jul 9, 2024
1 parent 5c989f1 commit ad775ce
Show file tree
Hide file tree
Showing 537 changed files with 23,926 additions and 33,298 deletions.
28 changes: 13 additions & 15 deletions AndroidApp/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ apply plugin: 'com.android.application'
android {
defaultConfig {
applicationId "com.example.pythontest"
compileSdkVersion 33
minSdkVersion 23
targetSdkVersion 33
compileSdkVersion = 34
minSdkVersion 30
targetSdkVersion 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -26,36 +26,34 @@ android {
}

packagingOptions {
pickFirst 'lib/arm64-v8a/libpython3.11.so'
pickFirst 'lib/armeabi-v7a/libpython3.11.so'
pickFirst 'lib/x86_64/libpython3.11.so'
pickFirst 'lib/arm64-v8a/libpython3.12.so'
pickFirst 'lib/x86_64/libpython3.12.so'
}

// Only building ARM currently
// productFlavors allows us to specify exactly which ABIs we support
flavorDimensions "arm8", "arm", "x86_64"
flavorDimensions.add("arm8")
flavorDimensions.add("x86_64")
productFlavors {
arm8 {
dimension "arm8"
ndk {
//noinspection ChromeOsAbiSupport
abiFilters 'arm64-v8a'
}
}
arm {
dimension "arm"
ndk {
abiFilter 'armeabi-v7a'
}
}
x86_64 {
dimension "x86_64"
ndk {
abiFilter 'x86_64'
}
}
}


namespace 'com.example.pythontest'
buildFeatures {
aidl true
buildConfig true
}
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions AndroidApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.pythontest">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
Expand Down
Binary file removed AndroidApp/app/src/main/assets/Python32.zip
Binary file not shown.
Binary file modified AndroidApp/app/src/main/assets/Python64.zip
Binary file not shown.
Binary file modified AndroidApp/app/src/main/assets/Pythonx86_64.zip
Binary file not shown.
5 changes: 3 additions & 2 deletions AndroidApp/app/src/main/assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ def main():

print('You should see this #1')
try:
import requests
except:
import __phello__
except Exception as e:
print(Exception, e)
print('You should NOT see this!!!!')
finally:
print('You should see this #2')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected void onCreate(Bundle savedInstanceState)

// load library to ensure we have access to native layer
// we do this because if the service isn't running yet, we don't have access to setLMSParameters
System.loadLibrary("python3.11");
System.loadLibrary("python3.12");
System.loadLibrary("pythonthread");


Expand All @@ -34,9 +34,13 @@ protected void onCreate(Bundle savedInstanceState)
.setAction("Action", null).show());

// Create our Python Thread
PythonThread lThread = new PythonThread(this);
lThread.start();
PythonThread lThread = new PythonThread(this) {
public void run() {
this.setName("PythonEngine");
}
};

lThread.start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,12 @@ private int initializePython()
unzipFileFromAssets("Pythonx86_64.zip");
lTempPath += "Pythonx86_64";
}
else if ( Common.is64bitProcessor())
else
{
// Extract our 64bit zip
unzipFileFromAssets("Python64.zip");
lTempPath += "Python64";
}
else
{
unzipFileFromAssets("Python32.zip");
lTempPath += "Python32";
}

// Put the python files where we can execute them
copyPythonFilesFromAssets("main.py");
Expand Down
8 changes: 3 additions & 5 deletions AndroidApp/app/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ set(jni_src_dir ${CMAKE_SOURCE_DIR}/../jni)

if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
include_directories(${jni_src_dir}/Pythonx86_64)
elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
include_directories(${jni_src_dir}/Python32)
else()
include_directories(${jni_src_dir}/Python64)
endif()
Expand All @@ -24,8 +22,8 @@ include_directories(${jni_src_dir}/python
)

# define the 3rd party libraries to include in this project
add_library(python311 SHARED IMPORTED)
set_target_properties(python311 PROPERTIES IMPORTED_LOCATION ${distribution_dir}/${ANDROID_ABI}/libpython3.11.so IMPORTED_NO_SONAME TRUE)
add_library(python312 SHARED IMPORTED)
set_target_properties(python312 PROPERTIES IMPORTED_LOCATION ${distribution_dir}/${ANDROID_ABI}/libpython3.12.so IMPORTED_NO_SONAME TRUE)

# set various C and CXX(++) flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG")
Expand All @@ -45,7 +43,7 @@ SET_TARGET_PROPERTIES (pythonthread PROPERTIES LINKER_LANGUAGE CXX)

# lastly, set to link against the 3rd party libs above
target_link_libraries(pythonthread
python311
python312
log
atomic
android)
6 changes: 3 additions & 3 deletions AndroidApp/app/src/main/jni/Pythonx86_64/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#include "bytearrayobject.h"
#include "bytesobject.h"
#include "unicodeobject.h"
#include "cpython/initconfig.h"
#include "pystate.h"
#include "pyerrors.h"
#include "longobject.h"
#include "cpython/longintrepr.h"
#include "boolobject.h"
Expand All @@ -74,8 +77,6 @@
#include "sliceobject.h"
#include "cpython/cellobject.h"
#include "iterobject.h"
#include "cpython/initconfig.h"
#include "pystate.h"
#include "cpython/genobject.h"
#include "descrobject.h"
#include "genericaliasobject.h"
Expand All @@ -85,7 +86,6 @@
#include "cpython/picklebufobject.h"
#include "cpython/pytime.h"
#include "codecs.h"
#include "pyerrors.h"
#include "pythread.h"
#include "cpython/context.h"
#include "modsupport.h"
Expand Down
42 changes: 34 additions & 8 deletions AndroidApp/app/src/main/jni/Pythonx86_64/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ extern "C" {
Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument
is used to enable certain printing options. The only option currently
supported is Py_Print_RAW.
(What should be said about Py_Print_RAW?). */
supported is Py_PRINT_RAW. By default (flags=0), PyObject_Print() formats
the object by calling PyObject_Repr(). If flags equals to Py_PRINT_RAW, it
formats the object by calling PyObject_Str(). */


/* Implemented elsewhere:
Expand Down Expand Up @@ -88,7 +88,7 @@ extern "C" {
-1 on failure.
This is the equivalent of the Python statement: del o.attr_name. */
#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
#define PyObject_DelAttrString(O, A) PyObject_SetAttrString((O), (A), NULL)


/* Implemented as a macro:
Expand All @@ -98,7 +98,7 @@ extern "C" {
Delete attribute named attr_name, for object o. Returns -1
on failure. This is the equivalent of the Python
statement: del o.attr_name. */
#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
#define PyObject_DelAttr(O, A) PyObject_SetAttr((O), (A), NULL)


/* Implemented elsewhere:
Expand Down Expand Up @@ -228,6 +228,32 @@ PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
PyObject *name,
...);

/* Given a vectorcall nargsf argument, return the actual number of arguments.
* (For use outside the limited API, this is re-defined as a static inline
* function in cpython/abstract.h)
*/
PyAPI_FUNC(Py_ssize_t) PyVectorcall_NARGS(size_t nargsf);

/* Call "callable" (which must support vectorcall) with positional arguments
"tuple" and keyword arguments "dict". "dict" may also be NULL */
PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
#define PY_VECTORCALL_ARGUMENTS_OFFSET \
(_Py_STATIC_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))

/* Perform a PEP 590-style vector call on 'callable' */
PyAPI_FUNC(PyObject *) PyObject_Vectorcall(
PyObject *callable,
PyObject *const *args,
size_t nargsf,
PyObject *kwnames);

/* Call the method 'name' on args[0] with arguments in args[1..nargsf-1]. */
PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
PyObject *name, PyObject *const *args,
size_t nargsf, PyObject *kwnames);
#endif

/* Implemented elsewhere:
Expand Down Expand Up @@ -722,7 +748,7 @@ PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
by PySequence_Fast, and that i is within bounds. */
#define PySequence_Fast_GET_ITEM(o, i)\
(PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
(PyList_Check(o) ? PyList_GET_ITEM((o), (i)) : PyTuple_GET_ITEM((o), (i)))

/* Return a pointer to the underlying item array for
an object returned by PySequence_Fast */
Expand Down Expand Up @@ -802,7 +828,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
failure.
This is equivalent to the Python statement: del o[key]. */
#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
#define PyMapping_DelItemString(O, K) PyObject_DelItemString((O), (K))

/* Implemented as a macro:
Expand All @@ -812,7 +838,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
Returns -1 on failure.
This is equivalent to the Python statement: del o[key]. */
#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
#define PyMapping_DelItem(O, K) PyObject_DelItem((O), (K))

/* On success, return 1 if the mapping object 'o' has the key 'key',
and 0 otherwise.
Expand Down
15 changes: 7 additions & 8 deletions AndroidApp/app/src/main/jni/Pythonx86_64/boolobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ extern "C" {
#endif


PyAPI_DATA(PyTypeObject) PyBool_Type;
// PyBool_Type is declared by object.h

#define PyBool_Check(x) Py_IS_TYPE(x, &PyBool_Type)
#define PyBool_Check(x) Py_IS_TYPE((x), &PyBool_Type)

/* Py_False and Py_True are the only two bools in existence.
Don't forget to apply Py_INCREF() when returning either!!! */
/* Py_False and Py_True are the only two bools in existence. */

/* Don't use these directly */
PyAPI_DATA(PyLongObject) _Py_FalseStruct;
PyAPI_DATA(PyLongObject) _Py_TrueStruct;

/* Use these macros */
#define Py_False ((PyObject *) &_Py_FalseStruct)
#define Py_True ((PyObject *) &_Py_TrueStruct)
#define Py_False _PyObject_CAST(&_Py_FalseStruct)
#define Py_True _PyObject_CAST(&_Py_TrueStruct)

// Test if an object is the True singleton, the same as "x is True" in Python.
PyAPI_FUNC(int) Py_IsTrue(PyObject *x);
Expand All @@ -31,8 +30,8 @@ PyAPI_FUNC(int) Py_IsFalse(PyObject *x);
#define Py_IsFalse(x) Py_Is((x), Py_False)

/* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_NewRef(Py_True)
#define Py_RETURN_FALSE return Py_NewRef(Py_False)
#define Py_RETURN_TRUE return Py_True
#define Py_RETURN_FALSE return Py_False

/* Function to return a bool from a C long */
PyAPI_FUNC(PyObject *) PyBool_FromLong(long);
Expand Down
4 changes: 2 additions & 2 deletions AndroidApp/app/src/main/jni/Pythonx86_64/bytearrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ PyAPI_DATA(PyTypeObject) PyByteArray_Type;
PyAPI_DATA(PyTypeObject) PyByteArrayIter_Type;

/* Type check macros */
#define PyByteArray_Check(self) PyObject_TypeCheck(self, &PyByteArray_Type)
#define PyByteArray_CheckExact(self) Py_IS_TYPE(self, &PyByteArray_Type)
#define PyByteArray_Check(self) PyObject_TypeCheck((self), &PyByteArray_Type)
#define PyByteArray_CheckExact(self) Py_IS_TYPE((self), &PyByteArray_Type)

/* Direct API functions */
PyAPI_FUNC(PyObject *) PyByteArray_FromObject(PyObject *);
Expand Down
2 changes: 1 addition & 1 deletion AndroidApp/app/src/main/jni/Pythonx86_64/bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PyAPI_DATA(PyTypeObject) PyBytesIter_Type;

#define PyBytes_Check(op) \
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
#define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type)
#define PyBytes_CheckExact(op) Py_IS_TYPE((op), &PyBytes_Type)

PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
Expand Down
2 changes: 1 addition & 1 deletion AndroidApp/app/src/main/jni/Pythonx86_64/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(

/* Deprecated since PyEval_CallObjectWithKeywords is deprecated */
#define PyEval_CallObject(callable, arg) \
PyEval_CallObjectWithKeywords(callable, arg, (PyObject *)NULL)
PyEval_CallObjectWithKeywords((callable), (arg), _PyObject_CAST(_Py_NULL))

Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallFunction(
PyObject *callable, const char *format, ...);
Expand Down
3 changes: 0 additions & 3 deletions AndroidApp/app/src/main/jni/Pythonx86_64/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ extern "C" {
#define Py_eval_input 258
#define Py_func_type_input 345

/* This doesn't need to match anything */
#define Py_fstring_input 800

#ifndef Py_LIMITED_API
# define Py_CPYTHON_COMPILE_H
# include "cpython/compile.h"
Expand Down
4 changes: 2 additions & 2 deletions AndroidApp/app/src/main/jni/Pythonx86_64/complexobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extern "C" {

PyAPI_DATA(PyTypeObject) PyComplex_Type;

#define PyComplex_Check(op) PyObject_TypeCheck(op, &PyComplex_Type)
#define PyComplex_CheckExact(op) Py_IS_TYPE(op, &PyComplex_Type)
#define PyComplex_Check(op) PyObject_TypeCheck((op), &PyComplex_Type)
#define PyComplex_CheckExact(op) Py_IS_TYPE((op), &PyComplex_Type)

PyAPI_FUNC(PyObject *) PyComplex_FromDoubles(double real, double imag);

Expand Down
Loading

0 comments on commit ad775ce

Please sign in to comment.