How do i find where static variable are used in android studio project? Is there any way to find such variables . As my project is too big and i want to remove possible memory leaks. Help me to achieve this. Thanks
I have been using this trick for a very long time now. I am sure this will definitely help you. Follow the instructions step by step and this will help...
If you are using windows, Press Ctrl + Shift + F to open entire project search...
Next, In the text to find add this Regex (public|private|protected) static and under options select case sensitive and Regular expression. This will match public static or private static or protected static.
In the scope, select the project production files
Set the file mask as *.java
Set the context as except comments and string literals
When you hit Find, you will get 1000's of results. You will find results like this:
Completely ignore the usages in generated code.... IMPORTANT
Only search in the found occurences for your static variables.
PS :
One drawback with this is that this will match static methods too... such as public static int randomMethod() { }
If your static variables don't have access specifiers such as public private or protected, just use regex as static .
1.Open your andorid project.
2.Click ctrl+shift+f
You will get a dialog box.
Then search static
You will get all static members and functions.
You can do right-click on said variable and choose Find Usages:
Result will be revealed on Find panel on bottom-right of the window.
Related
As I continue to work on and debug my hobby app to learn/practise Xamarin Android devt, I discovered I produced a version that could be installed on my phone, but it could not be run. (After successful installation, only the Done button could be pressed, the Open button was grayed out)
s'shot of Open button that is grayed out when the public static class is added in the code
There were a few changes since the last version that could be run, and having narrowed it down, the problem is the definition of a public static class in order to define some global variables.
public static class GlobalVariables1
{
public static string TdyAlertTime1 = "11";
public static string TdyAlertTime2 = "15";
public static string TdyAlertTime3 = "21";
}
This was adopted having followed How to declare global variable. I have tried making adjustments to that but still not resolving.
I also tried other methods found on the Net to no avail:
Store data and global variables using the Application object
public class HelloApplication : Application
{
public static int GlobalVariable = 1;
}
I also saw SharedPreferences and Application Properties Dictionary but the latter seemed applicable for certain functions eg. OnStart; and the former, I had poor experience before when trying to develop a Preferences screen.
But back to my original intention. I want to define simple global variables or constants (actually for debugging purposes currently, but learning it would be useful for other purposes too) to be utilised in two different classes in my project.
What could be wrong with the method I adopted (to cause the inability to run)? Or what method could I really use in a simple manner?
Thanx & Rgds
I'm just starting with programming Android apps and was working through the developers guide on android.com. When trying to display a text on a second activity it says it cannot resolve the symbol EXTRA_MESSAGE as you can see here:
As far as I can tell I did every step like the guide says. I also tried copy and pasting everything but it still doesn't work. What am I missing?
You're doing a static import of extra message from some random class in MainActivity. That's wrong, don't do that. Define EXTRA_MESSAGE as a public final static String, with whatever value you want (I'd suggest "message") in your MainActivity.
There is two ways of solving it.
1) Use same static variable (Its a dirty way). At DisplayMessageActivity use android.provider.AlarmClock.EXTRA_MESSAGE.
2) This approach I recommend you. Create public static final String field at MainActivity, remove android.provider.AlarmClock.EXTRA_MESSAGE, and use MainAcitivity field at both classes. Content of this variable does not matter as long as it is unique extra key.
I've searched around SO for this and found a few things, but I'm still not sure I fully understand, so I ask you for clarifications.
Here is what I need:
Have a project that has specific function: interrogate web service, display results in different views
Have a second, third and forth project that has exactly the same functionality as the first one, but only different graphic elements like splash screen image, icon, name, package name.
So, I have ProjectCore with activities and functionality. Project1 with a car icon and car image for splashscreen. Project2 with airplane icon and airplane image for splashscreen. Something like that. Each projects has a class with constants like'appId, appName, appServerURL"... All the web service call, data display is in Core as it's the same for all prohects, only the read is made from Constants class.
I was thinking of this approach
Make ProjectCore a Library project with a package like com.domain.core and dummy images
Make Project1, add reference to ProjectCore in it and with a package like com.domain.code.project1 and in resources folder, put images with same name as in core project
Make Project2 on the same principle like project1
Will this approach work ?
Thanks.
Later Edit. I've tried as mentioned before. For instance in Core project I had in drawable a file called splash.png. In Project1's and Project2's drawable folder I've put spash.png file with other images. This works fine. Running the Project1 and Project2 on my phone, started each app with it's own image. So far so good.
Then, because I have different constants I need to use in my App, I went into Core library project and added:
public class C {
public static String SomeConstant = "Project core!";
}
Here comes the problem, I need to have different constant values across Project1 and Project2. Because on Core project, the class is in com.domain.core.utils for instance... I can't add the same package in Project1 and Project2. How do I add the classes so I can update their values and be used on each project with particlar values ?
public class C {
public static String SomeConstant = "Project 1 constant!";
}
public class C {
public static String SomeConstant = "Project 2 constant!";
}
Thank you!
You want to create your functionality in a Library project and then have all of your Branded/OEM/3rdParty projects extend from this, overriding images and string resources where necessary.
When you need to use "Constants" you should instead have a single "run once" portion of your code (such as a splash screen) load these strings from resource files:
public static final String CONSTANT_ONE;
public void onCreate() { CONSTANT_ONE = getResources().getString(R.String.CONSTANT_ONE); }
EDIT
I'm unsure on how initialising a final value on onCreate() will perform. If final doesn't work well and you're worried about changing the variable during program execution then make the variable private (so only that class can assign to it) and then create a public static String getConstantOne() function.
Yes. Library projects are ideal for this, especially if only resources differ. I've used the exact approach that you've outlined with success...
Yes this should work fine. I did something a bit similar and I found occasionally you may have some circumstances where you want to call out from your library project to your application project. In these cases I used interfaces/abstract classes defined in the library project but implemented in application project...
When using DDMS in eclipse to simulate location data I manually input the lat/lng value. Can I set default values for them so that I don't have to do it over after restarting my eclipse?
You can simply create command line batch file that will fix geo location. You can read how to do this here and here.
Update:
I've searched through the source codes of ddmlib. We can only laugh from the comment:
public class EmulatorControlPanel extends SelectionDependentPanel {
// default location: Patio outside Charlie's
private final static double DEFAULT_LONGITUDE = -122.084095;
private final static double DEFAULT_LATITUDE = 37.422006;
However, if you want you can change this values and rebuild the sources. This class is situated here:
android/sdk/ddms/libs/ddmlib/src/com/android/ddmlib/EmulatorConsole.java
I'm not very good at reflection, maybe it is possible to change this values through it.
I'm not sure but I think it is possible to add ddmlib.jar to your application and in your application set the values for yours location.
Good luck! If you manage to do this, please, write about your experience!
If I know a variable's pattern such as R.id.edit_x where x (1..N), how can I get a reference to a given EditText, like findViewByID(R.id.edit_1). Is there something like an "eval" function in Dalvik ? Thanks.
Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection
hoha's answer is good. Another thing you can do is create a look-up table that maps 1..N to the resource IDs. (Presumably you know all the resource IDs ahead of time.)
maybe, you can check roboguice. it is a ioc framework for android and it's realy easy to use. i copy some code from the sample from the project to show how to use it:
public class AstroboyMasterConsole extends RoboActivity {
#InjectView(R.id.self_destruct) Button selfDestructButton;
#InjectView(R.id.say_text) EditText sayText;
#InjectView(R.id.brush_teeth) Button brushTeethButton;
#InjectView(tag="fightevil") Button fightEvilButton; // we can also use tags if we want
}
then you can you these injected variables in your code!