顯示具有 AndroidTool 標籤的文章。 顯示所有文章
顯示具有 AndroidTool 標籤的文章。 顯示所有文章

2013年8月25日 星期日

[Android] Could not open Selected VM debug port (8700). Make sure you do not have another instance of DDMS or of the eclipse plugin running....

Error Message

Could not open Selected VM debug port (8700). Make sure you do not have another instance of DDMS or of the eclipse plugin running. If it's being used by something else, choose a new port number in the preferences.



Solution

Window -> Preferences -> Android -> DDMS

- Set Base local debugger port to "8601"
- Check the box that says "Use ADBHOST" and the value should be "127.0.0.1"


[Android] The connection to adb is down, and a severe error has occured....

Error Message

The connection to adb is down, and a severe error has occured.
You must restart adb and Eclipse.
Please ensure that adb is correctly located at 'D:\sdk\platform-tools\adb.exe' and can be executed.


Solution

找到工作管理員中的 adb.exe 執行中的 process
關閉 adb.exe
重啟 eclipse



* Reference
- Android故障排除_The connection to adb is down, and a severe error has occured.
- Android : The Connection To Adb Is Down, And A Severe Error Has Occurred.

2013年1月31日 星期四

[Android] Errors when sign projecct

Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]

重新 sign 前 沒有刪掉以下檔案 安裝時便會發生此錯誤。

\META-INF\CERT.RSA
\META-INF\CERT.SF


jarsigner: unable to open jar file: test.apk

沒有找到 test.apk。


jarsigner: unable to sign jar: java.util.zip.ZipException: invalid entry compressed size (expected 483 but got 480 bytes)

要 sign 的檔案已經 sign 過。

2012年12月2日 星期日

[Android] Android NDK (Native Development Kit)

Android NDK (Native Development Kit)
能透過 java 直接呼叫 c 程式所做功能的工具。

Android SDK 
是 google 提供 developers 開發的 Java-based API,app 會被執行在 Dalvik virtual machine,類似於 JVM,app 可以在任何裝置上執行,不受硬體 CPU 影響。

這樣 app 的執行速度相對於直接使用 C 來控制慢,尤其是影音方面的運算。所以有了 NDK,透過 NDK 可以直接操作底層功能加快執行速度。

因此形成主程式使用 Java,有速度上要求部份的如驅動程式、繪圖功能則使用 C;Android system 可分為 Java - JNI - C,透過 JNI(java native interface) 間接 Java 與 C


* Reference
- 49歐姆 整機教學: android HAL 層與 android NDK
- 49歐姆 整機教學: android HAL 硬體抽象層
- 49歐姆 整機教學
- What is the NDK?

[AndroidTool] Could not open: C:\Users – Android could not start Emulator

Error Message

PANIC: Could not open: C:\Users – Android could not start Emulator


Solution

因為 emulator 預設被建立在 C:\Users\.....\.android/avd

所以若該路徑下找不到會無法啟動



* Reference
- PANIC: Could not open: C:Users – Android could not start Emulator | Share ChiWai/Share 智慧/智慧分享 – 技術分享/Tech Blog
- 李辉的博客 » android模拟器无法启动的解决

2012年12月1日 星期六

2012年10月9日 星期二

[AndroidTool] HierarachyViewer unable to debug device

Error Message

Unable to get view server protocol version from device
Unable to debug device

Caused by:

因為大部份手機不允許使用 HierarachyViewer,以保護手機上 app 的安全,因此必須在開發機或 emulators 上使用。


* Reference
- android - Why is hierarchyviewer not working for Samsung Galaxy TAB 7.0? - Stack Overflow

2012年4月15日 星期日

[Tool] Memory Analyzer Tool (MAT)

雖然現在手機在硬體上的限制已不如從前,效能也大大提升,但手機中的空間還是不比在電腦上,所以如果不需過多的 effort,盡量降低空間的使用仍是必要的,使用者很容易就安裝過多的 App 或是同時操作多個 App,那麼一個 App 能降低一點使用量,我認為全部加起來多少會有點影響,但並不說要在一開始開發時便以效能為一取向,這樣反而有點綁手綁腳,本末倒置了,除非效能是需求之一。

