The problem:
#Nullable
private View view;
public doSomethingWithView() {
checkNotNull(view); //<- this throws NPE if parameter is null
view.showWarningAboutIssue(); //<- IDE reports about possible null here
}
Is there a way to configure the IDE, so that it doesn't report for a possible NPE on the second line?
UPDATE:
I'm using Dagger checkNotNull method which is identical to Guava ones. If I change import from Dagger to Guava my IDE removes the warning.
UPDATE #2
With latest update of Android studio I can not reproduce it anymore
You need to add the following comment before view.showWarningAboutIssue() statement:
//noinspection ConstantConditions
This can also be performed by the GUI : Alt+Enter (or "bubblelight" menu), then choose Assert view!=null and then Suppress for statement:
Related
I work in the Android 4.1.2 studio. I updated Gradle plugin
- classpath 'com.android.tools.build:gradle:3.5.3'
+ classpath 'com.android.tools.build:gradle:4.1.2'
and
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
After that, I tried to build the apk, but the build failed due to errors in kotlin class, for example: "'handleMessage' overrides nothing " for Handler class, and "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type" for intent.getParcelableExtra. The Android studio itself does not show me such errors in kotlin class. Can you tell me what this is related to and how I can enable the display of errors in android studio
I have tried rebuild, clean , invalidate & restart.
Click the "Handler" to open the Decompiled Handler.class, search "handleMessage" in it, the new definition looks as below:
public void handleMessage(#NonNull Message msg) {
throw new RuntimeException("Stub!");
}
So the argument msg has been changed to #NonNull.
Therefore, to fix this issue, the original override statement
override fun handleMessage(message: Message?)
should be updated to remove the ? to match the function definition.
override fun handleMessage(message: Message)
I write a Groovy script and I need to access a field that has a private access modifier. This works fine but generates a warning:
Access to <field_name> exceeds its access rights
I want to supress this warning. What value of #SuppressWarnings annotation do I have to use in order to achieve it?
I've tried #SuppressWarnings("all") and it works fine but I would like to have more granular control over suppressions. I've also tried #SuppressWarnings("AccessingNonPublicFieldOfAnotherObject") but it has no effect.
Below how this warning looks in IntelliJ:
Unfortunately automatic hint doesn't suggest any sensible solution:
If you are talking about IntelliJ warning:
then you can hit Alt+Enter on the offender and choose "Suppress for method" as follows:
after which you will see #SuppressWarnings("GroovyAccessibility") over your test method:
I had this "problem" when I had buildSrc folder in Gradle-project which contained Kotlin code for my Gradle build scripts and it was used in Groovy code.
Seems like it was because I was accessing private variable field and not the getter. Kotlin makes field private by default. I solved this by using #JvmField in Kotlin code which makes the variable field public and does not generate getter for that field.
// groovy in gradle build script
compileOptions {
sourceCompatibility(CompileOptions.javaVersion)
}
// kotlin in buildSrc (this gives the warning)
object CompileOptions {
val javaVersion = JavaVersion.VERSION_11
}
// kotlin in buildSrc (this does not give the warning as field is now public)
object CompileOptions {
#JvmField val javaVersion = JavaVersion.VERSION_11
}
Ok, I have some fictional class
public class TEMP {
String data;
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (data != null) {
}
}
};
}
And it looks like this in Android Studio:
If I collapse all - it looks ugly:
If I remove that if block inside onClick() - it looks good:
Why is this happening and how can I solve this issue?
It's been a while, so I'm not sure how helpful this is.
This is folding the function into a lambda-like visual structure with Java 8 lambda-notation. Single method interfaces are (almost) equivalent to lambdas in Java 8. Android Studio currently does NOT support Java 8 lambda notation (details found at the bottom of this answer: Is it possible to use Java 8 for Android development?).
A workaround IS available for Java 8, based on the Jack toolchain. Here is the the workaround, and here is the deprecation of the Jack toolchain.
Another note is that according to here and here, Android Studio 2.4 preview 4 and later appears to support actual lambda notation. Note that these are previews, and the latest official version is 2.3.2.
As for how to fix it, I don't believe it is possible. You may be able to use the region feature to duplicate what you are trying to achieve. The link above is a how-to sort of thing.
When running unitTest on AndroidStudio (1.4), my below test fail as per title i.e. java.lang.NoSuchMethodError: org.assertj.core.api.Assertions.assertThat(Ljava/util/Map;)Lorg/assertj/core/api/MapAssert;
#Test
public void mapTest() {
Map<String, String> map = new HashMap<>();
map.put("Key1", "Value1");
map.put("Key2", "Value2");
Assertions.assertThat(map).isNotNull();
}
When I run it from command line using gradlew command, it passed. I'm already pointing my Java to jdk1.8.0_66.jdk. Anyone knows why?
(p/s: I get the latest AndroidStudio 2.0 Preview 5, my test fails massively. So I decided to stay with the older version of Android Studio).
Trying to search for help and found this link https://github.com/joel-costigliola/assertj-core/issues/395. Apparently he also face the same issue on IDE. Any clue what's the cause?
I would double check the classpath.
Use assertj core 1.7.1 - it's a version depending on java 6 whcih I'm sure is Android compatible.
Hope it helps
i have a pojo class
run this code
Field[] fields = clazz.getDeclaredFields();
i got a field under Android Studio IDE :
its type is interface com.android.tools.fd.runtime.IncrementalChange
its name is $change
My Android Studio version is 2.0 Preview 4
the pojo class which i definded by myself didn't have $change field
when i run the code in eclipse, it works normal.
where did the field come from?
how can i avoid this field , is there some setting in Android Studio ?
Instead of turning off instant run we can resolve this issue by utilising synthetic modifier check. 'com.android.tools.fd.runtime.IncrementalChange' is synthetic so we can check whether the field is synthetic using isSynthetc method.
Field[] fields = objClass.getFields();
for (Field field : fields) {
String name = field.getName();
Object value;
if(field.isSynthetic()){
continue;
}
//add your code here
}
Most likely this field is added in order to support the Instant Run feature added in Android Studio 2.0, and will not appear if you turn off Instant Run.
I think diordna answer is best link。install run is Android Studio new function,I won't close it。
I use JsonTool link in my sdk library ,when I run my app with androidStudio2.2 JsonTooll.objectToJson() give me a bad json String ,I add code
if (name.contains("this$") || field.isSynthetic()) continue;
solve it