技术小黑屋

How to Share Git Tag

Now I have two tags under my git repository. Let’s take a look at how to push the git tags to Server.

1
2
v2850
v4.1.1.2000_2852

###Push a single tag to the server

1
2
3
#git push origin tag_name
#Take v2850 for example
git push  origin v2850 

###Push all tags to the server

1
git push  origin --tags

###Others

Build Android Packages From Command Line

A few months ago,I dealed with a task:To build a large amount of apk files. The trick I came up with is to build apk file from the command so that I could use Python to glue all the works. Eventually I made it.And so this post is to make some description about the trick.
##Requirements

  • Setup JDK
  • Setup Android SDK ##Steps
    1. Generate R class file
    2. Compile Java codes(.java files) into classes(.class) files
    3. Convert .class files into .dex files
    4. Package Resouces
    5. Build Unsigned APK File
    6. Sign Apk with Jarsigner
    7. The Extra One:Use zipalign for optimization

###Generate R class File In Android,We use R class to refer resources instead of hard-coding the resouces.
For a better understanding,pleae have a look at http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=2883

1
aapt package -f -m -J /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen/ -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml

####Some descriptions

  • -f force overwrite of existing files
  • -m make package directories under location specified by -J
  • -J specify where to output R.java resource constant definitions
  • -S directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence.
  • -I add an existing package to base include set
  • -M specify full path to AndroidManifest.xml to include in zip

###Comiple .java into .class files

1
javac -encoding UTF-8 -source 1.6 -target 1.6 -bootclasspath /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -d /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/ /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src//coop/channel/provider/*.java /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen//coop/channel/provider/R.java 

####Some descriptions

  • -encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. If -encoding is not specified, the platform default converter is used.
  • -source release Specifies the version of source code accepted, Please Do NOT use Java 7(1.7)
  • -target version Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6(also 6), and 1.7 (also 7).
  • -bootclasspath bootclasspath Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.
  • -d directory Set the destination directory for class files. The directory must already exist; javac will not create it. As I have suffered a lot using Java 7, It’s recomended to use Java 6

###Convert .class into .dex files

1
/home/androidyue/dev_tools/android-sdk-linux_86_backup/build-tools/17.0.0/dx --dex --output=/home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/

To dive into Dalvik, please visit http://source.android.com/devices/tech/dalvik/index.html

###Package Resouces

1
aapt package -f -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -A /home/androidyue/temp/ubuntu/workspace/MxDataProvider//assets/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -F /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_

####Some descriptions

  • -F specify the apk file to output
  • -A additional directory in which to find raw asset files

###Build Unsigned APK File

1
/home/androidyue/temp/ubuntu/dev_tools/adt-bundle-linux_backup/sdk/tools/apkbuilder /tmp/unsignedApkFile.apk -v -u -z /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_ -f /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex -rf /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src/

####Some descriptions

  • -u Creates an unsigned package.
  • -z Followed by the path to a zip archive. Adds the content of the application package.
  • -f Followed by the path to a file. Adds the file to the application package.
  • -rf Followed by the path to a source folder. Adds the java resources found in that folder to the application package, while keeping their path relative to the source folder.

Sign Apk with Jarsigner

1
jarsigner -keystore /home/androidyue/temp/ubuntu/myKeystore -storepass storepassValue -keypass keypassValue -signedjar /home/androidyue/Desktop/output/max1111111.apk /tmp/unsignedApkFile.apk maxthon -digestalg SHA1 -sigalg MD5withRSA

####Some descriptions

  • [-keystore ] keystore location
  • [-storepass ] password for keystore integrity
  • [-keypass ] password for private key (if different)
  • [-signedjar ] name of signed JAR file
  • [-digestalg ] name of digest algorithm
  • [-sigalg ] name of signature algorithm

###Use Zipalign for optimization

1
zipalign -f -v 4 /home/androidyue/Desktop/output/max1111111.apk /home/androidyue/Desktop/output/max222222.apk

####Some descriptions

  • The is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.
  • For more details, please visit http://developer.android.com/tools/help/zipalign.html

###Others

Start an Android App by ADB

To be more geek,I began to start an Android App by using adb.Thanks to Google.It’s possible and powerful.
The tool we use to make it is ADB(Android Debug Tool),For more detailed use please visit http://developer.android.com/tools/help/adb.html#shellcommands Actually What I use here is

1
start [options] <INTENT>

An example is followed.

1
2
3
4
#Here I want to start MxBrowser app by using ADB
17:04:30-androidyue/tmp$ adb shell am start -n com.mx.browser/com.mx.browser.MxBrowserActivity
Starting: Intent { cmp=com.mx.browser/.MxBrowserActivity  }
Warning: Activity not started, its current task has been brought to the front

the Package name(com.mx.browser) before the slash is provided to determine which app should the intent delivered to.The com.mx.browser.MxBrowserActivity is the destination Activity.
For more explanations about ADB INTENT,please visit http://developer.android.com/tools/help/adb.html#IntentSpec

