Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Thursday, September 25, 2014

AOSP Keys convert to Eclipse KeyStore

 AOSP put keys in build/target/product/security 

testkey -- a generic key for packages that do not otherwise specify a key.
platform -- a test key for packages that are part of the core platform.
shared -- a test key for things that are shared in the home/contacts process.

media -- a test key for packages that are part of the media/download system.

Example of "Platform"
javakeystore(jks)算是pkcs12的部分
但由於jkssunproprietaryformat
所以openssl沒法處理jks 要先把他轉成pkcs12再用keytool來轉

首先要先把 private key (pkcs8) DER format轉成PEM format
#openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out platform.pem


然後要把private key public key 轉成pkcs12. 輸入之後會跳出提示"輸入密碼"
#openssl pkcs12 -export -in platform.x509.pem -inkey platform.pem -out platform.pkcs12


最後用keytool把他轉成jks. 如不輸入 "srcstorepass"或"deststorepass" 輸入之後會跳出提示"輸入密碼"
#keytool -importkeystore -srckeystore platform.pkcs12 -srcstoretype pkcs12   -destkeystore platform.jks -deststoretype JKS [-srcstorepass PASSWORD -deststorepass PASSWORD]


verify that the key has been added to the keystore.
#keytool -list -v -keystore .keystore



ref: ALLSTART's BLOG

Wednesday, August 27, 2014

Android setting file


Android launch by default app
/data/system/users/0/package-restrictions.xml

Map Uid and permission

/data/system/package.xml

Thursday, March 27, 2014

Compile APK with static java lib or java lib in AOSP

1. Create a folder("Myproject") under ANDROID_ROOT/packages/apps/
2. Before compile APK. Make sure compile all reference library first.
3.1 Part of make file example with local_java_library
 
     LOCAL_JAVA_LIBRARIES := com.sample.mylocallib

3.2 Part of make file example with local_static_java_library
   
    LOCAL_STATIC_JAVA_LIBRARIES := com.sample.mystaticlib
    LOCAL_PROGUARD_ENABLED := disabled

4. Proguard will remove unreferenced code by default. So if APK want keep all code, it may need to set proguard disabled. Or enable proguard and keep class in proguard setting
 
     Make file example:
     LOCAL_PROGUARD_ENABLED := custom
     LOCAL_PROGUARD_FLAG_FILES := proguard.flags

     proguard.flags example:
     -keep class com.sample.mystaticlib.** { *; }
     -keep interface com.sample.mystaticlib.interface.** { *; }

5. If get error "can't find xxxx/classes.jar" when compile APK. It may add classpath in make file
   
   Make file example:
    LOCAL_CLASSPATH := out/target/common/obj/JAVA_LIBRARIES/libmyinterface_intermediates/classes.jar

create static JAVA lib jar for other APK reference

static_java_library 和 java_library不同點:
1. java_library 編譯完會output到 ANDROID_ROOT/out/target/product/generic/system/framework,而static_java_library不會。
2. 當APK reference到java_library時,不會把jar source編譯進APK。但是static_java_library會編譯進去, 所以執行該APK的機器不需要有static_java_library存在。
3. 編譯兩種library的方法一樣,只差在make file

Make file example:

ifneq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),)
LOCAL_PATH := $(call my-dir)
# the library
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= com.mylib.staticlibrary
include $(BUILD_STATIC_JAVA_LIBRARY)
# the documentation
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)
LOCAL_MODULE:= mystaticlibraryDoc
LOCAL_DROIDDOC_OPTIONS := com.mylib.staticlibrary
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true
include $(BUILD_DROIDDOC)
endif # JAVA_SUPPORT

Friday, March 14, 2014

custom JAVA library JAR in Android devices

Use custom library JAR in Android devices(Kitkat)
1. Create "MySystemLib"folder under AOSP_ROOT/device/sample/frameworks/MySystemLib
2. Put JAR source code in  AOSP_ROOT/device/sample/frameworks//MySystemLib
    Should include forder "java", Android.mk, com.my.internal.lib.xml
 
    Android.mk:
