上周項(xiàng)目有需要,集成了“騰訊X5瀏覽器內(nèi)核”過(guò)程中,也遇到了一些問(wèn)題。經(jīng)過(guò)摸索,也順帶補(bǔ)充解決了之前ABI方面的理解。
一、需求描述?
APP,內(nèi)容模塊視頻部分,使用時(shí)夏正流行H5技術(shù)。
二、初步技術(shù)方案
1、打開(kāi)網(wǎng)頁(yè),用WebView控件。
2、初步技術(shù)方案遇到的問(wèn)題
Html5的Video控件播放視頻,在Android平臺(tái)的WebView中播放的效果,和IOS播放效果有差異。IOS點(diǎn)擊視頻部分,會(huì)用系統(tǒng)自帶的瀏覽器全屏播放視頻,體驗(yàn)效果佳;而Android的WebView無(wú)法全屏。至少體驗(yàn)效果比IOS上的差一些。
3、技術(shù)嘗試
①Html5頁(yè)面使用一些開(kāi)源封裝過(guò)的Video,帶全屏等。
結(jié)果:產(chǎn)品部,認(rèn)為體驗(yàn)不佳。提出疑問(wèn):“為什么在QQ上播放那么好呢?”,這,畢竟我們是技術(shù)方, 也不好直接說(shuō)“別人技術(shù)牛”。
②重寫(xiě)WebChromeClient的onShowCustomView開(kāi)啟全屏;onHideCustomView退出全屏。
結(jié)果:相信有朋友也折騰過(guò)這玩意,在Android 4.4開(kāi)始的手機(jī),大部分不會(huì)進(jìn)入該回調(diào)方法。
③技術(shù)體驗(yàn)上,不懂技術(shù)的同事等, 都是用QQ,微信舉例。那就使用騰訊X5內(nèi)核吧。
三、騰訊X5內(nèi)核接入
1、閱讀其文檔,知曉:
①成功調(diào)用x5內(nèi)核條件是安裝騰訊三個(gè)常見(jiàn)產(chǎn)品之一:1、手機(jī)QQ;2、微信;3、QQ瀏覽器。當(dāng)然版本也有限制,到這里,我們至少知道,三者中一個(gè),是很有可能成功的,只要版本不太低,至少身邊朋友的版本,都不至于太低了。
②第一次安裝集成該SDK的版本必須預(yù)熱(從SDK接入文檔中理解,理論上是, 大部分情況第一次是啟動(dòng)失敗的,從試驗(yàn)中,是kill了進(jìn)程,再開(kāi)啟才成功)。
③ABI只提供armeabi的.so
2、搬磚小分隊(duì),施工
①導(dǎo)入相應(yīng)的.jar, ?.so 文件:
libs: tbs_sdk_v1.5.1.1057_25436_obfs_20160331_144900.jar
armeabi:liblbs.so
②預(yù)熱X5內(nèi)核:
/**
* 開(kāi)啟額外進(jìn)程 服務(wù) 預(yù)加載X5內(nèi)核, 此操作必須在主進(jìn)程調(diào)起X5內(nèi)核前進(jìn)行,否則將不會(huì)實(shí)現(xiàn)預(yù)加載
*/
private voidpreinitX5WithService() {
Intent intent =newIntent(this,FirstLoadingX5Service.class);
startService(intent);
}
/**
* X5內(nèi)核在使用preinit接口之后,對(duì)于首次安裝首次加載沒(méi)有效果
* 實(shí)際上,X5webview的preinit接口只是降低了webview的冷啟動(dòng)時(shí)間;
* 因此,現(xiàn)階段要想做到首次安裝首次加載X5內(nèi)核,必須要讓X5內(nèi)核提前獲取到內(nèi)核的加載條件
*/
private voidpreinitX5WebCore() {
if(!QbSdk.isTbsCoreInited()) {
// preinit只需要調(diào)用一次,如果已經(jīng)完成了初始化,那么就直接構(gòu)造view
QbSdk.preInit(MainActivity.this, null);// 設(shè)置X5初始化完成的回調(diào)接口
}
}
③用多臺(tái)手機(jī)測(cè)試:華為4A, 華為榮耀(忘記什么型號(hào),是64位CPU),google nenux4, 小米4C, 華為mate 7, 紅米Note2 等等。APP打包測(cè)試。
結(jié)果:只有我的華為4A能播放。 為什么別的,就不正常呢?
------------------------------以下-解決篇--------------------------------
四、ABI知識(shí)理解得一知半解的時(shí)候, 有如下疑問(wèn):
一、只有armeabi的.so是否別的平臺(tái)遇到問(wèn)題?為什么只有華為4A可以呢?
①CPU方面,華為4A是比較老的CPU, 估計(jì)就是armeabi的了,由于別的機(jī)型,我都有對(duì)應(yīng)的abi目錄,都各自找到相應(yīng)的平臺(tái)目錄, 所以無(wú)法加載“l(fā)iblbs.so”。
②嘗試將“l(fā)iblbs.so”放在各個(gè)abi目錄中, 結(jié)果還是沒(méi)辦法啟動(dòng)x5。
③通過(guò)百度,google等搜索,再次進(jìn)行ABI方面的理解加深,獲取解決方案:
項(xiàng)目的“build.gradle”文件defaultConfig,增加配置
ndk {
abiFilters"armeabi","armeabi-v7a","x86","mips"
}
結(jié)果:確實(shí)解決了問(wèn)題。
二、加上那玩意,為什么就解決問(wèn)題呢?請(qǐng)?jiān)徫褹BI知識(shí)弱, 在進(jìn)一步學(xué)習(xí)之前,我有以下疑問(wèn):?加上那段配置, 任何平臺(tái)都找到了armeabi目錄下的“l(fā)iblbs.so”。是什么機(jī)制?請(qǐng)讓我假象,一下:
①難道是別的平臺(tái)都指向了最低兼容的armeabi目錄? 噢,如果這樣做的話,在APP中性能會(huì)有極大的損失。如arm-v7中的 ?浮點(diǎn)運(yùn)算,這就損失極大。更何況64位的CPU。
②難道是機(jī)器是智能化了?先找相應(yīng)平臺(tái)的.so, 不行,再逐個(gè)查看向下的兼容平臺(tái)?如果是這樣,那就太好了。
③什么優(yōu)先順序呢?這個(gè)Android選取ABI的機(jī)制?我也想了解。 順帶這個(gè)問(wèn)題一起學(xué)習(xí)。
三、解決了X5問(wèn)題,知道了配置一下ABI兼容。但是還是得知其然知其所以然, 如何證實(shí)以上猜想?得找個(gè)小文章證實(shí)一下
①google一些資料,在overflow上,找到挺好的http://stackoverflow.com/questions/20674650/how-to-configure-ndk-with-android-gradle-plugin-0-7/21413011#21413011。
鬼老很務(wù)實(shí), 他也有這種的疑問(wèn),從假象的提出,到論證,到結(jié)論。佩服。
文中的一些片段,我摘取一下:
To simply Link in Prebuilt Native Libraries, just add an ndk section to your task. For instance, I added it below in productFlavors. The abiFilter is the folder name the libs are stored in. abiFilters means both libs from the comma separated list will be added to your final APK (so you could theoretically have "armeabi", "armeabi-v7a", "x86", and "mips" all in one APK, and the O/S would choose the supported architecture lib on install):
Very helpful. Thanks indeed. BTW, do you know the default search directory for user static libraries? I opt not to use the trick of "-I" in ldLibs, if possible. – weidongxu Apr 16 '14 at 20:10
how does one explore/download the files listed here?: docs.google.com/… – Cliff Apr 26 '14 at 17:47
Found the example download but now I'm facing another issue w/ Gradle 1.11. The generated Androud.mk file uses absolute paths: stackoverflow.com/questions/23344567/… – Cliff Apr 28 '14 at 16:09
Thanks for the ldlibs tricking - I just found out that ldlibs are not kept in the order you type them in, which makes using multiple static libraries just about completely unusable. Unless they're "one" argument, after which it works. Awesome! – dascandy Jan 1 '15 at 22:15
2
This is the most detailed step-by-step introduction I've ever seen for ndk setup. Google should have hired you to rewrite all their documentations. – John Jul 15 '15 at 16:53
Thanks @John, nice of you to say. The closest interview to google I had was at Microsoft, but they told me I wasn't "the right fit for Microsoft" when I couldn't write a bubble sort algorithm on the board (who the frick would want to memorize that...). Actually, that was just 1 of 3 interviews, the other 2 offered me jobs, but I declined and started my own business. :) – reactive-core Oct 14 '15 at 18:49
@reactive-core, No wonder why Windows so fxxx up, they judge programmer by bubble sort algorithm! :) – John Oct 15 '15 at 7:17
add a comment
up vote
26
down vote
accepted
Found the answer. Including ndk.dir=path/to/ndk in the local.properties file did the trick.
Update: On the latest versions of Android Studio, you can set the value directly in the Project Structure > SDK location.
shareimprove this answer
edited Oct 8 '15 at 10:25
answered Dec 19 '13 at 7:13
user1906
1,1311920
Don't forget to accept your answer – orip Dec 23 '13 at 9:54
3
Is it a good idea to put something manually into the local.properties file, since it says "This file is automatically generated by Android Studio. Do not modify this file -- YOUR CHANGES WILL BE ERASED!" ?
從這對(duì)話中, 至少至少,都是能證明挺有料子的人。
OK,接下來(lái),我也實(shí)驗(yàn)。
②項(xiàng)目中,有用到,信鴿,地圖等一系列的.so文件。
我把a(bǔ)rmeabi的目錄,只剩下騰訊x5內(nèi)核的"liblbs.so"文件。用除了我手里的華為4A的,設(shè)備來(lái)安裝apk, 非armeabi基礎(chǔ)平臺(tái)的設(shè)備,都能順利加載x5內(nèi)核。這是其次,最重要的是 armeabi目錄刪除的所有.so包的,功能存在。
結(jié)論:手機(jī)讀取了對(duì)應(yīng)手機(jī)平臺(tái)的.so, 找不到"liblbs.so"的時(shí)候, 才去armeabi目錄,找它。這樣就很完美。符合提前提出的 疑問(wèn)②。
五、SDK預(yù)熱問(wèn)題
一、能用,體驗(yàn)方面,還是跑不掉的。遇到如下問(wèn)題:
①集成X5的APP, 第一次安裝,多數(shù)手機(jī)是加載X5內(nèi)核失敗,取到sys core。
②back back 關(guān)閉應(yīng)用,再打開(kāi)仍然失敗。要按 任務(wù),“劃掉“這個(gè)進(jìn)程任務(wù)才成功。哎,這點(diǎn),騰訊又不說(shuō),怎么做了。
二、如何提高體驗(yàn)
①啟動(dòng) TBSDemo,等待幾秒鐘后看到提示框“x5內(nèi)核安裝成功,是否重啟”,此時(shí)點(diǎn)擊“重啟”. 這句話是引用騰訊X5內(nèi)核SDK接入文檔(http://x5.tencent.com/doc?id=1003)
②重啟體驗(yàn)不好吧,最后使用的方案是在 "關(guān)閉APP首頁(yè)",時(shí)候徹底關(guān)閉這個(gè)進(jìn)程。
@Override
public voidonBackPressed() {
//是否新裝應(yīng)用、或者剛更新到本次版本的應(yīng)用
booleanisFist4Video = SharePreferenceUtil.getBooleanDataByKey(this,"isFist4Video", true);
if(isFist4Video){
SharePreferenceUtil.saveBooleanDataToSharePreference(this,"isFist4Video", false);
android.os.Process.killProcess(android.os.Process.myPid());
super.onBackPressed();
}
super.onBackPressed();?
}
③上述雖然解決了,第一次安裝,不用關(guān)閉進(jìn)程,而讓用戶back首頁(yè)關(guān)閉應(yīng)用而殺進(jìn)程。然而體驗(yàn)并不是非常好。 (這玩意測(cè)試過(guò),對(duì)是否有問(wèn)題,沒(méi)有影響。只是第二次就好)
三、X5案例的APP
合作伙伴
微信,手機(jī)QQ,QQ空間,京東58,同城,搜狐視頻,新浪新聞
這些”合作伙伴“,別人都是安裝完,就正常使用的?如何做到呢?朋友們,一起討論哦。