尤其在某些 UI 的操作上,若系統進行 Garbage Collection,則會讓 User 在操作時感覺到頓頓的,另外,圖片上的使用,很容易使得 App Out Of Memory(OOM)(每個 App 被分配的空間是 16M),因此可利用 Eclipse plugin - MAT 來找出在 App 中是那裡使用了大量空間,而且沒有做適當的釋出。

Install
  • Install MAT Plugin on Eclipse - Install New Software
    • http://download.eclipse.org/mat/1.1/update-site/
  • 開啟關閉 App 約五次,來觀察是否有正確的建立與回收物件
  • Dump HPROF file by DDMS
  • Convert HPROF format by Android Tool, hprof-conv
  • Open eclipse perspective, Memory Analyer and File --> Open Heap Dump...

  • Shallow Heap
    • 物件本身的記憶體使用量。
  • Retained Heap
    • 指物件本身 + 參照過來的物件們所使用的記憶體使用量。

如何妥善的使用 bitmap
Displaying Bitmaps Efficiently


* Reference
- 使用MAT(記憶體分析)工具查看Memory Leak ***
- HOWTO: Debug Memory Leak in Android
- Download MAT
- Android Memory Usage
- Avoiding memory leaks - Romain Guy ***
- Attacking memory problems on Android ***

2011年11月6日 星期日

[Android] Unexpected error while launching logcat

Error Message


Unexpected error while launching logcat. Try reselecting the device.]EOF
Failed to install test.apk on device 'emulator-5554': EOF


log is too large to explode logcat buffer?


[Android] Android Developer Tool (ADT) - Resources compilation

[Definition]
  • A plugin for Eclipse that provides a suite of tools that are integrated with the Eclipse IDE.
  • It can integrated Android project creation, building, packaging, installation, and debugging.
    • Add components based on the Android Framework API.
      • Java programming language and XML editors
      • Create an application UI.
    • SDK Tools integration
      • Debug your applications using the Android SDK tools.
    • Export signed (or unsigned) .apk files in order to distribute your application.

[ADT 14.0.0 (October 2011)]
  • Added webcam support to Android 4.0 or later platforms to emulate rear-facing cameras
  • Resource compilation
    • Changed how library projects are built in Eclipse.
      • You need SDK Tools r14 or newer to use the new library project feature that generates each library project into its own JAR file.
    • Improved incremental builds so that resource compilation runs less frequently.
    • Builds no longer run when you edit strings or layouts (unless you add a new id)
    • No longer run once for each library project.
    • No longer happens for normal save operations. It only happens when running or debugging (the build option that lets you disable the packaging step, which was introduced in ADT 12, is now on by default.)
  • Added the "Go to Matching" (Ctrl-Shift-P) feature, which lets you jump between opening and closing tags in XML files.

[Library project]
  • Source code in the library project can access its own resources through its R class.
  • Can reference other library projects and can import an external library (JAR) in the normal way.
  • Differs from an standard Android application project:
    • Cannot compile it directly to its own .apk and run it on an Android device.
    • Can not include raw assets.
    • Cannot export the library project to a self-contained JAR file, as you would do for a true library. (Changed in r14?)
      • Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.
Usage
  • When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. 
    • ADT creates virtual source folders linking to the library source folders.
    • Because library dependencies are controlled by the content of project.properties (formerly default.properties), ADT must dynamically add and remove these virtual source folders whenever a project is opened and closed.
    • Source-based library prevents distribution of re-usable components(?) unless one is willing to include the source code.
  • In cases...
    • A resource ID is defined in both the application and the library
      • The tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. (or the library with highest priority, and discard the other resource?)
      • This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.
      • Be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.
    • When two libraries referenced from an application define the same resource ID
      • The tools select the resource from the library with higher priority and discard the other.
  • At build time, the libraries are merged with the application one at a time, starting from the lowest priority to the highest.

