Posts

    Saturday, 3 January 2015

    Installing python on Android 5.0

    Executables with PIE (Position Independent Execution) have been supported since Android 4.1. It is a security feature involving address space layout randomization(ASLR) to prevent attacks such as buffer overflow. In Android's latest release Lollipop, PIE has been made a requirement. Similar to the way the shift from Dalvik to ART has crippled so many applications, this move has caused a lot of problems to developers.  Even some of the binaries shipped with android itself were broken because of this.

    I will be showing you how to install RunPythonFromShell. If you have already installed it and want to see how to bypass PIE go to the section Bypassing PIE.

    Step 1

    Download apk

    Install the apk and open it. Click the install button in the app to install all the modules and binaries.

    Step 2 

    If you don't want to run python from everywhere, skip this step. 
    • Open adb shell
    • Download the following gist. Move it to /system/bin and change set executable permission.
    • If you are running an android version older than lollipop then you are done! Lollipop users have to bypass PIE.

    Bypassing PIE

    Due to the PIE restrictions you get the following error when you run python:
              
             error: only position independent executables (PIE) are supported.

     The idea here is to replace the default linker with a custom linker (You need a rooted device. duh!).  First mount /system in write mode and backup the default linker.

    mount -o remount,rw /system /system
    mv /system/bin/linker /system/bin/linker.old

    Download custom linker

    Move custom linker to /system/bin. Then set proper permissions.

    chmod 0755 /system/bin/linker

    You can also use ES File Explorer or Root Explorer to do this. Permission of the file should look like this:

    You are done! You can run python from terminal!


    I will be writing a post on what all cool things you can do with python on android. Stay tuned! 

    Update

    Some of the users with 64 bit architecture were facing some linking problem. Here is what you have to do.
    Create a new lib64 directory and move the library file to that folder.

    mkdir /data/data/com.googlecode.pythonforandroid/files/python/lib64
    cp /usr/lib64/libcrypto.so   /data/data/com.googlecode.pythonforandroid/files/python/lib64

    Then you have to add this to the library path. To do that you have to update the variable LD_LIBRARY_PATH in the "/system/bin/python" script above.


    Thanks Yan Bellavance for pointing it out!

    Wednesday, 24 December 2014

    Add Exchange account to Gmail 5.0 on older android versions

    I installed unofficial CM 12 with Android 5.0.2 today. Earlier I was using stock Android Lollipop and it had the option of adding Microsoft Exchange account. Unfortunately CM12 didn't seem to have it.

    After doing some research I figured out that only devices running stock Android 5.0+ are capable of having exchange accounts. And CM12 although runs on Android 5.0, doesn't have this feature. On doing further research I found out that Android 5.0+ have something called Exchange services which enables gmail 5.0 to handle exchange accounts.

    Download Exchange Services


    Then, install and restart your device. Walah! Gmail will be able to add exchange accounts!

    Note: This method is not just for CM

    Friday, 5 December 2014

    How I fixed bootloop on my Nexus without loosing data

    I was playing around with framework-res.apk and SystemUI.apk to get Ambient Display on my Nexus 4. When I restarted my phone, it seemed to have stuck in a bootloop. I knew that factory reset might've worked. But I didn't want to lose any data.

    Fortunately adb was working. When I did adb logcat , it was logging an error saying:
     /system/framework/framework-res.apk is neither a file nor a folder (type=0)

    I realized that there was some problem with permissions of that file. I used adb shell and changed the permission of framework-res.apk and SystemUI.apk to 0644. But this was of no use. However I came to know that if I could somehow reset /system partition the problem would be solved.


    • So I downloaded the factory image of my phone. For nexus devices it is available here
    • Extract the contents of the image-xxxx-xxxx.zip to get the file system.img.
    • Then ran the following:
      adb reboot bootloader
      fastboot flash system system.img
    • This will wipe the /system partition and reset it without touching any other partitions. Hence you will not lose any data.
    • Restart. It took a while to start the phone completely. But it solved the problem!
    This is how I was able to reset my framework-res.apk and SystemUI.apk!


    Monday, 24 November 2014

    Type checking in javascript has never been easier

    I love languages which aren't strongly typed. It makes software development process very fast and very easy as we need not write huge, complex, unreadable code like in our typed languages. However this can lead to developer's worst nightmare: Runtime errors! In languages such as javascript it is not easy to check for type mismatches while in development process. Hence flow was invented!

    Flow is one of Facebook's open source projects. It allows developers to check for programming errors in javascript code with very less effort. This is a sample code available on their site:

    function foo(x) {
      return x * 10;
    }
    foo('Hello, world!');

    Note that a string is passed into the function foo and an arithmetic operation is performed on that string inside it. When flow is run on this code:

    01_HelloWorld/hello.js:7:5,17: string
    This type is incompatible with
      01_HelloWorld/hello.js:4:10,13: number

    It points out that the string is incompatible with number. Consider a much much complex code. An error like this can simply be unnoticed until a runtime error is raised.

    Source: https://github.com/facebook/flow
    Installation: http://flowtype.org/docs/getting-started.html
    Examples: http://flowtype.org/docs/five-simple-examples.html

    Sunday, 23 November 2014

    Navigation Bar suddenly disappeared on Nexus

    This can happen on any phone with soft navigation keys.

    Yesterday, I was tweaking my Nexus 4 after updating to Lollipop. When I restarted my phone I found that the Navigation Bar had disappeared! Even to switch from one app to another I had to restart my phone! Then I found a way to fix it.

    Install Root explorer or ES Explorer. Open it and go to /system and mount it in R/W.

    Then open the file build.prop in the current folder using text editor.

    Then go to bottom of the file and change the value of "qemu.hw.mainkeys" from 1 to 0
    Reboot and you are done!


    Thursday, 30 October 2014

    How to mount NTFS External Disk in write mode in OS X Yosemite

    This works for Mavericks also :)

    It is not possible by default to mount a NTFS External Drive in write mode on OS X since Mavericks. There is a complex way to do this. In order to make it simple for you, I wrote a script which does it.

    First you need to install Xcode and Homebrew. Google how to install them :P

    Download this script


    Then do this:
    chmod +x ntfs_write_mode_enabler
    ./ntfs_write_mode_enabler

    Now connect the driveIn Mavericks you will get a warning “Kernel extension is not from an identified developer”. You are good to go if you click OK. However, if you are using Yosemite, you have to restart your system to load the kernel extension. For more on that read this.

    In Yosemite you might get the following error if you are updating old osxfuse. As we have enabled developer mode which allows you to run unsigned kext, it will not be a problem.

    osxfuse: OS X Mavericks or older is required for this package.

    OS X Yosemite introduced a strict unsigned kext ban which breaks this package.You should remove this package from your system and attempt to find upstream binaries to use instead.

    Uninstalling


    Execute the following command to replace the modified NTFS mounter with the original one

    sudo mv /sbin/mount_ntfs.old /sbin/mount_ntfs

    Loading unsigned kernel extensions on Yosemite

    Since Mavericks, OS X started checking signatures of kernel extensions. However unlike in Mavericks, loading unsigned code to kernel is disabled in Yosemite (in Mavericks only a warning, “Kernel extension is not from an identified developer” was displayed). It is required to sign kernel extensions using the Developer ID obtained after enrolling in Mac Developer program. With Yosemite still in beta, it is hard to find signed kernel extensions other than the ones officially released by Apple. 

    You will get this error when you try to load unsigned kext:

    Diagnostics for [unsigned kext]:
    Code Signing Failure: not code signed
    ERROR: invalid signature for [unsigned kext], will not load

    This being said, it is possible to sign all unsigned kext by doing this:

    sudo nvram boot-args="kext-dev-mode=1"
    Restart to get all the kext running!

    Wednesday, 29 October 2014

    Porting from Eclipse to Android Studio on mac

    First download android studio from here.

    Install Android studio from the .dmg file as usual

    Now, before opening Android studio, open the terminal and delete the sdk which comes with it.

    cd /Applications/Android\ Studio.app
    rm -rf sdk

    Now create a link of your old sdk in that folder

    ln -s [absolute path to old sdk]  /Applications/Android\ Studio.app/sdk

    Now open Android studio like any other app and open your existing project. You are good to go!


    Or follow this to migrate your eclipse project to gradle!

    Monday, 6 October 2014

    How to fix blurry windows on retina display on mac

    You might've observed that some third party apps have blurry windows on mac. This is due to the fact that those apps are not making use of the retina display introduced by Apple sometime back. I found a simple way to fix this using an app called Retinizer. Simply drag and drop the app (.app file from Applications folder) to its window to "Retinize" the app!

    DOWNLOAD: http://retinizer.mikelpr.com/


    Sunday, 5 October 2014

    The curious case of deleting .nomedia file

    Deleting .nomedia file not enough to make a folder visible in android gallery

    TL;DR: After removing .nomedia file, add/delete some file in that folder.

    You might probably know that if you want to hide a folder from gallery all you have to do is create an empty file called .nomedia. This will make sure that the media scanner will skip that folder.

    Sometime back I did this when I wanted to hide the Whatsapp folder from gallery. Now I changed my mind and want Whatsapp folder to show up in the gallery. So I did like any other guy would do -- I deleted .nomedia file from that folder. But this didn't work.

    If I had any android version prior to Kitkat, I could've just run media scanner from some app. Unfortunately after Kitkat this wouldn't work (They changed how media scanner works to save battery).

    Then I tried renaming the folder (after deleting .nomedia file). Magically, all the images started displaying in the gallery. However, I couldn't afford changing the name of the folder.

    I realized that post jellybean, media scanner updates the media index only when there is a change in the folder. So, I just added a new image to that folder manually. It worked like a charm!

    NOTE: If the folder is on sdcard, you can just remove the sdcard and put it back (after removing .nomedia file) to run media scanner.