Android: Getting source code from an APK file with R.java - android

I used the instruction here :
Is there a way to get the source code from an APK file?
It was really good. I got almost everything of my lost project but no R.java file. It is nececery to import the project to eclipse. How can I extract this file from apk file? Where can I find it?
tnx.

R.java is created automatically on Eclipse and other IDEs. Removing the line from my main activity that says import R.java usually helps my IDE recognize that it's not there, and it recreates it when you try to run the program. If that doesn't work, try Project, Clean... and try running it again.

You cannot directly extract r.java file, but the contents of r.java file can be extracted using 'aapt' tool. the following command gives list of all resources and their respective IDs as present in r.java file:
aapt d resources name_of_apk

You don't need the R.java file. It is automatically generated by the Android SDK when you try to run your android application.

Related

Missing myProject.R file in android

It seams that I missing the R file, this is a common error and usually I have solved by cleaning the projects, using android tools fix project properties and restarting eclipse.
Now I have installed the new android developer tools eclipse and after importing the project I have this error in eclipse:
Errors occurred during the build.
Errors running builder 'Android Pre Compiler' on project 'MyProject'.
java.lang.NullPointerException
and the R file is not generated so it is missing.
The project is a library project. Thanks
Sometimes if you have errors in an XML file, it will stop the project from building correctly and the resources (project.R) file won't be created properly. You will also need to make sure that you are importing your resource file and not the one from android.R
If you've changed the project build target or minSdkVersion, you might have XML nodes in one of your layout files that isn't supported by the SDK you are using which will stop it from building. That's also worth checking
Go to Project and hit Clean. This should regenerate your R.java file.
Also get rid of any import android.R.* statements and then do the clean up as mentioned.
Otherwise, XML resource under layout may not having the correct naming convention or might be having some capital letters.
Try changing them to lowercase and make sure your layout.xml files don't contains errors.
Clean the project because the project may have trouble finding the name of a resources, it really sucks that i constantly have this problem when adding new image..

android, how to build the project in order to have R.java?

I cleaned my project but after cleaning it doesn't generate R.java file. It is set to build automatically but new R file doesn't generate. Also, I disabled automatic building and manually tired to build but still don't have R file.
Also, I grated same package under gen folder and tried to build R file manually but I was not successful. Why it is like this?!!!
Any suggestion appreciated.
Verify there are no errors in your XML resources. Any error will cause R.java not to compile.

Recover the R.java file

While compiling my project, I lost my R.java file.
I recreated the new project and replaced all my .xml files, .java files, and all my images in the project, but in that project I also lost my R.java file.
What should do to solve this problem? Can I recreate my R.java file for the project?
The R.java file is automatically generated by the Android tools ; so, you should always be able to re-generate it.
In this kind of case, forcing a clean and full-rebuild of the project generally helps.
In Eclipse, try :
Project > Clean
And choose to clean your project.
Copy all the src files to a backup folder.
clean and build the project.
You will get the R file.
Then copy back the files back to src
There might be 2 possibilities:
There might be any error in one of XML files.
Just clean and build your project again.
I have the same problem and it came down to having a resource file named 'case.xml'. It seems that 'case' is a reseved identifier. I just changed the name and that fixed the issue.
Usually "losing" your R.java file happens when you have an error in your XML. The same for the resources.ap_ FileNotFound issue.
Try to have a look in the error list and see if you have any errors in your XML files. Fix them and then re-build as mentioned above.
If you try all the above answer and you still getting the error then check your java file.
If there is any import android.R file then remove that sentense.
again try cleaning and building you project.
I hope this will do.
If you clean project while your .xml file contains an error then it will remove your auto generated R.java file, so don't clean your project while any of your .xml files contain an error.

How to generate R.java