Previously (< r14)
Library projects were handled as extra resource and source code folders to be used when compiling the resources and the application’s source respectively. While this worked fine for most cases, there were two issues.
  1. Developers asked us for the ability to distribute a library as a single jar file that included both compiled code and resources. The nature of Android resources, with their compiled IDs prevented this.
  2. The implementation of the library projects was extremely fragile in Eclipse. Adding extra source folders outside of the project folders is non-trivial when it needs to be handled automatically, in a way that doesn’t expose a user’s local installation path (which is required for people working in teams through a source control system such as SVN or git).
To fix all of these issues, we have decided to move to library projects generating a jar file that is directly used by other projects.

The first impact of this change
  • Had to change the way resource IDs are generated.
  • The main projects and all required libraries
    • The resource IDs will still be generated as final static int in the final R class generated in the main project with the resources.
  • The new library project
    • The resource IDs generated by libraries to be non final.
  • Note
    • In a regular projects
      • Resource IDs are generated as final static int. 
      • These constants then get inlined into the classes that use them. 
      • This means the compiled classes do not access the values stored in, say, R.layout.myui, but instead directly embed the value 0x000042.
    • To make the compiled classes not embed this value and instead access it from the R classes, library project will generate the resources IDs as static int only.
    • This prevents the Java compiler from inlining the values in the library code
  • Therefore prevents usage of the switch statement in the library code.
    • Eclipse provides a refactoring action to convert from switch statements to if/else.

Summary
Library projects containing the jar file directly instead of the source code.(Support in r15)

Related
[Android] export android project 為 jar 檔


By the way
  • /res/xml
    • 存儲xml格式的文件。
    • 會被編譯成二進制格式放到最終的apk裡。
    • 可以通過R類來訪問這裡的文件,並且解析裡面的內容。
  • /res/raw
    • 這裡的文件會原封不動的存儲到設備上。
    • 不會被編譯為二進制形式,訪問的方式也是通過R類。
  • /assets
    • 不會被編譯成二進制形式之外。
    • 訪問方式是通過文件名,而不是資源ID。
    • 可以在這裡任意的建立子目錄,而/res目錄中的資源文件是不能自行建立子目錄的。

Pending Study Issues
  • The detail about eclipse export java/android jar.


* Reference
- Android Application, Android Libraries and Jar Libraries **** Although it describes the structure before r14 but it is still very worth to read.
- ADT Plugin for Eclipse | Android Developers
- Android Developer Tools
- Build changes in revision 14 - Android Tools Project Site ***
- SDK Tools
- Library Projects
Android Developers Blog: Changes to Library Projects in Android SDK Tools, r14 ***
Managing Projects | Android Developers **
- Building and Running *
- Developing In Eclipse, with ADT
- I am Happy Android,3C: android /res/xml /res/raw /assets

2011年10月30日 星期日

[Android] Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder

Error Message

Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder

發生在當想要啟動 android 4.0 的 AVD


Solution

Android 4.0 必須要安裝 ARM EABI v7a System Image 才能啟動



* Reference
- Android 4.0:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder. - 懒人工作室 - 博客频道 - CSDN.NET
- How to create a AVD for android 4.0 - Stack Overflow

[Android] Application does not specify an API level requirement

Error Message

WARNING: Application does not specify an API level requirement!

會發生在 Run Android Application on eclipse
是否在 project 中少指定了什麼?

[Android] conversion to dalvik format to error 1

Error Message

conversion to dalvik format to error 1

Occurred when export apk file


Solution

proguard.bat

android 提供 proguard.bat 這個工具 讓你在 export apk 時 能對你的 code 做混淆的步驟

但在 SDK Tool, r12 會出現此問題,在 r11 還並不會有這問題。

在推出 android 4.0 後的 r14 仍然沒有修復此問題。

解決此問題的方法是修改以下這個檔案:

