recompiling a new apk after decompiling and modifying the previous apk - java

I have de-compiled an apk file using apk-tool (command: "apktool d file.apk") and changed some xml parts.
Also I have the jar file using dex2jar. I opened jar with java de-compiler and selected "save all sources". Then I unzipped it and changed some parts of the code (modified it).
Now how should I regenerate the new apk file?? I tried copying java sources into the folder from apk-tool and the recompiled them together but it didn't work. What should I do??

You are trying to mix technologies that aren't possible to mix. Apktool during rebuild requires files to be either in the pre-assembled dex format or various disassembled .smali files.
Apktool doesn't have any support to build from java sources. Not to mention, it wouldn't work as Apktool can't rewrite resource IDs which would happen with java sources. You can read the technical explanation here.
You need to build your java sources, then use baksmali to obtain the .smali equivalence and use those in apktool.

Related

Why don't I see code when viewing included Jar libraries in Eclipse?

There doesn't seem to be any code there. I expected to see class declarations so I could see what the code does but instead there's some
Are they somehow precompiled? What's the difference between included Jar file and a pure code?
You are looking at .class file, which is a generated when you compile .java file. To see what the program does, you have to look into .java file. You can refer official java documentation for that.
A .jar file is packaged file with .jar extension, it contains compiled java files and their class files. This file is usually imported into projects to use the classes defined in that package.
You can use "jar xf jar-file" command in command-prompt/terminal to extract the files from jar and look into the package.
A JAR will normally contain compiled class files. It may also contain source files or there may be a separate JAR that contains the source files, but not necessarily so.
If you want to use the library in your project, then a JAR of compiled class files is what you want. If you want the source code, then you'll have to see if it is available from wherever you downloaded this from. If all you want is to see how to use the classes, then probably what you want are JavaDocs for the library you are using. This is an HTML based API documentation.
Well, this is because you haven't attached any source for the mentioned dnsns.jar. You can attach source to existing JAR files in Eclipse. Refer this SO post: Is there an easy way to attach source in Eclipse?
For this specific dnsns.jar, it is part of your JRE, and if you are not able to see its source in your IDE, then it means that the Java that you have setup in IDE lacks the source. If your installation does not have the source (src.zip), then you can get it manually as mentioned on this SO post: Where to find Java JDK Source Code?
EDIT: Alternatively, you can also use a decompiler (e.g. http://jd.benow.ca/) to reverse engineer the source from byte code, though, it may not be the exact match to the original source but you can understand the overall idea. You can add the decompiler as the default program for opening .class files in eclipse Windows > Preferences > General > Editors > File Associations. Select *.class filter and add your decompiler as the program. Though, it is not as clean as attaching the source to JAR, but may work if you don't have access to source.
EDIT2: About your question
What's the difference between included Jar file and a pure code
Eclipse can find .java files for your own code because obviously they are in your workspace. But when you add a JAR file as library, it may have the source (.java) in it or not. If the source is available, eclipse can display it by default. If not, you have to add it manually.

IntellIj IDEA cannot find source files

I'm trying to attach sources for this library called FileDrop so I can view documentation. When I click Attach Sources... and select the directory with the source code, nothing changes. It still has the Sources not found message at the top of the class. However, in my external libraries section, if I manually expand the library jar file, I can view the original source in there. I think this is because the library jar has the full, uncompressed source in it.
The zip file of the library has this structure after downloading and extracting it:
filedrop-1.1
Example.java
filedrop.jar
FileDrop.java
So the source files do not have any package...not sure if that is part of the problem. The two .java files are also in the jar file. So when I create a library in IDEA, the jar shows up in External Libraries under the library I created, but the source does not.
In the Project Structure -> Libraries screen, it shows the jar file under Classes, and it shows the parent directory (filedrop-1.1) under both Classes and Sources, but it is apparently not detecting any sources within them. This is the screen where I've been trying to add the sources, and they show up here as expected, but apparently no sources are found.
This is IntelliJ IDEA 13.1.4.
The packaging of this library is extremely weird. The jar file contains two different FileDrop classes, one in the default package and another in the net.iharder.dnd package, which seems to be a newer version. It also contains two source files (Example.java and FileDrop.java), the second of which seems to be the source code of the old version of the class (the one in the default package).
The CVS repository at http://iharder.cvs.sourceforge.net/viewvc/iharder/filedrop/net/iharder/dnd/ seems to contain the new version of the code, so you can do a CVS checkout and attach the iharder/filedrop directory from the checkout as sources to the library.

What exactly does a jar file contain?

As an intern, I use company code in my projects and they usually send me a jar file to work with. I add it to the build path in Eclipse and usually all is fine and dandy.
However, I got curious to know, what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
What does this mean? I come from a C/C++ background so is a jar similar to an already compiled .o file and all I can see is the .h stuff? Or is there actual code in the jar file that I'm using that's encrypted so I can't read it?
Thanks for all the answers!
Edit: Thanks, guys, I knew it was a sort of like an archive but I was confused to why when I tried to open the .class files, I got a bunch of random characters. The output was similar when I tried to open a .o file in C so I just wanted to make sure.
Thanks!
A JAR file is actually just a ZIP file. It can contain anything - usually it contains compiled Java code (*.class), but sometimes also Java sourcecode (*.java).
However, Java can be decompiled - in case the developer obfuscated his code you won't get any useful class/function/variable names though.
However, I got curious to what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the jar itself). It's hard to compare C to Java really, as Java byte code maintains a lot more metadata than most binary formats - but the class file is compiled code instead of source code.
If you either open the jar file with a zip utility or run jar xf foo.jar you can extract the files from it, and have a look at them. Note that you don't need a jar file to run Java code - classloaders can load class data directly from the file system, or from URLs, as well as from jar files.
The best way to understand what the jar file contains is by executing this :
Go to command line and execute jar tvf jarfilename.jar
A jar file is a zip file with some additional files containing metadata. (Despite the .jar extension, it is in zip format, and any utilities that deal with .zip files are also able to deal with .jar files.)
http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html
Jar files can contain any kind of files, but they usually contain class files and supporting configuration files (properties), graphics and other data files needed by the application.
Class files contain compiled Java code, which is executable by the Java Virtual Machine.
http://en.wikipedia.org/wiki/Java_class_file
JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.
Jar file contains compiled Java binary classes in the form of *.class which can be converted to readable .java class by decompiling it using some open source decompiler. The jar also has an optional META-INF/MANIFEST.MF which tells us how to use the jar file - specifies other jar files for loading with the jar.
Jar( Java Archive) contains group of .class files.
1.To create Jar File (Zip File)
if one .class (say, Demo.class) then use command jar -cvf NameOfJarFile.jar Demo.class (usually it’s not feasible for only one .class file)
if more than one .class (say, Demo.class , DemoOne.class) then use command jar -cvf NameOfJarFile.jar Demo.class DemoOne.class
if all .class is to be group (say, Demo.class , DemoOne.class etc) then use command jar -cvf NameOfJarFile.jar *.class
2.To extract Jar File (Unzip File)
jar -xvf NameOfJarFile.jar
3.To display table of content
jar -tvf NameOfJarFile.jar
A .jar file is akin to a .exe file.
In essence, they are both executable files.
A jar file is also a archive (JAR = Java ARchive). In a jar file, you will see folders and class files. Each .class file is similar to a .o you might get from C or C++, and is a compiled java archive.
If you wanted to see the code in a jar file, download a java decompiler (located here: http://java.decompiler.free.fr/?q=jdgui) and a .jar extractor (7zip works fine).
JD-GUI is a very handy tool for browsing and decompiling JARs
A .jar file contains compiled code (*.class files) and other data/resources related to that code. It enables you to bundle multiple files into a single archive file. It also contains metadata. Since it is a zip file it is capable of compressing the data that you put into it.
Couple of things i found useful.
http://www.skylit.com/javamethods/faqs/createjar.html
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
The book OSGi in practice defines JAR files as, "JARs are archive files based on the ZIP file format,
allowing many files to be aggregated into a single file. Typically the files
contained in the archive are a mixture of compiled Java class files and resource
files such as images and documents. Additionally the specification defines a
standard location within a JAR archive for metadata — the META-INF folder
— and several standard file names and formats within that directly, most
important of which is the MANIFEST.MF file."
Just check if the aopalliance.jar file has .java files instead of .class files. if so, just extract the jar file, import it in eclipse & create a jar though eclipse. It worked for me.
While learning about JAR, I came across this thread, but couldn't get enough information for people like me, who have .NET background, so I'm gonna add few points which can help persons like myself with .NET background.
First we need to define similar concept to JAR in .NET which is Assembly and assembly shares a lot in common with Java JAR files.
So, an assembly is the fundamental unit of code packaging in the .NET environment. Assemblies are self contained and typically contain the intermediate code from compiling classes, metadata about the classes, and any other files needed by the packaged code to perform its task. Since assemblies are the fundamental unit of code packaging, several actions related to interacting with types must be done at the assembly level. For instance, granting of security permissions, code deployment, and versioning are done at the assembly level.
Java JAR files perform a similar task in Java with most differences being in the implementation. Assemblies are usually stored as EXEs or DLLs while JAR files are stored in the ZIP file format.
Source of Information -> 5- Assemblies

can't jar after encrypting .class files

I have encrypted .class files and would like to jar them so that I could use dexclassloader to load them.While the problem I get is that I use ant (build.xml[dx command]) to jar them ,but it makes error.
[echo] Converting compiled files and external libraries into D:\XXX/classes.dex...
[apply] trouble processing:
[apply] bad class file magic (6244b7bf) or version (e2dd.0927)
[apply] ...while parsing com/XXX/LibraryProvider.class
[apply] ...while processing com/XXX/LibraryProvider.class
[apply] 1 warning
[apply] no classfiles specified
It seems like .class file is bad after I encrypted them. How can I solve the problem?
How can I solve the problem?
Get rid of the encryption.
First, such encryption is pointless on regular Java, let alone Android.
Second, for Android, your .class files will only ever exist on your development machine. Android APK files do not contain your .class files with JVM bytecode. The APK files will contain Dalvik bytecode; the build tools cross-compile your .class files to Dalvik bytecode. By encrypting those class files, the build tools cannot use them.
You are welcome to use ProGuard to obfuscate your Java bytecode, as that still results in .class files that the build tools will understand. Proguard is integrated into the Android build chain.

How to convert .apk files to java

How to convert .apk files to java in android.
The extension Google uses for Android Applications (APK) may seem a bit complicated at first, but it really isn’t. In fact, an .apk is nothing else than a .zip file disguised as an .apk. That’s dumbing it down, but you get it. Essentially, if one wanted to see what’s inside an app, they would just change the extension of application-name.apk to application-name.zip, unzip it. And there you have it: The contents of the .apk! We aren’t done yet, read more after the break.
Here is where it gets tricky. Inside the folder where you unzipped the contents of the application, you’ll find a file named classes.dex. That’s the most important file of the whole application, containing all the java files, but it’s encrypted! No worries, that can easily be solved. You’ll need two things:
Dex2Jar from http://code.google.com/p/dex2jar/
A regular Java decompiler, such as JD from http://java.decompiler.free.fr
Copy classes.dex to the folder where you unzipped Dex2Jar, and run from the command line: “dex2jar.bat classes.dex”
This will produce a file, strangely named something like: “classes.dex.dex2jar.jar” If you have WinRAR installed, you can just unpack the files. If you don’t, install it.
Now go ahead and adjust it to your liking!
its easy just follow the below 4 steps :
1) Unzip your .apk to the folder and get the classes.dex file.
2)Place this classes.dex file inside dex2jar(you should download this) and execute ./dex2jar.sh classes.dex in the terminal.
3)It will create classes.dex.dex2jar.jar,place it inside jd-gui(you should download this).
4)Execute ./jd-gui classes.dex.dex2jar.jar---will generate the .java files.
You can't just convert a jar to an apk. It has to be compiled for android. Android's java library doesn't have all the swing stuff that's in normal java, so the app most likely would have to be re-written to work on android
Pretty sure MIDlets (MIDP jars) don't use Swing or awt. They have a separate user interface api under javax.microedition.lcdui (http://java.sun.com/javame/reference/apis/jsr037/javax/microedition/lcdui/package-summary.html)
As for converting MIDP programs to runnable APKs, there is certainly a way. Technically it isn't a conversion but the use of a wrapper around the unconverted MIDlet. This is the approach that Opera Mini for Android uses. More info:
http://edugoing.com/qna/index.php?qa=unanswered&start=40

Categories