I downloaded few source codes from Google source codes.
I executed them on Eclipse but R.java file is not created and therefore I got many errors saying that R cannot be resolved. There is no main.xml in layout too. Some other XML files are there. How can I execute these programs?
As long as you have a valid Android project, you can use aapt package to generate the R.java from resources. Here's example usage (all should be in one line):
aapt package --non-constant-id -f -m -M <abs_path_to_AndroidManifest.xml>
-S <abs_path_to_res_dir> -I <abs_path_to_platforms_android.jar>
-J <abs_path_to_dir_that_should_contain_gen_R_java)
--generate-dependencies
The above can be made scriptable using ANT by using the aapt target or by simply using an exec target.
Presume you have Android sdk. If you have.. just clean the project. R.java will be autogenerated.
Hi I just faced the same problem. The file R.java is deleted an regenerated each time you clean the project. In my case there were some errors in one of .xml files and no R.java was generated. If you did not have Main.xml that might be the reason. anyway if you fix .xml problems R.java should be generated.
Open Android SDK Manager
Tick Tools that there are 3 packages of Android SDK Tools , delete and install them again.
When they are finished, please refresh your project in Package Explorer.
You will see BuildConfig.java and R.java in /gen
I think you wanna need that I recommend this comment
Enjoy with your building app.
After few hours frustration finally I have decide to remove the red marks from the my file where it was not able to resolve the R.java as it was not actually available in my code. After removing the first red line from my activity file the project builds and generated the R.java automatically.
This is something really freaking thing for me as it goes without any reason and came back with the same thing.
If your build.xml includes the "-resource-src" target (which it will if you include $SDK_DIR/tools/main_rules.xml or lib_rules.xml) you can rebuild it with ant.
For future readers, restart adb then clean.
Correct all errors in the Xml layouts, if the errors is only in the id attribute, change to other name an save, then type the original values, this solution works for me.
I encountered this issue after adding a jpg file with a numeric file name into my drawable folder. I was getting a compilation error, " expected on line y" when trying to rebuild the project after cleaning it. You may want to check that you do not have a similar issue. Although I encountered this while using Android Studio, I am sure it would affect Eclipse as well.
Cleaning the project, delete the R.java and regenerate it.
If you have an error anywhere in your /res file (the resources) , the R will not be generated usually.
It could be an error in the name of a picture , or anything ...
fix your errors before.
good luck ;)
I have encountered similar problem and i changed the workspace folder and all errors disappeared. It's sort of simple solution but it works.

Developing for Android in Eclipse: R.java not regenerating