ifneq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
            $(call all-subdir-java-files)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= com.my.internal.lib
include $(BUILD_JAVA_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)
LOCAL_MODULE:= LauncherFacadeInterface
LOCAL_DROIDDOC_OPTIONS := com.sprint.internal.idinterface
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true
include $(BUILD_DROIDDOC)
endif # JAVA_SUPPORT

3. "make  com.my.internal.lib" or "mm -B"
4.  com.my.internal.lib.jar is generated in 'out/target/product/generic/system/framework'
5. Put com.my.internal.lib.jar  in /system/framework in android devices
6. Put com.my.internal.lib.xml in /system/framework/etc/permission in android devices
7.  Reboot device to refresh system lib
8. Declare lib in manifest.xml
   
            android:name="com.my.internal.lib"
             />

Friday, March 7, 2014

ClassLoader in Android

PathClassLoader:
Load class from Android system. So those classes have to be registered in system. They may be a installable APK. And the class(dex files) would storage in /data/dalvik-cache


        String packageName = "com.android.calculator2";
        String className = "com.android.calculator.sample.class";

        String apkSourcePath;
        apkSourcePath =getPackageManager().getApplicationInfo(packageName, 0).sourceDir;

         PathClassLoader pathClassLoader = new dalvik.system.PathClassLoader(apkSourcePath,
                    ClassLoader.getSystemClassLoader());
          Class clazz = pathClassLoader.loadClass(className);
          Method[] methods = clazz.getMethods();
          for (Method m : methods) {
                Log.i("","method name:" + m.getName());
           }


DexClassLoader:
Load class from jar, apk file. Those file is stored in accessible storage, such as internal storage or sd card. When class loader loads jar file, it will generate dex file. Then load class from dex file.

Example:
           Put  com.android.calculator.jar in /data/data/MY_APP_PACKAGE/files    

            File jarFile = new File(getFilesDir().getAbsoluteFile() + File.separator
                    + "com.android.calculator.jar");
            final File optimizedDexOutputPath =getDir("outdex", Context.MODE_PRIVATE);
            DexClassLoader dexClassLoader = new DexClassLoader(jarFile.getAbsolutePath(),
                    optimizedDexOutputPath.getAbsolutePath(),
                    null,
                    ClassLoader.getSystemClassLoader());
            Class clazz = dexClassLoader.loadClass("com.android.calculator.sample.class");
            Method[] methods = clazz.getMethods();
            for (Method m : methods) {
                Log.i("","method name:" + m.getName());
            }
            Constructor constructor = clazz.getConstructor(Context.class);
            Object instance = constructor.newInstance();

Wednesday, January 15, 2014

Start Service return type

START_NOT_STICKY
啟動之後,如果service被kill by system,service不會自動啟動,也不會自動重新傳入intent. 除非開發者有程式呼叫startService(intent)

 START_REDELIVER_INTENT
啟動之後,如果service被kill by system,service會自動啟動,也會自動重新傳入最後一個intent.

START_STICKY
啟動之後,如果service被kill by system,service會自動啟動,但不會自動重新傳入intent. 如果再kill期間,開發者有程式呼叫startService(intent), 才會傳入intent.

START_STICKY_COMPATIBILITY

啟動之後,如果service被kill by system,service不一定會自動啟動, 如果啟動的話,模式與START_STICKY一樣。

Monday, September 23, 2013

Decompile APK