Could Not Reliably Determine the Server's Fully Qualified Domain Name

An easy trick to solve problem:Could not reliably determine the server’s fully qualified domain name
Let’s take Linux for example.

1
2
#Modify your configuration file.
12:52:21-androidyue/etc/httpd/conf$ sudo vim httpd.conf 

Find out the following sentence

1
2
3
4
5
6
7
8
9
10
11
12
13
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work.  See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make 
# redirections work in a sensible way.
#
#ServerName localhost:80

If the ServerName localhost:80 has been commented,it may cause above problem.A simple trick is just uncomment the sentence.Let it be like this

1
ServerName localhost:80

Hope you could resolve your problem,Guys.

Get MD5 Hash of Big Files

Recently I have been dealing with files and I need to get md5 hash of all kinds of files;Some are small and some are big.
For the small files I use this method to get md5 hash value.

1
2
3
4
5
6
7
def getFileMd5(filename):
    file = open(filename, 'rb') 
    m = md5()
    m.update(file.read())
    file.close()
    result =  m.hexdigest()
    return result

However for calculating md5 hash value of big files,the above method will be very Less Efficient.For Big files I use the following method(It’s acquired from stackoverflow)

1
2
3
4
5
6
7
8
9
10
def getBigFileMd5(filename, block_size=2**20):
    f = open(filename, 'rb')
    m = md5()
    while True:
        data = f.read(block_size)
        if not data:
            break
        m.update(data)
    f.close()
    return m.hexdigest()

And I did a test.The cost of Getting md5 hash of a Big file(size:10.7 GiB; 11,455,512,109 bytes) is 213.447s.And I think it’s OK.

How to Create a .db File in Python With Sqlite3

How to create a .db file in Python with sqlite3?Actually It’s really easy.
Run the following code

1
2
import sqlite3
sqlite3.connect('./test.db')

After running the above code,the test.db file will be created if it does not exist.
Of course,you have the proper permission to create the file.

Yum Install Build-essentials

I was once stucked in installing build-essentials which is for Debian/Ubuntu.I could not find any packages like build-essentials in yum.However this helped me out.

1
sudo yum install kernel-devel kernel-headers

Javac:command Not Found

I have been getting well on with my java tool.However When I use the javac command.It says

1
javac : command not found

It’s just because I just only install the JRE(Java Runtime Environment) and do not install JDK(Java Development KIt).I got the answer going for my fedora

1
sudo yum install java-devel

##Others

Manage Httpd Service

To Start

1
sudo /sbin/service httpd start

To Stop

1
sudo /sbin/service httpd stop

To Restart

1
sudo /sbin/service httpd restart

List Ports on Linux

I have often suffered this painful thing.When I start a service but the destination port is always used.So I should list all open ports and kill the occupied application.
So This is a short tip for how to list open ports on Linux

1
sudo netstat -tulpn

Note sometimes you should grant the command for a enough access.
For a better understanding,please

1
man netstat

or navigate to http://linux.about.com/od/commands/l/blcmdl8_netstat.htm

For Mac Users

1
sudo lsof -i -P | grep -i "listen"

###Others

How to Find Out What Linux Distribution You Are Using

How to known what Linux Distribution I am using now.
All right, run this command and you could get what you need in from the output result

