Trying to execute/debug an existing APK file with Eclipse - android

I have an existing .APK file without any sources. I want to debug it with Eclipse on Bluestacks (or other) emulator. Eventually, I'd like to set a breakpoint, but for now, I just want to get it to run on the emulator. I'm not talking about just using adb to install it on the emulator and then run there. I've been unsuccessful in getting the resulting .apk, built by Eclipse, to run on the emulator.
Here are the steps I've done:
I renamed the .apk to .zip and unzip into a folder.
In Eclipse, I created a new "Android project from existing code". In the next screen, "Import Projects", I browsed to the folder where the apk was unzipped.
But this project has errors in Eclipse! So I tried the next steps:
I executed apktool on the .apk, and it created a folder which I use for the same import
mentioned in step 2. Now the project no longer has errors.
In Eclipse, I "run as" or "debug as" this project as an Android application, and it starts on the emulator.
No good! The logcat shows errors such as "dalvikv - thread exiting with uncaught exception". And there are other logcat messages about being unable to instantiate application and java.lang.ClassNotFoundException.
QUESTION 1:
Can someone tell me what other steps are necessary to turn this into a "good" project? Is there something obvious that I'm missing about Classes?
Once I can get it to either "run as" or "debug as" successfully, then I will want to debug it by setting a breakpoint. But I can't seem to get the source folder right. I have .smali files as a result of the apktool step mentioned in step 3. Also, I've tried various tools, such as dex2jar and jd-gui, so that I have .java files. But whenever I point tell Eclipse the folder where these sources are (and I have "search subfolders" checked), Eclipse says "Source not found". And "Edit Source Lookup Path". I also tried putting the
sources in the /src folder of the workspace.
QUESTION 2:
Where can I put the sources so that Eclipse will find them? Can these source files be either .smali or .java?

So basically what you are trying to do is to extract the source out of a packaged APK file. What you have achieved is the max i could do as well. There's no way to get the exact and true source code due to obfuscations whilst packaging an APK file. So what you have reached to is the max you can get to. I used these tools but never could extract a true .java file as the author may have written. It will contain some compiler addtitons and optimizations which are not understood by eclipse.

Question 1: You will have many step to get android project from .apk file. This work is named reversing, decoding...
You can use apktool to decompile apk to .smali files
Then use javaDecompiler for reversing .smali files to .java files
Create project and import .java files into it
Question 2: Try question 1 you can do it.
Notice: if developer who coded that apk use proguard, you only see follow of code.

Related

Fails to generate apk file

Ok, so recently I've been trying to run my application on Android Studio (1.1.0), but every time I try to run it, the following error pops up:
Waiting for device
Target device: samsung-blah-blahblah
Uploading file local path: C:\Users\jimmykim9001\AndroidStudioProjects\blahblah\app\build\outputs\apk\app-debug.apk
remote path: /data/local/tmp/blah.com.blah
Local path doesn't exist.
Now by doing some research I've come to this link: https://www.youtube.com/watch?v=i4WcMIyc8OQ This would have been helpful if I was able to find my apk files when searching my project; however, they're not there, which is strange because I'm pretty sure they're supposed to be created automatically. So I tried to rebuild the project, but that fails half-way through (Failed to complete Gradle execution. Cause: ). What I need to do is get Android Studio to generate the app-debug.apk file, but I have no idea how. All the links I've found involve moving the file into the right place, but the file itself doesn't exist for me. Any ideas?
Note: I imported a jar file recently and ever since I did that, the problems started. Not sure if this matters or not.
I got your question. I have also faced the same problem some time ago. Let me clue you in , here is some approach i would like to try :-
Your apk must be here ( if it have been automatically generated)
your_app_name\app\build\outputs\apk
If app-debug.apk is not there then , Clear the cache File -> Invalidate Cache, then restart your IDE . Then rebuild it.
Try removing the jar you have recently added and rebuild the project.
you can try re-importing your project.
From above all what worked for me 1st point . Try these .................................best of luck.
Here are some links you might like to see....
Build unsigned APK file with Android Studio
Apk location in New Android Studio

Android studio - how to see APK file in project view

