我通过以下配置将python对应配置到Android studio中,并通过打包,但是无法启动django,麻烦有经验的大佬帮忙看看,谢谢。
以下是myapplication.java:
package com.example.rs232;
import android.app.Application;
import android.util.Log;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import io.dcloud.application.DCloudApplication;
public class MyApplication extends DCloudApplication {
private static Application myApplication;
@Override
public void onCreate() {
super.onCreate();
myApplication = this;
Log.d("APPTag","myapplication is starting");
// 初始化 Python 环境
if (!Python.isStarted()) {
Log.d("pythonTAG","python is starting");
// 启动 Django 服务
Python.start(new AndroidPlatform(this)); // 初始化 Chaquopy
startDjangoServer();
}
}
public static Application getInstance() {
return myApplication;
}
// 启动 Django 服务
private void startDjangoServer() {
try {
Log.d("djangoTAG","django is starting");
Python py = Python.getInstance();
PyObject djangoScript = py.getModule("rs232con.start_django"); // 对应 run_django.py 文件
djangoScript.callAttr("run_django");
} catch (Exception e) {
Log.d("djangoErrorTAG","django is error");
Log.d("djangoErrormsg",e.toString());
e.printStackTrace(); // 打印错误日志
}
}
}
以下是Android studio :
apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'
android {
compileSdkVersion 30
buildToolsVersion '30.0.3'
defaultConfig {
applicationId "com.example.rs232"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
python {
// 指定python路径 注意更换为自己的Python路径!!
buildPython "D:\\Program Files\\Python38\\python.exe"
//安装第三方库
pip{
install "django==4.2.15"
install "pyserial"
}
}
sourceSets{
main{
// python.resources 'src/main/python'
// setRoot "src/main"
assets.srcDirs = ["src/main/assets/rs232con"]
// python.include "src/main/python"
}
}
// source{
// main{
// // 设置源代码根目录为 python 文件夹
//// setRoot 'src/main' // 如果 python 文件夹在 src/main 下
//// srcDirs = ['']
// python.srcDirs 'src/main/python' // 指定 python 文件夹路径
// }
// }
}
// 自定义打包名称
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "rs232con.apk"
}
}
signingConfigs {
config {
keyAlias 'key0'
keyPassword '123456'
storeFile file('test.jks')
storePassword '123456'
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions {
additionalParameters '--auto-add-overlay'
ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.core:core:1.1.0'
implementation "androidx.fragment:fragment:1.1.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.facebook.fresco:fresco:2.5.0'
implementation "com.facebook.fresco:animated-gif:2.5.0"
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.alibaba:fastjson:1.2.83'
implementation 'androidx.webkit:webkit:1.3.0'
}
但是打包后的apk我解压后还是无法找到对应的打包django项目文件,不知道是不是哪里配置不对?
7665

被折叠的 条评论
为什么被折叠?



