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

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

Related

Android SDK tools + Flutter: correct directory structure and environment variables

I am having the damnedest time getting Flutter to find the SDK command-line tools. I am trying to install the latter by downloading the zip provided at that link and unpacking it in say ${HOME}/Android. This produces a tools directory, and if I run the executable
${HOME}/Android/tools/bin/sdkmanager
directly (whith any number of options, etc.) I always get an error as documented in this other post. The solution recommended there actually works: move the tools directory further down the tree to get the path
${HOME}/Android/cmdline-tools/tools/bin/sdkmanager
That new intermediate directory must be named cmdline-tools though; I've tried other things (Sdk, sdk_manager, etc.) to no effect: the same errors occur.
Now, poking around in the cloned Flutter repo, I see in the file dev/bots/download_android_sdk.sh that I'm supposed to have the executable
$ANDROID_SDK_ROOT/tools/bin/sdkmanager
This tips me off that in my setup I need
ANDROID_SDK_ROOT=${HOME}/Android/cmdline-tools
exporting that and running a bunch of sdkmanager installation commands (like say
$ANDROID_SDK_ROOT/tools/bin/sdkmanager platform-tools
and the like) I see that my ${HOME}/Android directory is starting to get populated with the corresponding directories platform-tools, build-tools, etc. So those newly-installed directories live at the same level as my $ANDROID_SDK_ROOT which remember, as I'm doing this, is set to $HOME/Android/cmdline-tools.
On the other hand though, when I then go look (in the same Flutter repo) in the file
packages/flutter_tools/lib/src/android/android_sdk.dart
I see that I'm supposed to have the directory $ANDROID_SDK_ROOT/build-tools. This is in direct contradiction with the previous observation that build-tools and the like are supposed to be at the same level as $ANDROID_SDK_ROOT, and not under it.
My problem is I don't know how to square this apparent self-contradiction in the Flutter codebase. Which is it? In my setup, is ANDROID_SDK_ROOT supposed to be $HOME/Android or $HOME/Android/cmdline-tools?
PS
I understand that $ANDROID_SDK_ROOT is supposed to be "the SDK installation directory", but in this context this tells me nothing: after all, installing SDK is precisely what I'm trying to do.
You got a couple comments on GitHub as well, but just be aware that the files in the dev/ folder in the Flutter repository are mainly intended for CI purposes and for people who are developing the Flutter framework itself.
The scripts you're pointing out are actually stale files that used to help set up some tests we ran on CI. We now set those tests up differently, but forgot to delete the files - so thanks for pointing this out, they're soon to be gone!
The best way to set up your Android SDK is by following the instructions at the Android Studio site. You can either use Studio to do it, or just download the SDK bundle(s) themselves.

Java Error When Enter To Eclipse [duplicate]

When i am starting my eclipse i am getting this exception.How could i resolve this.
!SESSION Thu Aug 05 12:52:23 IST 2010 ------------------------------------------
!ENTRY org.eclipse.equinox.launcher 4 0 2010-08-05 12:52:23.805
!MESSAGE Exception launching the Eclipse Platform:
!STACK
java.lang.ClassNotFoundException: org.eclipse.core.runtime.adaptor.EclipseStarter
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:556)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
at org.eclipse.equinox.launcher.Main.main(Main.java:1287)
I cleared my workspace also but still i am getting same problem.
I add one plugin name called tomcatPluginV321.zip.I extracted this in plugin folder.
then i am facing this problem.
In your config.ini file of eclipse eclipse\configuration\config.ini
check this three things:
osgi.framework=file\:plugins\\org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar
osgi.bundles=reference\:file\:org.eclipse.equinox.simpleconfigurator_1.0.0.v20080604.jar#1\:start
org.eclipse.equinox.simpleconfigurator.configUrl=file\:org.eclipse.equinox.simpleconfigurator\\bundles.info
And check whether these jars are in place or not, the jar files depend upon your version of eclipse .
I just went through this. If you want to manually move your Eclipse installation you need to find and edit relative references in the following files.
Relative to Eclipse install dir:
configuration/org.eclipse.equinox.source/source.info
configuration/org.eclipse.equinox.simpleconfigurator/bundles.info
configuration/config.ini
eclipse.ini
For me in all these files there was a ../ reference to a .p2 folder in my home directory. Found them all using a simple grep:
grep '../../../../' * -R
Then just hit it with sed or manually go change it. In my case I moved it up one folder so easy fix:
grep -rl '../../../../' * -R | xargs sed -i 's/..\/..\/..\/..\//..\/..\/..\//g'
Now Eclipse runs fine again.
This can a time consuming problem. For those who want to get on with their work because they do not want to waste much time, I would like to suggest that you download the zip file and unpack the zip file and use it. The zip file is available for all major OS.
https://www.eclipse.org/downloads/packages/
Here instead of hitting the Download button, which will download the Eclipse installer, scroll to the middle area where a list of the various Eclipse IDEs with their respective features are shown. Select the Eclipse IDE of your like and click on the link on the right-hand-side to download the zip or corresponding file for your OS version.
If you still want to use your other Eclipse installations because, for example, you have plugins installed, then download the Eclipse installer and on the right-hand top corner press the icon with 3 minus. After that press Update, then after restart, install the version of Eclipse IDE, which you want (the one that you want to reactivate) in a different folder. The installation will take some time. After the installation is finished you should be able to start your old Eclipse IDE.
Just copying the content of the zip-file to its prefered location from the zip-file will give you this error when you attempt to run the only executable that is visible in the archive. It is named similarly but it is not the real thing.
You should let the archive extract itself to make the installation complete correctly. Doing so gives you an executable named eclipse.exe with which you will not get this error.
check jar files in your project which are mentioned in config.ini if not proper then install manually and then follow the following steps:
Select your product configuration file, right-click on it and select Run As Run Configurations
Select "Validate plug-ins prior to launching". This will check if you have all required plug-ins in your run configuration.
If this check reports that some plug-ins are missing, try clicking the "Add Required-Plug-Ins" button. Also make sure to define all dependencies in your product.
And your application start running
I have the same error after attempting to update Android Development Toolkit (ADT) plugin for Eclipse 3.5.
I haven't figured out what caused this but I re-installed (unziped Eclipse) to fix it.
my case seems to be invalid JRE version.
1.7+ required while I launched with 1.6
plus: I filtered some plugin jars which might be required. so changed to select all
You might be launching your application from a Product file which is not linked to the plugin file. Reset your workspace and launch using the MANIFEST.MF > Overview > Testing > Launch.
I had the same issue.
just removed all my worskspace:
C:\Users\<name>\.<eclipse similar name>

