Go: Android Development: Read app signatures with JNI

Source: Internet
Author: User

Blog post from HTTP://WWW.TUICOOL.COM/ARTICLES/UVJME2R, thanks to bloggers for sharing

In order to prevent the anti-compilation, it is intended to write the key code in so (such as add decryption), in so add to determine whether the APK package signature is consistent code, to avoid so two times packaging. In fact, using JNI to read the signature is the reflection mechanism of java.

First look at how Java reads the signature:

1 Try {2       PackageInfo info = Context.getpackagemanager (). Getpackageinfo (Context.getpackagename (), +); 3       Signature sign = info.signatures[0]; 4       LOG.I ("Test", "Hashcode:" + Sign.hashcode ()); 5     Catch (namenotfoundexception e) {6      e.printstacktrace (); 7     }
View Code

Then we implement the JNI C language, and we also need to pass the context:

1 intGetsignhashcode (JNIENV *env, Jobject context) {2   //class of the context3Jclass Context_clazz = (*env)Getobjectclass (env, context);4   //get the ID of the Getpackagemanager method5Jmethodid Methodid_getpackagemanager = (*env)Getmethodid (env, Context_clazz,6"Getpackagemanager", "() Landroid/content/pm/packagemanager;");7  8   //Get Packagemanager Object9Jobject Packagemanager = (*env)Callobjectmethod (env, context,Ten Methodid_getpackagemanager); One //    //Get Packagemanager class AJclass Pm_clazz = (*env)Getobjectclass (env, packagemanager); -   //get the ID of the Getpackageinfo method -Jmethodid methodid_pm = (*env)->getmethodid (env, Pm_clazz, "Getpackageinfo", the"(ljava/lang/string;i) Landroid/content/pm/packageinfo;"); - // - //    //get the ID of the Getpackagename method -Jmethodid Methodid_pack = (*env)Getmethodid (env, Context_clazz, +"Getpackagename", "() ljava/lang/string;"); -   +   //get the current app's package name AJstring application_package = (*env)Callobjectmethod (env, context, at methodid_pack); -   Const Char*str = (*env)->getstringutfchars (env, application_package, 0); -__android_log_print (Android_log_debug, "JNI", "PackageName:%s\n", str); -   -   //Get PackageInfo -Jobject PackageInfo = (*env)Callobjectmethod (env, Packagemanager, inMETHODID_PM, Application_package, 64); -   toJclass Packageinfo_clazz = (*env)Getobjectclass (env, packageinfo); +Jfieldid fieldid_signatures = (*env)Getfieldid (env, Packageinfo_clazz, -"Signatures", "[Landroid/content/pm/signature;"]); theJobjectarray Signature_arr = (Jobjectarray) (*env),Getobjectfield (env, * PackageInfo, fieldid_signatures); $   //Take the first element out of the signature arrayPanax NotoginsengJobject signature = (*env)->getobjectarrayelement (env, Signature_arr, 0); -   //Read Signature's hashcode theJclass Signature_clazz = (*env)Getobjectclass (env, signature); +Jmethodid Methodid_hashcode = (*env)Getmethodid (env, Signature_clazz, A"Hashcode", "() I"); theJint hashcode = (*env)Callintmethod (env, signature, methodid_hashcode); +__android_log_print (Android_log_debug, "JNI", "Hashcode:%d\n", hashcode); -   returnhashcode; $}
View Code

The second parameter, Jobject, is not the default parameter of the JNI function, but the incoming context instance.

Go: Android Development: Read app signatures with JNI

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.