AppCompatActivity.onCreate can only be called from within the same library group
Asked Answered
D

3

129

After upgrading to appcompat 25.1.0 I've started getting weird errors.

In my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

I get lint error:

AppCompatActivity.onCreate can only be called from within the same library group (groupId=com.android.support)

How to prevent such behavior?

Dumbstruck answered 14/12, 2016 at 19:49 Comment(3)
replace invalidateOptionsMenu() with supportInvalidateOptionsMenu(). it should do the jobPaba
I don't use such methods in my codeDumbstruck
Sounds like it's a bug: code.google.com/p/android/issues/detail?id=230387Isomerous
I
107

As Felipe already pointed out in his comment this is a bug in the pre-release version of the tools.

You can workaround it for now, until Google release a fix, by adding the following into your project module's build.gradle file:

android {
  lintOptions {
    disable 'RestrictedApi'
  }
}

It's worth noting that this may hide true errors in your project as it suppresses all errors of that type, so the better option would be to downgrade the version of Android Studio and the tools used in the project.

Indemonstrable answered 20/12, 2016 at 21:11 Comment(1)
I’d suggest @Cbr’s answer below, it only suppresses at the method level, and it works on Android Studio 3, unlike //noinspection RestrictedApi.Bartlett
L
165

As previous responses highlighted, it is bug. I recommend not to disable the specific lint warning project-wide, but for that method only. Annotate your method as follows:

@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    //your code here
}
Lectureship answered 5/7, 2017 at 12:55 Comment(5)
This answer is best because it doesn’t disable globally, it solves the error/warning in the IDE and at compile time, it’s easy to understand because it’s explicitly listed at the top of the method (unlike //noinspection RestrictedApi which is hidden as a comment, and only silences the warning in the IDE, but not at compile time), and it works with the newest version of Android Studio and Gradle.Bartlett
This answer should be used. Never good idea to do a global disable on the project.Inenarrable
You can also use this annotation on only the line you need it on instead of suppressing for the whole methodRoush
Thank you, that worked for me. In my particular case the error was solved with applicationContext: @SuppressLint("RestrictedApi") fun toastString(string: String){ var context = getApplicationContext() Toast.makeText(context, string, Toast.LENGTH_LONG).show() }Truancy
@BenKane: What's the syntax to use the annotation on only one line instead of suppressing for the whole method? I tried this: if (menu instanceof MenuBuilder) { @SuppressLint("RestrictedApi") ((MenuBuilder) menu).setOptionalIconsVisible(true); } but it shows an error prompt saying: "Annotations are not allowed here."Benne
I
107

As Felipe already pointed out in his comment this is a bug in the pre-release version of the tools.

You can workaround it for now, until Google release a fix, by adding the following into your project module's build.gradle file:

android {
  lintOptions {
    disable 'RestrictedApi'
  }
}

It's worth noting that this may hide true errors in your project as it suppresses all errors of that type, so the better option would be to downgrade the version of Android Studio and the tools used in the project.

Indemonstrable answered 20/12, 2016 at 21:11 Comment(1)
I’d suggest @Cbr’s answer below, it only suppresses at the method level, and it works on Android Studio 3, unlike //noinspection RestrictedApi.Bartlett
C
69

Disabling the warning in lintOptions doesn't look a good option it's better to suppress inspection at the statement level.

Add this comment above the line of code which gives the warning:

//noinspection RestrictedApi
Chaunce answered 29/3, 2017 at 14:37 Comment(9)
I'm going with this one. I might be the better solution not to disable the lintOptions globally. ThxShetrit
Doesn't work in Android Studio 3. Were there some changes ?Numerary
can't say that as I am using the latest beta for all my projects. And didn't find any problemChaunce
@Numerary I use @SuppressLint("RestrictedApi") instead of //noinspection RestrictedApi with no problems in Android Studio 3Amboina
Yes sure, that one works for me too but the //noinspection works for one statement only which is better.Numerary
@vovahost, but that //noinspection RestrictedApi Doesn’t work with Android studio 3.... Use @Cbr’s answer below if your on Android Studio 3.Bartlett
@Bartlett The Cbr's solution applies to the whole method which I didn't want to use because it may hide other errors.Numerary
@Numerary Ok, do you have any useful suggestions or input? Also using @SuppressLint("RestrictedApi”) on a method will ONLY suppress lint errors for “RestrictedApis”. So it shouldn’t be an issue at all... you’ll still receive other errors that aren’t “RestrictedApi” errors, so really using @SuppressLint("RestrictedApi”) isn’t an issue and it should be the new answer.Bartlett
What if you do use other RestrictedApis though?Monstrosity

© 2022 - 2024 — McMap. All rights reserved.