How to generate an apk file from Android Application programmatically? - java

This is a weird question . I want to know is there any way we could generate an apk from an android App. Let me explain- An android app which run cmd Commands and could generate apk for another application. Is there any possible way.
I have read that this could be done using ant in Java. I want to know that is this possible on a running android app.
Thanks.

You can use the ANT Jars ant.jar and ant-launcher.jar
in this case the path for build.xml should be fully specified and call it from java class this way :
public class AntTest {
public static void main(String[] args) {
String build = "D:/xampp/htdocs/aud/TempProject/build.xml";
generateApkThroughAnt(build);
}
/*
* Generate APK through ANT API Method
*/
public static void generateApkThroughAnt(String buildPath) {
File antBuildFile = new File(buildPath);
Project p = new Project();
p.setUserProperty("ant.file", antBuildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
BuildException ex = null;
try {
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, antBuildFile);
p.executeTarget("clean");
p.executeTarget("release");
} catch (BuildException e) {
ex = e;
} finally {
p.fireBuildFinished(ex);
}
}
}
To create a build.xml file go to Eclipse=>Your Project=>Right click=>Export=>General=>Ant Buildfiles after that you will need to run :
android update project --name <project_name> --target <target_ID> --path <path_to_your_project>

Scripts can help you. Make a simple scripts which runs all android commands starting from compiling, packaging, creating key-store, signing using that key-store into release mode.
Android has provided everything which can work from command line.Just merge them in form of script and you will be able to generate .apk file.
You can make simple PHP script or ant script.

May be this is Possible, but You can do this by Reverse Engineering methods, so after getting source code of that apk file,compile it using Android IDE.
Reverse Engineering Strp by Step Method for apk decoding:
Step 1:
Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.
Step 2:
Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this link http://code.google.com/p/dex2jar/ and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from http://varaneckas.com/jad and now double click on jd-gui and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.
Step 3:
Now open another new folder and put these files
put .apk file which you want to decode
download apktool v1.x AND apktool install window (both can be downloaded at the same location) and put in the same folder
download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)
Open a command window
Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
apktool d "fname".apk ("fname" denotes filename which you want to decode)
now you get a file folder in that folder and now you can easily read xml files also.
Step 4:
It's not any step just copy contents of both folder(in this case both new folder)to the single one
and now enjoy with source code...

I don't think that's possible.
Because Android doesn't include the "compiler" inside itself. Well I guess If you really want to do it, you can always make a server the generate the apk file, and make your app connect with said server.
Hope that help you :).

Related

execute a .jar file from C# but it generates files at wrong location

I have coded a Minecraft Server Runner in C# WinForms which lets you run a Minecraft Server, a .jar file which needs to generate files. The problem is that I launch this .jar file via the .exe application, and the files generate at the .exe application location.
-- What I have tried:
I tried moving the .exe application to the specific server file location, but the application needs a restart to register this change which I don't want to happen.
I also don't want the user being forced to put the .exe application to the Server folder and restart it. Here is the code I use to launch the .jar file:
Process.Start("C:\user\documents\server\server.jar");
How can I fix this issue?
The jar file can be executed by the java -jar filename.jar. So use the following Process.Start call to invoke the jar.
Process.Start("java", "-jar C:\user\documents\server\server.jar", username, password, domain);
Hope this helps.
P.S: For this to work, either add the Java to your path or invoke with the java.exe's Path.
To fix this I executed the .jar file in the C# application via the CMD.
Here is the code I used instead:
string path = #"C:\user\documents\server\"; //Path to your server.jar file.
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = path + "server.jar"; //Name of the .jar file.
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
process.Start();
All credits go to "Olivier Rogier" ( https://stackoverflow.com/users/12031933/olivier-rogier ) for helping me find this solution

How to find this file in my local computer?

I am using an open source Maven project and it runs fine. I was looking at this code and want to find out where the model file "/models/en-sent.bin" is located. I think it should have been downloaded onto my computer when the Maven project is built. I tried to search it using my Widows search box, but can't find this file on my local computer. Maybe it is because the maven project is packaged into a Jar file,which contains this file but i can't search it?
public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
return AnalysisEngineFactory.createPrimitiveDescription(SentenceAnnotator.class, PARAM_SENTENCE_MODEL_PATH,
ParamUtil.getParameterValue(PARAM_SENTENCE_MODEL_PATH, "/models/en-sent.bin"),
PARAM_WINDOW_CLASS_NAMES, ParamUtil.getParameterValue(PARAM_WINDOW_CLASS_NAMES, null));
}
Windows does not search inside zip files and jar by default. To enable that you could go to.
start menu>indexing options>advanced >file types>index properties and file contents
Be warned, this might take a very long time to build the index.

Runnable jar exported with eclipse isn't working, in eclipse it still works