I've found out that my R.java is never updated, so it doesn't contain information about my new resources, so I decided to delete it and thought that Eclipse would generate a new one. But that didn't happen, and I don't have R.java now. How can I regenerate one?
I'm using Windows 7.
From one of the comments: "Doing Project -> Clean is what caused the problem for me. Cleaning deletes R.java...and for whatever reason the plugin is not regenerating the file."
I found this happening to me with a broken layout and everything blows up. Relax, it's like that old mistake when you first learned programming C where you forget one semicolon and it generates a hundred errors. Many panic, press all the buttons, and makes things worse.
Solution
Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
If you somehow hit something and created import android.R in your activity, remove it.
Run Project -> Clean. This will delete and regenerate R and BuildConfig.
Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
Wait a few seconds for the errors to disappear.
If it doesn't work, delete everything inside the /gen/ folder
If it still doesn't work, try right-clicking your project -> Android Tools -> Fix Project Properties.
Check your *.properties files (in the root folder of your app folder) and make sure that the links in there are not broken.
Right-click your project > properties > Android. Look at the Project Build Target and Library sections on the right side of the page. Your Build Target should match the target in your AndroidManifest.xml. So if it's set to target 17 in AndroidManifest, make sure that the Target Name is Android 4.2. If your Library has an X under the reference, remove and re-add the library until there's a green tick. This might happen if you've moved a few files and folders around.
What to do if R doesn't regenerate
This usually happens when you have a broken XML file.
Check errors inside your XML files, mainly within the /res/ folder
Common places are /layout/ and /values/, especially if you've changed one of them recently
Check AndroidManifest.xml. I find that often I change a string and forget to change the string name from AndroidManifest.xml.
Check that Android SDK Build-tools is installed. Window -> Android SDK Manager -> Tools -> Android SDK Build-tools
Make sure when you update the Android SDK Tools, you also update the Android SDK Platform-tools and Android ADK Build-tools. Build fails silently if they don't match.
If you can't find the issue, right click /gen/ -> Restore from local history... -> tick R.java -> click Restore. Even if it doesn't solve the problem, it will clear out the extra errors to make the problem easier to find.
This site suggests:
if you run a clean on the project it should regenerate all the generated Java files, namely R.
...and...
In Eclipse, under the Project menu, is an option build automatically.
That would help you build the R.java file everytime modifications are
made. The Clean... option is also there under Project.
This site suggests another solution.
I had the same issue. When I checked it out I found that the name of the XML resource under layout was not having the correct naming convention. It had some capital letters. So I renamed it to make all letters lowercase and the magic worked.
One reason the R.class can go missing suddenly is when there are errors in you XML files. For instance, when you add an XML file with uppercase letters in the name like myCoolLayout.xml which is not allowed. Or when you have references that don't point to existing files, etc.
In Eclipse, simply use Project --> clean to clean the project. The
R.java is going to be automaticly (re)-created.
If for some reason that dosn't work: Make sure your layout.xml files don't contains errors. Eclipse seems to be a bit buggy here: sometimes it doesn't mark the errors within the XML nor the package explorer. In such a case: Take a look at the "console" or "problems" view after using "clean". All errors should be displayed there. Fix them and redo a clean.
NOTE: It is NOT neccessary to fix the errors you get because of a missing R file! Just fix the XML files and other project errors and use clean!
Quick fix:
The package name in the manifest needs to be the same as the one in the /src folder, the /gen folder package will be automatically reproduced.
Detailed observation:
Observe the name of package in the /gen folder. In my case it was different than the one in the /src folder.
The package referenced in the manifest was that of the /gen folder.
I attempted to add a package with the name of the /src folder to the /gen folder too see what would've happened, but this did not solve the issue. I proceeded then to remove the package name that was not the same as the package name of the /src folder. This package with the name that did not correspond to the /src folder was recreated as soon as I removed it.
Since the package reference in the manifest corresponded to the one that was being senselessly recreated and that did not correspond with the package in the /src folder, this prompted to rename package = "oldPackage" with the package = "srcFolderPackage".
I had the same issue, and I finally found the problem: In the strings.xml, I did not keep the line <string name="app_name">program-name</string> which is present by default when creating a new Android project. I thought it was only used for the program HelloWord, which is the original program.
However, the AndroidManifest.xml refers to this value, hence:
=> A compile error;
=> the R.java isn't updated;
=> ID are not recognized...
Android has added in SDK build tool ADT 22 for the building mechanism. You just need to do the following steps.
Update Android SDK Tool
Update Android SDK Platform Tool
Update Android SDK Build Tool
Add the path of your build tool to the path variable. (path up to---- YOUR DIRECTORY-PATH\android-sdk\build-tools)
This will solve the issue.
If your R.java isn't getting generated, one of the solutions is to delete the layout file named "blabla.out.xml".
After deleting this file, try cleaning the project from menu Project -> Clean.
As a generalization of Glaux's answer, if you have any errors in the res directory, then R.java may not generate - even if you clean and rebuild. Resolve those errors first.
As an example: when you add an image file of say, "myimage-2.jpg", the system will consider this an error, since file names are limited to alphanumeric values. Do a refresh on your 'res' directory after adding any files and watch the output in your console window for any file name warnings.
If your OS is Ubuntu, I can provide some suggestion:
Install or upgrade ia32-lib:
sudo apt-get upgrade ia32-libs
Check if you have the right permission on the aapt folder:
cd ANDROID/adt-bundle-linux-x86_64-20130522/sdk/build-tools/android-4.2.2
chmod 777 aapt
Start Eclipse:
sudo eclipse
Run Project -> Clean in Eclipse
You 100% have an error in an XML-file, but the XML verification does not show you the error. This is the reason why you need to check your XML files first!
It is ALWAYS helpful to take a look at the Problems Tab in Eclipse. In my case, I was getting a "android unable to resolve target 'android-8'" error message that kept the R.java from being generated. So, I corrected the imported target to the one I was using in the default.properties file, then I performed a clean via Projects->Clean and voila! R.java is automatically generated! Hope it helps!
Cleaning project, closing Eclipse, re-opening it and launching my project finally made my R.java to reappear... Hope it helps
Make sure you are not importing
android.R;
R.java will never be generated if there are any errors in the res folder. For example, in the drawable subfolder there are two files which have the same name, one is icon.png and the other is icon.html.
You can see some error in the Eclipse console log window which is saying "Resource entry icon is already defined.". After deleting icon.html, you can clean or just delete the gen folder. You will find that R.java is created.
I've found that any file that has capital letters in the res folder will create this error. This happened to me with a PNG file I added and forgot about.
After reading through many posts and YouTube videos, I found that each of us have R.java missing for different reasons.
Here's how I fixed this in Eclipse:
Create R.java in gen folder manually and save.
After that go to Project and click "Clean"
The following message will display and your file will automatically be rewritten:
R.java was modified manually! Reverting to generated version!
For me, I had linked v7 appcompat twice. Anyhow, in Eclipse, right click the project name in Package Explorer or Navigator, go to Properties, Android, and uncheck any duplicates in the Library section. You may need to Build --> Clean afterwards.
There's obviously no one final answer to this, but here's another one I don't see here already:
I've had R.java disappear after (attempting) a Team Synchronize, and there were conflict resolution files in the project (i.e., thingy.xml.mine,thingy.xml.r35, etc.). Deleting them/resolving the conflicts regenerated R.
I had this problem. Accidentally I deleted this
xmlns:tools="http://schemas.android.com/tools"
which started causing build errors all over the project in my XML files as well as my Java files. As soon as I retyped what I deleted, it worked again :)
OK so it's clear that there can be a lot of causes for this problem. If you're on a 64 bit linux machine and you are just setting up the ADT for the first time, you may get this problem where R is not automatically generating. Check the console tab and you may see an error similar to:
'No such file or directory' while attempting to get adb version from '/home/patrick/code/android-sdks/platform-tools/adb'
If that's the case you need to install ia32-libs, using something like:
sudo apt-get install ia32-libs
See here for details: Android adb not found
I've came across this problem a few times. I found that if I didn't import the package R through my application's name, for example, if my application had the package name example.test then I found that I had to import example.test.R in order to access any of the resources.
If this wasn't imported then the resources that where getting returned were the default resources with none of my own included.
With that said if you find that you are only getting a list of default resources then just check to make sure that you're importing application_package_name.R and not android.R.
My problem was that appcompat was not in the same directory as my project.
I found this out when compiling my app with Ant.
Also make sure that you have the latest version of the ADT plugin, and open the Android SDK manager to update all SDK tools to the latest version. Check for updates in Eclipse itself as well. Non-matching versions caused the problem for me.
My problem was inside a menu file.
The compiler doesn't seem to warn you if strings which do not exist inside strings.xml are referenced in menu files. Check if your items in your menu XML files reference any strings which do not exist in your strings.xml. Usually in android:title.
For me, this error was caused when I tried to duplicate an existing res/menu instead of creating one with the wizard.
All of these answers could not work if you use Maven. The solution for me was to add
<genDirectory>${project.basedir}/gen</genDirectory>
to the configuration section of android-maven-plugin.
I found a solution why R.class is not made by Eclipse after making it again - 2 clean, build, etc.
The problem is here in strings.xml:
<string name="hello">Hello World, HelloAutoComplete!</string>
<string name="app_name">HelloAutoComplete</string>
These are by default created by Eclipse when you create projects.
Definitely you are changing the strings.xml for your own requirement. Sometimes you clear the string.xmls these two lines from your code:
It is making a problem in the AndroidManifest.xml file:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".HelloAutoComplete" android:label="#string/app_name">
So it can't communicate with strings.xml.
I changed my layout XML file name and found out later that of the XML file (widget provider in this case) still refers to the old layout XML which doesn't exist, and that prevented the auto generation/correction of R class.

Categories

Resources