Re-sign.jar to re-sign the APK under your own Mac

Source: Internet
Author: User
Tags egrep

Using Robotinum to the APK file for automated testing, without knowing the source of the situation, only the APK file how to automate testing it?

You first need to re-sign the apk file and get the package name of the APK file and the class name of the program entry.

The first online said with Re-sign.jar this jar package, but I use Mac computer experiment many times can not, has been prompted JAVA_HOME environment variable is not set, but I have set, echo $PATH output can also see Java_home, But the experiment on the Windows system is good, but I am a Mac computer, the experiment for several hours, especially in the sincere advice to test the little girl to help, but the girl does not bird my case, so rage on his own to achieve the function of Re-sign.jar.

What did Re-sign.jar do with the situation seen on Windows? The first is to re-sign the existing apk, then get the package name of the APK file and the class name of the program entry, in order to get the package name and the entry class name of the apk file we need to use the Apktool tool, because some apk is confused, You need to decompile to get a clear androidmanifest.xml file. We know that the package name of the APK is labeled as: Package= "xxxxxx", and the program entry class name is <activity > </activity> tag contains <xxx.action.MAIN> and <category. Part of the laucnch>. This allows you to get the package name and the entry class name by parsing the manifest.xml file.

Note: It is necessary to configure the Java environment variables, the Android environment variables, and to add the Apktool directory to the environment variable. The shell script needs to be with the APK in the sibling directory

Start writing shell scripts.

#!/bin/bash

# program:
#re-sign to Specialy apk
# History:
# 2015.9.4 Evilking First release

Echo ===============================
Echo 1. Place this. sh file under the APK sibling directory you need to sign
Echo 2. Pass in the file_name parameter and set it to the APK file name that needs to be signed
Echo 3. Set the ANDROID_TOOLS_PATH environment variable to the TOOLS path under the SDK
echo 4. Setting Jdk/bin Environment variables
Echo 5. You need to copy/users/your username/.android/debug.keystore to the APK peer directory
Echo 5. This script is signed based on JDK1.6, if the previous apk is not signed with this version may bleed "Unable to sign the jar" case, open with the zip tool, locate the following directory Meta-inf, delete the directory
Echo 6. If there is a warning that-tsa or-tsacert is not provided, the signature instruction is followed by-tsa Https://timestamp.geotrust.com/tsa
Echo ===============================

#获得当前路径
cur_dir=$ (PWD)

#需要签名的apk文件名
File_name=$1

#验证该apk文件是否存在
if [!-e ${file_name}.apk];then
echo "Error:not fount ${file_name}.apk FILE"
Exit 1
Fi

#查看指定apk的签名信息
Echo View signature information for the specified APK
Jarsigner-verify-verbose-certs ${cur_dir}/${file_name}.apk | Tail-n 10

#删除掉apk中的签名信息
Echo =================================
Echo starts to erase the signature information from the APK
MV ${file_name}.apk ${file_name}.zip

#从zip包中直接删除META-inf Folder
zip-d ${file_name}.zip meta-inf/*

MV ${file_name}.zip ${file_name}.apk

echo Delete Meta-inf complete
Echo =================================
#将原来的apk修改回来

#再次查看_unsign. APK signature Information
Echo =================================
echo View _unsign.apk's signature information again
Jarsigner-verify-verbose-certs ${file_name}.apk | Tail-n 10

#对apk包重新签名
Echo =================================
echo re-signing the. apk package

#检测debug. KeyStore is in this directory
if [!-e Debug.keystore];then
echo "Warning:re-sign need Username/.android/debug.keystore file"
Read-p "Input your uname:" uname
Cp/users/${uname}/.android/debug.keystore./debug.keystore
Fi

Jarsigner-digestalg sha1-sigalg md5withrsa-keystore debug.keystore-storepass android-keypass android ${FILE_NAME}.ap K Androiddebugkey-tsa Https://timestamp.geotrust.com/tsa


Echo ==================================
Echo ========= Signature succeeded ... Start simplifying ...

Zipalign 4 ${file_name}.apk ${file_name}_sign.apk
Echo ============ simplifies signature completion ============

#查看重新签名的apk信息
Echo ===================================
echo ====== View the re-signed APK message ==========
Jarsigner-verify-verbose-certs ${file_name}_sign.apk | Tail-n 10

RM ${file_name}.apk

#下面部分是提取该应用的包名和程序入口类名
Echo =======================================
Echo ================ begins extracting the package name =============

Apktool D ${file_name}_sign.apk &>/dev/null

Echo ============= Extract Package name =================

package_name=$ (egrep "package=\". *\ "" ${file_name}_sign/androidmanifest.xml | sed ' s/.*package=//g ')
echo "Package= $package _name"

#获取程序入口
echo ============= Get program Entry ================
Declare-i Last_line
last_line=$ (grep-n "action. MAIN "${file_name}_sign/androidmanifest.xml | Cut-d ': '-F 1)

Declare-i First_line
first_line=$ (cat ${file_name}_sign/androidmanifest.xml | Head-n ${last_line} | egrep-n "<activity" | cut-d ': '-F 1 | Tail-n 1)

echo "Program Entry class is:"
entry_class=$ (head-n $last _line ${file_name}_sign/androidmanifest.xml | tail-n + $first _line | sed ' s/.*android:name=\ ' G ' |   Cut-d "\" "-F 1 | Head-n 1)
If ["$ (echo $entry _class | Cut-d "."-f 1) "= =" "];then
echo "$ (echo $package _name | Sed ' s/'//g ') $entry _class "
Else
echo "$entry _class"
Fi

Rm-r ${file_name}_sign/
echo =================== End =============

Spent a whole day, but the value of the long-lost sense of accomplishment ah.

Reference post: http://blog.csdn.net/h5q8n2e7/article/details/47837653

  

Re-sign.jar to re-sign the APK under your own Mac

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.