I'm trying to export a java project in eclipse as a runnable jar, but for some reason the runnable jar doesn't work. If I double click the executable jar, it doesn't do anything. I tried both extract and package required libraries into generated jar.
So I also tried to export some simpler projects, those worked fine. The biggest difference is my real project has files: images and xml files.
In code reference them like this:
File file = new File("Recources/test.xml");
ImageIcon imageIcon = new ImageIcon("Recources/" + num + ".gif");
The structure of the project looks like this:
But in the executable jar they look like this:
Thank you for your help.
Edit:
I have tried the 'java -jar filename.jar', but now it says it can't find my resources folder, while in eclipse it can still find it.
Files in a JAR-File aren't just like files stored in your hard-disc. If you include files in a JAR, they'll be seen as a Stream of Bytes. So you have to use different methods to access these resources.
//To read/access your XML-File
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.xml")));
//To read/access your gif-Files
ImageIcon icon = new ImageIcon(this.getClass().getResource("/"+num+".gif"));
"/" is not the root-Folder of your file-system, but the root folder of the resources inside your JAR.
The issue may be that Java is not the default program to run the jar.
Try right click -> Open with, and select the Java Runtime, and it should run successfully.
Make it the default program to enable double-click running.
Right click -> Properties -> Change -> C:\Program Files\Java\jre7\bin\javaw.exe
Inspired by stratwine's answer at https://stackoverflow.com/a/8511277
So thank you all, but it seems like the problem wasn't the export only. There was an error I saw when I opened my program with cmd, I was using file name to open xml and images while I should have used inputStreams: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html.

In netbeans, how to save or access an image after deploying jar file

I am trying to make a mess management application in Java using NetBeans. I want to save images of Members in a specified folder inside my src directory. I just created folder named EmpImgs for storing employees images. Here is my code:
File srcDir = new File(file); // current path of image
File dstDir = new File("src\\J_Mess_Mgnt\\EmpImgs\\"+Txt_C_G_M_M_ID.getText());
objm.copyFile(srcDir, dstDir);` // copy image from srcDir to dstDir
Here I use another class for copying images to predefined folders and renaming the images based on their ID.
Everything is working properly in Java IDE.
But unfortunately after making an executable .jar file, this code will not work. I cannot save or access any image file in that directory.
I just went through this site, but I didn't find a suitable answer.
All I need is saving and editing images inside jar folder
Hehe hi mate you need some help. This is a duplicate but I will cut you some slack and maybe you should delete this later. So back to basics, the jvm runs byte code, which you get from compiling java source code to .class files. Now this is different to C and C++ were you just get a .exe. You don't want to give your users a bunch of .class files in all these folders which they can edit and must run a command on the command line, but instead give them what is known as an 'archive' which is just an imutable file structure so they can't screw up the application, known as a jar in java. They can just double click on the archive (which is a jar), and the jvm will call the main method specified in the MetaInf directory (just some information about the jar, same as a manifest in other programming languages).
Now remember your application is now a jar! It is immutable! for the resasons I explained. You can't save anymore data there! Your program will still work on the command line and in IDEs because it is working as if you used your application is distrubuted as bunch of folders with the .class files, and you can write to this location.
If you want to package resources with your application you need to use streams (google it). BUT REMEMBER! you cant then save more resources into the jar! You need to write somewhere else! Maybe use a user.home directory! or a location specified from the class path and the photos will be right next to the jar! Sometimes you might need an installer for your java application, but usually you don't want to create the extra work if you don't need to.
At last I find an answer suit for my question.It is not possible to copy images or files to a executive jar folder.So I used a different Idea.Create some folders(as per our requirement),Where my executable jar folder is located(No matter which drive or where the location is).The code is..
String PRJT_PATH=""; //variable to store path of working directory.
private void getdire() throws IOException{
File f=new File(".");
File[] f1=f.listFiles();
PRJT_PATH=f.getCanonicalPath(); //get path details.for eg:-E:/java/dist
}
private void new_Doc_folder(){ //function for creating new folders
try{
String strManyDirectories="Docs"+File.separator+"Bil_Img"; //i need to create 2 folders,1st a folder namedDocs and In Docs folder another folder named Bil_Img
String SubDirectories="Docs"+File.separator+"EmpImgs"; //same as above but keep in mind that It will not create a Same folder again if already exists,
// Create one directory
boolean success = (new File(strManyDirectories)).mkdirs(); //create more than one directory
boolean success1 = (new File(SubDirectories)).mkdir(); //Creates a single directory
if (success && success1) {
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
It works Successfully.
Regds

How to access class file in android

I want to acess class file from bin folder in android.
I was doing it using
File f = new File("/bin/filename.class");
Its working fine in java but in android giving path doesnt work, so
please suggest me other way to access class file of any java file in android.
You can't access file like
File f = new File("/bin/filename.class");
from bin folder. Android can't recognize this path.
Java .class files are converted to Dalvik Executable (.dex), in your apk there won't be any .class files, just one single dex file called classes.dex.
I think this post from Android Developers blog might help you: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

Categories

Resources