1. Change .APK to .ZIP and unzip it. There is classes.dex in the folder.
2. Download dex2jar(http://code.google.com/p/dex2jar/) to convert classes.dex to classes.
     . dex2jar.sh PATH_TO_CLASSES_DEX/classes.dex
    
    The command should generate classes_dex2jar.jar
3. Download jdgui(http://mac.softpedia.com/dyn-postdownload.php?p=48710&t=4&i=1) to unzip jar file.
    Open jdgui and choose classes_dex2.jar.jar.  All folder and java file should be browsed in JDGUI.  
    Save all source.

4. Download android-apktool(https://code.google.com/p/android-apktool/downloads/list). Choose "apktool1.5.2.tar.bz2" and "apktool-install-macosx-r05-ibot.tar.bz2"(This depends on platform).
    . apktool d PATH_TO_APK/xxx.apk
   All resource and xml file are generated in APK_NAME folder

Tuesday, May 28, 2013

Run GCM demo by app engine server

GCM api
1. Create google api project about GCM
2. Create server key. This api key will be used fot setting up server application.

Create App Engine project
1. Apply one project in app engine. Input project name and project url name.
    Peoject url name will be used in Android application.
2. Download App engine SDK for JAVA

  
Set up Server Application
1. Android SDK Manager, install Extras > Google Cloud Messaging for Android Library.
2. In a text editor, edit PATH_TO_ANDOIRD_SDK/extras/google/gcm/samples/gcm-demo-appengine/src/com/google/android/gcm/demo/server/ApiKeyInitializer.java and replace the existing text with the API key obtained above.
3. In a shell window, go to the PATH_TO_ANDOIRD_SDK/extras/google/gcm/samples/gcm-demo-appengine directory.
4. ant -Dsdk.dir=PATH_APP_ENGINE_SDK/appengine-java-sdk-1.8.0/ compile
    It will generate WebContent/. The code will be upload to App engine, so we don't need to launch server in our local side.

Upload Server Application to App Engine
1. From the appengine-java-sdk directory, run:
     bin\appcfg.cmd update PATH_TO_ANDOIRD_SDK/extras/google/gcm/samples/gcm-demo-appengine/WebContent/
2. Open http://appspot.com 
3. It should show "no devices registered" page.

Set up Android Application
  1. Using a text editor, open PATH_TO_ANDOIRD_SDK/extras/google/gcm/samples/gcm-demo-client/src/com/google/android/gcm/demo/app/CommonUtilities.java and set the proper values for the SENDER_ID and SERVER_URL constants.
  2. In a shell window, go to the gcm-demo-client directory.
  3. Use the SDK's android tool to generate the ant build files:
  4. android update project --name GCMDemo -p . --target android-16
4. ant clean debug
5. It will generate GCMDemo-debug.apk in bin/ folder
6. Install APK in devices.
7. Done.

Tuesday, April 3, 2012

compile Android Apps

Command
1. make: compile all Android system
2. make [package name]: the package name can be found in packages/apps/xx/Android.mk
or
mmm packages/apps/Package_name
The APK and odex will generate in out/target/product/generic/system/app/xxx.apk
3. make snod: this command will regenerate system.img

Put 3rd apk into system.img
1. put apk into out/target/product/generic/system/app/
2. make snod
or
1. mkdir packages/apps/MyApp
2. vi Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_POST_PROCESS_COMMAND:=$(shell cp -r $(LOCAL_PATH)/*.apk $TARGET_OUT)/app/)
3. make


Generate AIDL stub
1. make: compile all Android system
2. All system AIDL will convert java code in android_source/out/target/common/obj/JAVA_LIBRARIES/.....

Tuesday, December 20, 2011

Add external lib in Android.mk

1. add LOCAL_STATIC_JAVA_LIBRARIES += jar'sFileName . This line must be added above "include $(BUILD_PACKAGE)"

2. add LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := pathtoyourlib.jar This line must be added after "include $(BUILD_PACKAGE)"

3. add include $(BUILD_MULTI_PREBUILT)


then compile it

Wednesday, November 16, 2011

android remove preload app

get root permission

check which is target APK
pm path com.xxx.packagename

remount partition to let permission to be read/write
mount -o remount,rw -t yaffs2 /dev/block/mtdblk3 /system

we need get partition info from
cat /proc/mounts

Monday, November 14, 2011

android keycode

utf area code
android/external/icu4c/data/unidata/Blocks.txt

Friday, October 28, 2011

access UI thread

Some ways to access ui thread from other thread

1.Activity.runOnUiThread(Runnable)
2.View.post(Runnable)
3.View.postDelayed(Runnable, long)
4.Handler
when start service, we can use handler+message to access UI thread. Create a Handler in UI thread, and register this Handler in service. Send message to Handler in service, so we can access UI thread by Handler.

5.AsyncTask

Make sure that you access the Android UI toolkit only on the UI thread. 另外service也是run在UI thread之中,所以可以access UI.

service life cycle
1. if start service by Context.startService().
onCreate()->onStartCommand()->onDestroy(). 直到呼叫Context.stopService or stopSelf(). 如果多次呼叫startService. 只要呼叫一次stopService, 則所有呼叫的service都會停止。但service所create的thread不會停止,記得要在onDestroy中停止自己create的thread。

2. if start service by Context.bindService()
onCreate()(只有第一次啟動該service時才會呼叫)-> onDestroy()

Thursday, October 13, 2011

Android Log

main log $adb shell logcat -b main -v threadtime
system log $ adb shell logcat -b system -d *:i
kernel log $ adb shell dmesg (or adb shell cat /proc/kmsg)


system app
/system/app
user app(internal storage)
/data/app
user app(external)
/mnt/asec
/mnt/secure/asec

set system log level per application

       adb shell setprop log.tag.XXXX VERBOSE

Tuesday, September 6, 2011

complie Android source to emulator

依照android source的指示, 但安裝所需套件時, 參考以下套件
Ubuntu Linux (32-bit x86):
sudo apt-get install git-core gnupg sun-java5-jdk flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev

Ubuntu Linux (64-bit x86):
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl sun-java5-jdk zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev ia32-libs x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev

java5 is for Froyo, java6 is for gingerbread
complie完之後,if we want to run emulator. 在environment path中加上"ANDROID_PRODUCT_OUT=target/product/generic"
and exec "./out/host/linux-x86/bin/emulator"

if use gcc4.6 to compile Donut, also need to change some things.reference Here
change ./frameworks/base/libs/utils/Android.mk
from
LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
to
LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -fpermissive

change ./development/emulator/qtools/Android.mk
from
common_cflags := -O0 -g
to
common_cflags := -O0 -g -Wwrite-strings -fpermissive

Change ./frameworks/base/tools/localize/Android.mk
from
LOCAL_LDLIBS += -lrt
to
LOCAL_LDLIBS += -lrt -lpthread

change frameworks/base/tools/appt/Android.mk
from
LOCAL_LDLIBS += -lrt
to
LOCAL_LDLIBS += -lrt -lpthread

host C++: obbtool <= frameworks/base/tools/obbtool/Main.cpp :0:0: error: "_FORTIFY_SOURCE" redefined [-Werror] :0:0: note: this is the location of the previous definition cc1plus: all warnings being treated as errors make: *** [out/host/linux-x86/obj/EXECUTABLES/obbtool_intermediates/Main.o] Error 1 To fix this, refer to this patch. https://github.com/CyanogenMod/android_build/commit/e9cbfa60c8dd60d04570d8bf7bd0d54a4304baf5 It will patch line 61 in build/core/combo/HOST_linux-x86.mk as following. -HOST_GLOBAL_CFLAGS += -D_FORTIFY_SOURCE=0 +HOST_GLOBAL_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libMesa_intermediates/src/glsl/linker.o] Error 1 change external/mesa3d/src/glsl/linker.cpp Adding a "#include
from
unsigned output_index = offsetof(VertexOutput,varyings) / sizeof(Vector4); /*VERT_RESULT_VAR0*/;
to
unsigned output_index = offsetof(VertexOutput,varyings) / sizeof(Vector4); /*VERT_RESULT_VAR0;*/


make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/liboprofile_pp_intermediates/arrange_profiles.o] Error 1
change external/oprofile/libpp/format_output.h
from
mutable counts_t & counts;
to
counts_t & counts;

make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libgtest_host_intermediates/gtest-all.o] Error change external/gtest/include/gtest/internal/gtest-param-util.h
add
#include <cstddef>

make: *** [out/host/linux-x86/obj/EXECUTABLES/test-librsloader_intermediates/test-librsloader] Error 1
change external/llvm/llvm-host-build.mk
add
LOCAL_LDLIBS := -lpthread -ldl

查看編譯版本等資訊
getprop




on Mac OS
Mac OS:10.6.6(snow leopard)
xCode: 
4.3.3 (10G)
macport:for snow leopard
gmake:3.81

download and install package by step on Android source website.
1. need HFS+ disk format and disk size is suppose be bigger 25G.
2. download xCode.(4.2G). Build Android needs macOS.sdk 10.6. If download xCode4.5, it will not include MacOS10.6. It still need to download old version xCode to get 10.6SDK.
3. setup macport and install some packages (see Android source doc)
4. change gmake version to 3.81
5. Download source code in HFS disk
6. change JDK1.5, snow leopard uses 1.6 by default.
7. Change the gcc alias. This is needed by Qemu.

sudo rm /usr/bin/gcc
sudo ln -s /usr/bin/gcc-4.0 /usr/bin/gcc
8. To fix this error go to “external/qemu/” and open Makefile.Android file and edit the following line
MY_CC := $(HOST_CC)
to
MY_CC := gcc
9.

Thursday, September 1, 2011

samsung device

unlock MSL: http://www.instinct-samsung.com/how-to/msl-%28merged%29/

remove UICCcheck
mv Android.mk Android.mk.bak(let builder ignore this app)
remove system/app/xxUICCxx.apk

Wednesday, July 1, 2009

android apk產生與安裝

1. eclipse中的package expore, 點選專案,右鍵>android tools>export undigned application package。除此之外也可以在編譯完後,在專案的目錄中的bin/找到unsign apk
2. 如果是signed apk的話,參考這篇

安裝
1. 進入SDK目錄下的tools, 找到adb
2. $adb –e install xxxx.apk (安裝xxxx.apk到模擬器中) adb用法參考這

有時候安裝的時候出現Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES],看了網路上的說明,似乎是source與編譯完的apk有差異….不是很懂。但再重新編譯一個apk就可以安裝了..

Wednesday, May 6, 2009

intent用法收集

1. 從AP開啟控制台location的設定
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));