(#Android SDK Path)\tools\proguard\bin\proguard.bat, 將

call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %*

改為

call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %1 %2 %3 %4 %5 %6 %7 %8 %9

import jar

import JAR 檔時 有可能 reference 了兩次 JAR 檔

可確認 Java Build Path > LibrariesAndroid > Library


* Reference
- 在android中使用proguard混淆代码出现“Conversion to Dalvik format failed with error 1”错误的解决方法 - Naivesoft - 博客频道 - CSDN.NET
android - "Conversion to Dalvik format failed with error 1" on external Jar - Stack Overflow

2011年9月28日 星期三

[AndroidSign] apk sign

* Restriction
  • Android 系統禁止更新安裝簽名不一致的 APK。
  • You cannot release your application to the public when signed with the debug certificate.


* Generate apk sign
  • 要簽名一個沒有簽名過的APK,可以使用一個叫作 Auto-sign 的工具。
    • Auto-sign工具實際執行的是一個叫做Sign.bat的批處理命令。
    • 用文本編輯器開啟這個批處理文件。
  • Obtain a suitable private key


* signapk.jar
  • 是 Android 源碼包中的一個簽名工具。路徑為/build/tools/signapk/SignApk.java
  • 對比一個沒有簽名的 APK 和一個簽名好的 APK,會發現,簽名好的 APK 包中多了一個叫做 META-INF 的文件夾。裡面有三個文件:
    • MANIFEST.MF
    • CERT.SF
    • CERT.RSA。
  • signapk.jar 就是生成了這幾個文件(其他文件沒有任何改變。因此我們可以很容易去掉原有簽名訊息)。


* signapk.jar process:

1、生成 MANIFEST.MF 文件
  • 程式遍歷 apk 包中的所有文件(entry),對非文件夾非簽名文件的文件,逐個生成SHA1 的數字簽名訊息,再用 Base64 進行編碼。
  • 之後將生成的簽名寫入 MANIFEST.MF 文件。
  • SHA1 數字簽名
    • 是一種安全 hash 算法,類似於 MD5 算法。它把任意長度的輸入,通過散列算法變成固定長度的輸出(這裡我們稱作「摘要訊息」)。
    • 你不能僅通過這 個摘要訊息復原原來的訊息。另外,它保證不同訊息的摘要訊息彼此不同。
    • 因此,如果你改變了 apk 包中的文件,那麼在 apk 安裝校驗時,改變後的文件摘要信息與 MANIFEST.MF 的檢驗訊息不同,於是程式就不能成功安裝。

2、生成 CERT.SF 文件
  • 對前一步生成的 Manifest,使用 SHA1-RSA 算法,用私鑰進行簽名
  • RSA 是一種非對稱加密算法。
    • 用私鑰通過RSA算法對摘要訊息進行加密。在安裝時只能使用公鑰才能解密它。
    • 解密之後,將它與未加密的摘要訊息進行對比,如果相符,則表明內容沒有被異常修改

3、生成 CERT.RSA 文件
  • CERT.RSA文件中保存了公鑰、所採用的加密算法等訊息。


* Overview
1、 Android 簽名機制其實是對 APK 包完整性和發佈機構唯一性的一種校驗機制。
2、 Android 簽名機制不能阻止 APK 包被修改,但修改後的再簽名無法與原先的簽名保持一致。(擁有私鑰的情況除外)。
3、 APK 包加密的公鑰就打包在 APK 包內,且不同的私鑰對應不同的公鑰。換句話言之,不同的私鑰簽名的APK公鑰也必不相同。所以我們可以根據公鑰的對比,來判斷私鑰是否一致。


* APK Parser
  • 源碼中有一個隱藏的類用於APK 包的解析。這個類叫 PackageParser
  • 路徑為 frameworks\base\core\java\android\content\pm\PackageParser.java
  • 當我們需要獲取APK包的相關訊息時,可以直接使用這個 class。

我們就可以通過 packageInfo.signatures 來訪問到APK的簽名訊息。

它們的關係如下面代碼所示:

也就是說 signature = new Signature(certificate.getEncoded());

certificate 證書中包含了公鑰和證書的其他基本訊息。

公鑰不同,證書肯定互不相同。

我們可以通過 certificate 的 getPublicKey 方法獲取公鑰訊息。

所以比對簽名證書本質上就是比對公鑰訊息



Related
[Android] Debug Key


* Reference
- Android APK簽名對比及說明 - Android 軟體設計 - Android 台灣中文網 - Android(安卓,安致)討論區 - APK.TW **
- Signing Your Applications

2011年9月12日 星期一

[Android] export android project 為 jar 檔

* steps:
  1. Remove mainfest.xml and res/*.
  2. eclipse -> export -> JAVA -> JAR file
  3. "Select the resources to export:" 下的項目皆不選 (ex: .classpath, .project, AndroidManifest.xml, default.properties, res, assets)
  4. "Next"
  5. "Finish"
---
Update on 20111106

* Cannot include AndroidManifest.xml or you will get this exception.

Error generating final archive: Found duplicate file for APK: AndroidManifest.xml
Origin 1: (#project path)\bin\resources.ap_
Origin 2: (#store JAR path)\Test.jar


* Or include the duplicate res will get this exception.

include res may duplicate with user


Error generating final archive: Found duplicate file for APK: res/layout/main.xml
Origin 1: (#project path)\bin\resources.ap_
Origin 2: (#store JAR path)\Test.jar


* Test including strings.xml

  • If include strings.xml in Jar
    • It won't be compiler and keep in Main Project apk.
    • Main Project cannot find the string resource id that belong to Jar or get the string in Main Project that resource id is equal to.
    • If try to access it will get exception:
      • Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f040002
  • If using open project to be library
    • strings.xml will compiler with Main Project and export.
    • So could get the string in library by JAR's namespace.



* Reference
- Android如何将程序打成jar包 | 第三极 | 移动开发者
- web_surf - ChinaUnix博客 - IT人与你分享快乐生活
- android中生成和使用jar 分享 - 黑暗任务 - ITeye技术网站 **

2011年8月6日 星期六

[Android] Debug Key

* Why needs this key?
  1. 驗證 app。由於 app 可能被他人利用相同的 package name 來以不同的 apk 做替換,因此以 signature 來驗證開發者的身份,限制每個 apk 都必須進行 signature 才能安裝到 android system。
  2. 確保 apk 內每個檔案不被替換,signature 會對每個檔案進行處理,因此若更改 apk 其中的檔案在進行安裝,會得到 invalid apk 的訊息而安裝失敗。

* Signature types
  • default debug.keystore:
    • 建立 Android 專案之後才會產生的檔案。
    • For development phase.
    • Each default keystore only valid for a year.
  • private keystore: used when publish apk.


* If have the error message likes:

Error generating final archive: Debug certificate expired on ****

Solution:
Delete your debug keystore, default path is /user/.android/debug.keystore on Windows.
And then Android build tools 會重新產生 debug certificate.


* Reference
- (转)解决Debug certificate expired的问题
- Android 的 Debug certificate (debug.keystore)
- android apk签名(为什么 如何做 验证)
- 重新签名Android pre-install APK

2011年7月23日 星期六

[Resource] Draw 9-patch

* android SDK 中所提供的工具,位於 (SDK path)\tools\draw9patch.bat

* 讓圖片可以放大(變長或變寬)並且不會失真。

* 工具中:
圖片旁邊會有 1 px 的空白,在想放大的邊點選這 1 px 的空白。

Zoom:只是單純讓你放大圖片,比較好操作,不會對最終檔案有影響!
Patch scale:只是讓你看圖片最終變長、變寬以後的樣子。在你儲存前都可以修改!

做法可參考Draw 9-patch

* 調整好的檔案 檔名應儲存為 fileName.9.png

* 在 SDK 2.3 版使用此工具時會出現 NoClassDefFoundError,請見[Android]draw9patch 的 NoClassDefFoundError 解決方法


* Reference
- Draw 9-patch
- NinePatch
- Draw 9-patch **
- [Android]draw9patch 的 NoClassDefFoundError 解決方法
- Draw 9-patch Tutorial **
- android 工具 Draw 9-patch 详解 **
- Android学习系列(4)--App自适应draw9patch不失真背景 ***
- android draw 9-path使用总结
- android 9patch ****