Installing SDK and NDK to start coding android apps with python

http://python-for-android.readthedocs.org/en/latest/toolchain/#introduction
Please do not mind because this not really related to SO. I aint getting replies in askubuntu that is why I am posting this question. Please do answer. it will be much appreciated.
refer the link above, I downloaded the SDK and the NDK, i run the SDK manager and I installed the required files. This is enough as a platform right? or I need to download ADT or eclipse?
I installed cython also as given in the doc. What I want to know it, if I install eclipse, which version should I first download? because there is no eclipse in particular for python.
Secondly, after setting up any platform(in my case, its just SDK manager, not eclipse) it says
'after installing them export both installation path, NDK version and API to use' what does it even mean?
I remember, before installing or running sdk manager, I opened .bashrc file, put up 2 lines,
export path=$path:......android-sdk
export path=$path........android-ndk
saved it.
I do not know whether the SDK manager started working because of that, but it works and my emulator also works after creating a vew avd.
I am totally stuck at the exporting of installation paths, versions and apis. Where do I export them to?
And if you go futher down, you can also see 'configure youre path to add android binary:' what does this mean? I have a ubunu 12.04, I need to set environment variables?
Then further down, it says 'USAGE',
./distribute.sh -m "kivy"
When i run it, it says distribute : no file or directory. Well, I have just downloaded kivy and extracted I havnt done anything more to it. What am I supposed to do. Please help me here. I really need it. Thank you
I can't give all the details on the individual bits of the build process you might have done wrong, but rather than doing things this way you can use the buildozer tool. If you follow the instructions to install it, it automatically downloads and links all the dependencies (including sdk/ndk/python-for-android/python/kivy), reducing the build process for a kivy app to a single command.
Then further down, it says 'USAGE', ./distribute.sh -m "kivy"
When i run it, it says distribute : no file or directory. Well, I have just downloaded kivy and extracted I havnt done anything more to it.
You would need to run that command from within the directory where you unpacked python-for-android. There would be a file in that directory called 'distribute.sh'.
Also, you don't need to download kivy yourself, python-for-android does this automatically as part of its distribute process. You do need to install your own version of kivy to use it locally, which I assume you've already done?
I assume you are making a kivy app? That is what these instructions are designed for.

Trying to execute/debug an existing APK file with Eclipse

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.

using Eclipse for Android development - cant find jarsigner.exe4

Now I am at the stage of preparing my android app for publishing I discover that jarsigner.exe is not on my hard disk ( done a full search) I have the jave sdk and java jre and keytool.exe but no jarsigner. I have googled and followed thro links that appeared to offer download of jarsigner but none of them acftually do. I have been to the official java site but cant find an option to download this file - can anyone help?
I am using Eclipse and thought that when I did the various downloads for Eclipse, Android and Java I would have all the necessary files.
From the reading I have done it seems that the Eclipse export wizard only prepares debug signed .apk and I must follow the unsgned/keytool/jarsigner route to get a distrbutable file.
You don't need it.
Right-click on the project in the Package Explorer view, in the popup menu choose Android Tools/Export signed application package.
In the directory of the JDK (not JRE).
%JDK_HOME%\bin\jarsigner.exe
You can download it from here - http://www.oracle.com/technetwork/java/javase/downloads/index.html

Categories

Resources