1
2
3
4
5
6
7
8
9
10
11
19:12:17-androidyue~/github/octopress (master)$ cat /etc/*-release
Fedora release 17 (Beefy Miracle)
NAME=Fedora
VERSION="17 (Beefy Miracle)"
ID=fedora
VERSION_ID=17
PRETTY_NAME="Fedora 17 (Beefy Miracle)"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:fedoraproject:fedora:17"
Fedora release 17 (Beefy Miracle)
Fedora release 17 (Beefy Miracle)

How to Delete Git Remote Branches

It’s easy to remove a certain remote branch in a Git respository. You could try this

1
git push origin --delete <branchName>

And of course another short command is waiting for You

1
git push origin :<branchName>

Accelerate Yum Installation

Yum is a great tool for manage dependencies and easy to use.However actually there is a way to speed up the installation.
Fastestmirror is a tool that will help yum determin what mirror is fastest and to use.
To install on Fedora 17

1
sudo yum install yum-fastestmirror

Of course you could customize the fastestmirror configuration easily

1
sudo vim /etc/yum/pluginconf.d/fastestmirror.conf

So when you next install a package from the network,I hope you could feel it much faster.

How to Sum the Number of Files or Folders in a Folder

I once had a request to sum the number of files in a specific folder.After googling I found the command wc could help me out.
wc - print newline, word, and byte counts for each file. And I think wc is short for Word Count.Please have a detailed information by visiting http://linux.about.com/library/cmd/blcmdl1_wc.htm if you have the desire.
Ok,Let’ get the total number of files(including folders) in a specific folder

1
20:56:08-androidyue/tmp$ ls | wc

Well,the following command will sum the number of pure files(Not including folders)

1
20:58:27-androidyue/tmp$ ll | grep ^- | wc

However how to sum the number of pure folder?The following command will help.

1
21:00:20-androidyue/tmp$ ll | grep ^d | wc

Install Git Daemon on Fedora

Git-daemon is A really simple server for git repositories.You can take a detailed understanding by reading https://www.kernel.org/pub/software/scm/git/docs/git-daemon.html .
I once often used git-daemon when I was using Ubuntu.But when I turn to Fedora and found it was not installed.And the output is like the following.

1
git: 'daemon' is not a git command. See 'git --help'. 

So I write this post to record How to install and enable git-daemon First Let’s intall the git Daemon:

1
sudo yum install git-daemon

However git daemon is off by default.We should enable it.We should do as the following.

1
vim  /etc/xinetd.d/git

And modify the file as the following configuration. # default: off # description: The git dæmon allows git repositories to be exported using
# the git:// protocol.

service git
{
            disable         = no
            socket_type     = stream
            wait            = no
            user            = nobody
            server          = /usr/libexec/git-core/git-daemon
            server_args     = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
            log_on_failure  += USERID
            # xinetd does not enable IPv6 by default
            flags           = IPv6

}

And then,We use this command to start the git Daemon

1
git  daemon--export-all--base-path=your_local_git_path

Mostly we could clone the repositories from the git daemon.
But if you have enabled Firewall and other people want to clone the repository from your git daemon,please let the port 9418 available to the outsite.

BashBites:How to Append Outputs to a File

Is there any way to append output(debugging information) to a existings file? As we all know in bash the command ‘>’ will write ouputs to a file.However this command will also erase the existing data it the file.
Of course bash provides a command that appends outputs to a file.It resembles ‘>’.It’s ‘»‘
The following is a piece of example code. #####Let’s look at >

1
2
3
4
5
6
7
8
[androidyue@androidyue tmp]$ touch a.txt
[androidyue@androidyue tmp]$ cat a.txt 
[androidyue@androidyue tmp]$ echo "ABC" > a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
ABC
[androidyue@androidyue tmp]$ echo "DEF" > a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
DEF

#####Well,Let’s look at the exiciting command »

1
2
3
4
[androidyue@androidyue tmp]$ echo "GHI" >> a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
DEF
GHI

Install Flash Plugin on Fedora 17

#####Addings

1
2
3
4
5
6
#For 32-bit System:
sudo rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm
#For 64-bit System 
sudo rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noarch.rpm
   
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux

#####Updatings

1
sudo yum update

#####Installings

1
sudo yum install flash-plugin nspluginwrapper alsa-plugins-pulseaudio libcurl

Issues About Installing Octopress

Actually I am fresh to Write Blog with Octopress in Github Pages.According to the Introduction .And now I have make it avaible and the website is present. However I have suffered some problems when I did the setup.Here is a summary of what I have sufferd and how I resolved.

Could not find rdiscount-2.0.7.3 in any of the sources Run bundle install to install missing gems.
source ~/.rvm/scripts/rvm
rvm use 1.9.3 --default 
I have used rake preview ,But I got 404 when I open the http://127.0.0.1:4000
[androidyue@androidyue octopress]$ rake preview
Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
[2013-09-07 15:46:20] INFO  WEBrick 1.3.1
[2013-09-07 15:46:20] INFO  ruby 1.9.3 (2013-06-27) [i686-linux]
[2013-09-07 15:46:20] INFO  WEBrick::HTTPServer#start: pid=10490 port=4000
Configuration from /home/androidyue/github/octopress/_config.yml
Auto-regenerating enabled: source -> public/
[2013-09-07 15:46:21] regeneration: 95 files changed
>>> Change detected at 15:46:21 to: screen.scss
   create public/github/stylesheets/screen.css 
   Errno::ENOENT on line ["56"] of /home/androidyue/.rvm/gems/ruby-1.9.3-p448/gems/compass-0.12.2/lib/compass/actions.rb: No such file or directory - /home/androidyue/github/octopress/public/github/stylesheets/screen.css
   Run with --trace to see the full backtrace]

As the above information and all the sources are outputed in public/ folder And the following are a part of my _config.yml file

# If publishing to a subdirectory as in http://site.com/project set 'root: /project'
root: /
permalink: /blog/:year/:month/:day/:title/
source: source
destination: public/
plugins: plugins
code_dir: downloads/code
category_dir: blog/categories

So compare the two piece information and check the output dir and is public or a subdirectory in public folder.If your outputdir is a subdirectory you should use this link http://127.0.0.1:4000/yourSubDirName P.S.This is the help from StackOverflow http://stackoverflow.com/questions/17465404/rake-preview-not-working-in-octopress

I will keep record about this topic becuase I am making friends with Octopress in Github Pages

My First Blog

#####Hello World,This page is a playgroud for using Octopress and markdown language.Thanks for my GirlFriend’s support! ######Common Code

echo "Hello World";

######Python Code

1
2
from os import system
system('ls')

######Java Code

1
String s = "Hello World";

######Bash Code

1
2
mkdir test
ls 

######List

  • item1
  • item2