問題描述
我能夠構(gòu)建一個(gè) .apk,但是在我將它安裝到我的 android 手機(jī)上之后,它只是在啟動時(shí)崩潰了.我失敗的想法是我正在使用 3rd 方庫,例如(beautifulsoup).
I am able to build an .apk, but after I install it on my android phone it simply crashes at startup. My thoughts for failing is that I am using 3rd party libraries e.g(beautifulsoup).
這就是我的導(dǎo)入在 main.py 中的樣子:
This is how my imports look in main.py:
from kivy.app import App
from kivy.properties import ListProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import time, os, random, urllib2, re, cookielib as cj
from bs4 import BeautifulSoup as bs
from functools import partial
我在跑小牛10.9.3
I'm running mavericks 10.9.3
它與 buildozer.spec 文件有關(guān)嗎?我嘗試將 BeautifulSoup 添加到應(yīng)用需求中,但這并沒有改變?nèi)魏问虑?
Does it have something to do with buildozer.spec file? I've tried adding BeautifulSoup to app requirements, but it doesn't change a thing.
任何幫助將不勝感激.
推薦答案
我也遇到了這個(gè)問題,但我(顯然)能夠通過解決方法讓一切正常工作.由于看起來您發(fā)布的不是 logcat,因此我假設(shè)您遇到了與我相同的問題.
I ran into this problem as well, but I was (apparently) able to get everything working fine with a workaround. Since it doesn't look like you posted a logcat, I'll assume you ran into the same issue I did.
是的,您確實(shí)需要在規(guī)范中將 beautifulsoup4 列為一項(xiàng)要求.從查看 bs4 的代碼來看,bs4 似乎愿意使用幾個(gè)構(gòu)建器"中的任何一個(gè).它支持 HTMLParser、html5lib 或 lxml.我不知道為什么我們不能加載 HTMLParser,但它實(shí)際上是三個(gè)庫中 最不喜歡的 庫,如果不是因?yàn)閷?dǎo)入周圍沒有 try 塊,它似乎一切都會正常工作(只要其他解析庫之一可用).
Yes, you do need to list beautifulsoup4 as a requirement in your spec. From looking into bs4's code, it looks like bs4 is willing to use any of several "builders." It supports HTMLParser, html5lib, or lxml. I have no idea why we can't load HTMLParser, but it's actually the least preferred library of the three, and if it weren't for the fact that there's no try block around the import, it seems that everything would work fine (as long as one of the other parsing libraries was available).
考慮到這一點(diǎn),我包含了其他庫之一,并決定破解導(dǎo)入過程,以便 Python 假裝 _htmlparser 加載正常 :)
With this in mind, I included one of the other libraries, and I decided to hack the import process so that Python would pretend _htmlparser loaded okay :)
這篇文章很有啟發(fā)性:http://xion.org.pl/2012/05/06/hacking-python-imports/
最終的結(jié)果是這樣的:
import imp
import sys
class ImportBlocker(object):
def __init__(self, *args):
self.black_list = args
def find_module(self, name, path=None):
if name in self.black_list:
return self
return None
def load_module(self, name):
module = imp.new_module(name)
module.__all__ = [] # Necessary because of how bs4 inspects the module
return module
sys.meta_path = [ImportBlocker('bs4.builder._htmlparser')]
from bs4 import BeautifulSoup
我還在 buildozer.spec 的要求中添加了 html5lib.
I also added html5lib to the requirements in buildozer.spec.
現(xiàn)在,這是解決問題的正確方法嗎?我不知道.最好的方法可能是要求作者修復(fù)它.可能就像將導(dǎo)入放在 try 塊中一樣簡單.盡管如此,這是我目前采用的方法,它至少是一個(gè)有趣的練習(xí),也是一種測試你的應(yīng)用直到出現(xiàn)更好的修復(fù)的方法.
Now, is this the right way to solve the problem? I don't know. The best approach would probably be to request that the author fix it. It might be as simple as to put the import in a try block. Nevertheless, this is the approach I've gone with for the moment, and it is at least an interesting exercise, and a way to test your app until a better fix comes along.
另外,我應(yīng)該警告您,我最近才這樣做,所以我不能 100% 保證它不會造成任何問題,但至少,它運(yùn)行良好,足以讓我的應(yīng)用程序運(yùn)行并抓取我感興趣的特定網(wǎng)站.祝你好運(yùn)!
Additionally, I should warn you that I only did this recently, so I can't 100% guarantee that it won't cause any problems, but at a minimum, it's worked well enough to get my app running and scraping the particular website I was interested in. Good luck!
這篇關(guān)于Buildozer 編譯 apk,但它在 android 上崩潰的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!