Is there any way to get the version code from my project settings to use on my About screen?
Project structure/app/Flavours has both a Version Code and a Version Name. Is there any way I can "inject" either one into a text field that appears on my About screen? I'm thinking of the same technique you use to put variables in other text messages, like "Order number %1$s has been deleted as per your request" which gets filled in via
getString(R.string.message01, orderNumber)
or something similar.
If the technique for the version code is the same, what would I use to get the version code or name in the above code?
I'm running Android Studio 3.1.4.
You can access your version name and code by using the BuildConfig class.
int code = BuildConfig.VERSION_CODE;
String name = BuildConfig.VERSION_NAME;
Or as you want to use it:
<string name="version_example">App version: %1$s</string>
getString(R.string.version_example, BuildConfig.VERSION_NAME)
The BuildConfig class gets auto-generated at compile time depending on your Gradle config. You can also use it to define custom fields, see https://developer.android.com/studio/build/gradle-tips#simplify-app-development
Related
I have a value for a String in String.xml
<string name="id">4</string>
and I have a class which contains a variable
public static int Id=1;
Now what I need is I want to get either of these two values in the gradle, which will check a condition and based on the condition it will rename my app. Below given is the part of the gradle code
def identifier //here i need to get value from the java or xml
switch(identifier)
{
case 1:
temp="ApplicationNewName";break;
}
newName=newName.replace("-release",temp);
output.output.File=new File(output.outputFile.parent,newName);
My question is that, Can i access the variables initialised in the java file or string xml in gradle file ?
You're approaching this problem backwards, Gradle gives you the ability to set those variables within the script itself and then you can further access those variables throughout your Android code. Here's a relevant answer for how you can set build configuration variables: https://stackoverflow.com/a/17201265/2168085
It also sounds like you are trying to build different apps from a single code base, or build variations of those apps. If that's the case then you should really look into build flavors to solve this problem. Essentially a build flavor allows you build different apps from a single main code base and apply variations or new functionality to the different flavors. This can be as basic as having a free and paid version of an app or a full white label code base where you can build very different apps from the same master code base. In Android these are more commonly known as build variants and the developer documentation gives plenty of good information on how to get started: https://developer.android.com/studio/build/build-variants.html
I have been reading about ignoring hardcoded string from Log:
Log.d(TAG, "onBindViewHolder: ");
Even inside a Toast:
Toast.makeText(context,"Hardcoded text",LENGTH_SHORT).show();
Sources:
How can I find all RELEVANT hard coded strings in Android Studio?
Android find all hardcoded strings in code using Android Studio
In the first source there is the best approach but somthing is not working and that is:
"adding #SuppressLint("HardcodedNonLog") to the top of the class definition"
Is there a way to ignoring these kind of hardcoded strings?
UPDATE
Create a Gradle task to add "//NON-NLS" to every Log, could be a good way? If the answer is yes, how could I do that?
Yes, you can!
At least with Android Studio 3.0 canary 9 (not checked with previous version).
There is option like "Annotate class 'Log' as #Non_Nls"
After that will be generated file at path "$modulename/android/util/annotations.xml" with this content:
<root>
<item name='android.util.Log'>
<annotation name='org.jetbrains.annotations.NonNls'/>
</item>
</root>
With this file all strings mentioned in Log will be ignored for this module.
Hardcoded text is not an error it is a warning which can be dealt with by pressing left alt+ Enter. When you press alt+Enter you are just defining that hardcoded string to string.xml in values folder.
I wasn't sure exactly what headline to give this question because I'm not sure what this is technically called. In Android Studio while typing out Espresso tests I noticed that it refuses to accept something like this:
onView(withId(R.id.someId)).perform(click());
and instead will only accept this:
Espresso.onView(ViewMatchers.withId(R.id.someId)).perform(ViewActions.click());
even though every example that I've seen online shows the first example as correct code. Why is Android Studio forcing me to preface every ViewMatcher/Espresso/ViewActions/etc. method with the classname even after the imports are included in my class?
To clarify - trying to use the first example shows "cannot resolve method" and using autocomplete on it (which I have to do several times before it will work) invariably autocompletes to the second example. In all the "regular" code for my project autocomplete works correctly and short method names are recognized. I've tried doing a "clean" and "invalidate cache and restart" but no change.
Example of Google doc that shows usage according to the shortened code:
https://developer.android.com/training/testing/ui-testing/espresso-testing.html
You can try to check out your Android Studio Preferences for imports. Just go to Preferences -> Search "imports"
Here are the settings that I use and I don't have that issue:
What fixed the issue for me: just removed red withId in one place. Then just re-entered "withId": after that all the other red withId were replaced with black text color withId and now it compiled successfull!
Im trying to build the Contacts app from AOSP as a Android project in Eclipse. I can't find the reference
Calls.VOICEMAIL_TYPE
in code CallLogFragmentTest.java
private void insertVoicemail(String number, long date, int duration) {
Object[] values = getValuesToInsert(number, date, duration, Calls.VOICEMAIL_TYPE);
// Must have the same index as the row.
values[CallLogQuery.VOICEMAIL_URI] =
ContentUris.withAppendedId(VoicemailContract.Voicemails.CONTENT_URI, mIndex);
insertValues(values);
}
Even in my other Android environment the variable VOICEMAIL_TYPE does not exist?
Has someone done this, if so exactly what libs did you use?
VOICEMAIL_TYPE is found in CallLog.Calls, as you can see from the source.
Here are two reasons I can think of for why you are missing this:
You do not have all the right source code (i.e., your copy of CallLog does not have VOICEMAIL_TYPE)
You actually set up an Android project (with an android.jar as part of your build path), as VOICEMAIL_TYPE is marked as #hide, so while it is part of an OS build, it is not part of the Android SDK
There may be other reasons as well.
CommonsWare pointed me out in the right direction and I solved it by building the AOSP and then I got out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes-full-debug.jar and added it first in my build path in Eclipse
I am trying to enable push notifications on my application.
I have the client sample code and now i am trying to get some of the code to implement it on my application. For those of you who have already implemented push notifications on your apps i am sure you are familiar with the class GCMIntentService.
So i am copying this class on my project , i configure the Manifest correctly(or i think i do) but i get errors in the file.
The errors are in expressions like R.string."something"
For example I get errors in :
R.string.gcm_registered
R.string.gcm_message
R.string.gcm_deleted
R.string.gcm_error
R.string.gcm_recoverable_error
R.drawable.ic_stat_gcm
The error is always that gcm cannot be resolved or is not a field.
But what exactly is this expression?! Is it a class or something?
Is it something on the Manifest that I haven't configured correctly?
It are strings, probably in the example app you're using, in /res/values/strings.xml
You put strings in there to have a localized place to put your user visible Strings in, so you have
1. One place to find all your strings, if you ever want to change and/or re-use
2. Easy access to translation by putting translations in per example /res/values-de/strings.xml
Look for that file in whatever example you're using, you'll find some XML defined strings. Copy them into your own project, in the same place.
this may helps you ,define all Reuired String in your strings.xml and save
R.string.gcm_registered
R.string.gcm_message
R.string.gcm_deleted
R.string.gcm_error