2.從AP開啟android內部圖庫,並傳回選擇的圖片

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE)
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
// TODO Do something with the select image URI
}
}



3. 切換到另外一個class視窗

Intent intent = new Intent(this, RedirectGetter.class);
startActivityForResult(intent, INIT_TEXT_REQUEST);

Tuesday, May 5, 2009

android DDMS 啟動 sdcard存取

須要先建立sdcard鏡像檔, 再啟動模擬器時連結鏡像檔即可存取
1.在commond line下執行 $andoird_sdk_home/tools/mksdcard 256M d:/test/testSDcard.iso
2. 在eclipse中 Run->Run configures->'target'->addtional Emulator Command Line Options,
加入參數-sdcard d:/test/testSDcard.iso
3. 在DDMS啟動Emulator
4. DDMS->File Explorer, 找到sdcard這個目錄, 右上角可以看到'Push a File onto the device', 就可以上傳了

除了直接在DDMS中上載檔案到sdcard, 指令列也可以
1. 同上述第一步驟
2. $andoird_sdk_home/tools/emulator -sdcard d:/test/testSDcard.iso
3. 開另外一個command line, $andoird_sdk_home/tools/adb -e push test.jpg sdcard, 即可上傳test.jpg這份檔案到emulator的sdcard這個目錄

PS 新版的DDMS已經可以直接在create emulator中直接建立sdcard空間