I would like to see the output folder itself directly in the android studio IDE.
To be clear i want the apk output folder to show up in my IDE so i can see apks as they are built. The folder from the OS explorer shows the apk in Foo/*/build/outputs/apk/ .
However, i want this same folder to appear in the IDE. Is it possible ? This way after a build it would be easier to get the apk and its also a good indicator when the build process is complete.
Here is what the IDE UI currently looks like, notice how its missing any output folder:

lost all projects from eclipse all java and xml files on workspace have 0kb how to restore

I am using eclipse juno -adt tool Eclipse Platform Version: 4.2.1.
i lost all projects from eclipse project explorer .they doesnt have single line of code. all projects in work space folder have 0kb data(user/workplace). is it possible to restore the complete projects from .metdata or work space runtime.
cd ~/Documents/workspace/.metalog/.plugins
rm -rf org.eclipse.core.resources
i have tried above commands but rm is not recognized .how can i setup path for eclipse.. please help me out..
I don't think that you can get it back, ask experts if you still don't get you may try this, there is 50% chance that you may get .java files atleast.
1) Download recuva
2) Open recuva and select All contents.
3) Locate the path to your workspace.
4) "Deep scan" that may take hours, when you get all the deleted data back, extract .java, .xml files from them.
5) Create a new workspace and add those files, and you can reprogram Manifist files manually. (If you are working on android)
Try manually separating metadata for each package from the metadata folder in your workspace. Once you do that put the corresponding metadata and packages to a separate folder and then in your eclipse choose workspace window select the folder which you created for individual packages. It'll work. All the best.
I have just experienced the same problem. The entire project displays all the files but they are all 0 bytes. Most recent bit bucket sync is not very recent. After cursing and kicking things, I had some luck with recuva. Since it is an android project, I have the latest apk I had installed in my phone yesterday as I was doing the test, dex2Jar and JD-GUI helped a little her, but there are still mountains of code I will never get back.

android - .apk file is not modified

i run the application using eclipse at this .apk file is not modified. Suppose i used the .apk file in the device at the installation time i got the “Parse Error : There is a problem parsing the package”. How to solve this? can anybody help me.
For this i deleted the .apk file from the bin folder and after run the app using eclipse. Generally in this case .apk file is generated. but here it is not generated. but .class file are generated.
I got this errors in 2 cases-
When I changed the package name of the app (In that case, uninstall old and run it again)
When you install this APK from Gmail in < Android 2.2 phones.
Since you are getting this in Eclipse, did you change the package name or edited in Manifest?
Try: Project -> Clean.
Or, try: Right click on project -> Android Tools -> Fix Project Properties.
In the same menu you can change the package project.

Repackage APK file to contain custom assets - what build tool to use?

Update: This is an old post, and references below to broken aapt versions will be out of date.
Based on previous feedback, I am storing custom text fields in the assets directory of my app.
I will write the app, using default user details in an asset file, and the client would like to rebuild the app for each user, including that user's details in the asset file.
(I am aware this is method has some serious shortcomings, but the client is still keen to do it in this way - see Embed login details in APK file, different for each user (or other options?))
This question relates to troubles I am having with rebuilding the APK file, once I have unzipped it, and updated the custom asset file. I am quite convinced I am missing something small, however, the documentation and posts I have found on these methods are not helpful enough for a newcomer.
aapt - Android sdk tool: Unfortunately the android docs on "Using aapt" on the android docs [link 2 below] are very limited. The console command -help shows a bit more info. When trying to use it to just add a file to the test.apk, it ends up deleting the original, and creating a new file test.apk.zip containing only the file I tried to add. I have not been able to find the correct command line combination to take an unzipped apk and repackage it - that would be my first prize.
apkbuilder - Android sdk tool: Firstly this tool is deprecated, which is a negative point for it. I also can't get it to work with what I have in that unzipped folder. I think I'm missing a pre-apkbuilder step because apkbuilder asks for a resource zip archive, and I have a resource folder.
ant - build tool: Other similar posts say to build with ant, rather than using the android tools. I am having trouble getting ant to work. One particular link to Getting Ant to Work with Android [link 3 below] looks promising but looks like it is for a different android sdk (my build.xml that is generated by android looks different to his). Unfortunately I know little about ant, and am having trouble becoming expert enough to solve my current issue.
Further to NickT's solution below - running the ant script gives me the error
taskdef class com.android.ant.SetupTask cannot be found using the classloader AntClassLoader[].
I have found some references online to this error, have confirmed that local.properties has an sdk.dir setting that is pointing to my android sdk install folder (sdk.dir=/Applications/android-sdk-mac_86).
?????: There might be some other option that I have not listed / discovered, which I would be interested in hearing about.
I realize that delving into the gears that are normally covered up by my ide can lead to diffuculties. But I know that a lot of the SO users can do many of these things, and I hope I get the interest of some of them. Thanks for any help.
(Eclipse 3.6 on Mac Snow Leopard 10.6 64 bit)
PS, I am not able to post more than 1 hyperlink yet, so I have included these addresses to show more info to my question.
(1): stackoverflow.com/questions/4783160/embed-login-details-in-apk-file-different-for-each-user-or-other-options
(2): developer.android.com/guide/developing/tools/aapt.html
(3): www.disgruntledrats.com/?p=27
To answer my own question with the method we finally chose, and "for the record":
I was unable to get Ant working correctly for me (my lack of understanding).
I ended up using the aapt tool. It works as per the Android sdk documentation and really is quite simple to use:
aapt add -v Test.apk "assets/readme.txt"
This will add a readme.txt file (we were storing it in a subfolder of the current running folder, called assets. If you want to store the new file somewhere else, aapt does offer a command line switch to specify that)
However, there are some caveats:
I couldn't get it to work on signed APK files.
some versions of aapt don't work properly! They don't recognise the subfolder...
To solve 1:
Export the APK from eclipse as an unsigned APK.
Use the keytool to generate a key (see Android dev docs).
Use aapt.
Sign the APK using jarsigner.
To solve 2:
I can't really help with this. Every version of the SDK (on Mac) I tried to download included an aapt tool that did not recognize the subfolders. A colleague downloaded a version that worked, but we could still not figure out which version it was that he downloaded, so we kept this "magic" copy and renamed it and will be using it until we find a new version that works.
The simplest solution would be to give the template source of the app to your client with a script that rebuild the app from scratch.
This way your client simply have to change the assets in the folder, double click a batch file and get the apk in a minute or so.
You don't have to know anything about ANT to compile and package without the IDE, just follow the steps here :
http://developer.android.com/guide/developing/other-ide.html
The ANT script is generated by the "android create" "android update" commands. You then just need to run "ant release" and off you go.
A bit late, probably, but this post appears in lots of searches, so I thought this may be of interest.
I just used a standard Zip tool to 'unsign' the APK by removing the META-INF folder, updated any asset files, and then resigned the APK using jarsigner. (If I could find a way to sign JARs without using jarsigner, so we wouldn't need the Java SDK installed everywhere, this would be really useful - ideas anyone?)
To expand on Yahel's answer, yes I think Ant is the way to go. It can be a bit intimidating, but it's urprising how well it works 'out of the box' without too much messing about. Customising it takes a bit of reading but I managed it, so I'll give you the benefit of my experience.
Firstly create a sample application as suggested. Let's suppose you want it to be created in c:\junk.
Then from your a command prompt in Android SDK\tools directory type:
android create project -t 5 -n AndSampleApp -p c:\junk\AndSampleApp -a AndSampleApp -k com.yourpackage.andsample.
( -t 5 gives you API level 7, type 'android list targets' to see other options)
Let's assume you've been building your projects in an Eclipse workspace (it's /dev/projects/EclipseHelios/AndroidWorkspace on my machine). Copy the sample app's build.xml and build.properties to the project folder in Eclipse
and edit the newly created build.xml and remove the line
<project name="AndSampleApp" default="help">
and replace it with just:
<project>
Now edit the the newly created build.properties which will be empty except for comments and add (customisng where necessary):
# The password will be asked during the build when you use the 'release' target.
# OH NO IT WON'T! IF YOU PUT THE KEYS & PWDS IN AS BELOW - It'S AUTOMATIC
# Customised (hard coded) section ---------------------
projectname=YourActualProjectName
# Line below is the parent folder of where the sources are
workspace.dir=/dev/projects/EclipseHelios/AndroidWorkspace
# Line below is where you want the binaries to go
outbasebase.dir=/dev/projects/AntBuilds
key.store=c:/users/you/your-release-key.keystore
key.alias=your_release_alias
key.store.password=YourSecretPassword
key.alias.password=YourSecretPassword
#------------------------------------------------------
ant.project.name=${projectname}
base.dir=${workspace.dir}/${projectname}
source.dir=${base.dir}/src
outbase.dir=${outbasebase.dir}/${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout
If you then get a command line prompt in /dev/projects/EclipseHelios/AndroidWorkspace/YourActualProjectName
and type 'ant release', you should end up with a built and signed apk in /dev/projects/AntBuilds/YourActualProjectName
You should be able to customise the assets location by the adding an assets.dir entry in the build.properties

Categories

Resources