java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.checkSelfPermission [duplicate] - java

I am trying to make runtime permissions backward compatible with older devices but using the support library i cant find checkSelfPermission() static call in ContextCompat class, but its documented here
Here is my project gradle settings:
defaultConfig {
applicationId "org.myprogram.cool"
minSdkVersion 16
targetSdkVersion 23
versionCode 39
versionName "3.0"
}
and here is the dependencies:
compile 'com.google.android.gms:play-services:+'
compile 'com.squareup:otto:1.3.5'
compile 'com.android.support:appcompat-v7:22.1.1'
any idea what i am missing ?

checkSelfPermission() didn't get introduced into the native Context until API 23. So you will need to use at least version 23.0.0 of the support library.
You should change this
compile 'com.android.support:appcompat-v7:22.1.1'
to this
compile 'com.android.support:appcompat-v7:23.0.1'

If you have migrated from eclipse adding the below line wont work
compile 'com.android.support:appcompat-v7:23.0.1'
While migrating, it adds appcompatv4 as an external dependent library.
Make sure you have deleted the appcompatv4 library from the libs folder and then it should start working

Make sure class exampleActivity class is extending from AppCompatActivity.
Make sure that your minSdkVersion is 23 in the build.gradle file.
Sync your gradle after making this change.
That should work.

Related

How to increase target api level successfully

I want to increase the target api level of my app without affecting other libraries that the app depends on, such as firebase and alike. I tried changing it in project structure under app module but this did not work out. Nothing changed in gradle file
My next option is to increase it by editing the gradle file itself and increasing to api level 28. I am however scared of the effects this may have on the other libraries.
Below is my build gradle file, please assist me.
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.chomba.haroldking.kuta"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation ' com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.firebaseui:firebase-ui-database:3.1.0'
implementation 'com.android.support:palette-v7:26.1.0'
implementation 'com.wdullaer:materialdatetimepicker:3.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.google.firebase:firebase-storage:16.0.1'
}
apply plugin: 'com.google.gms.google-services'
Do I need to make changes to the other libraries
To upgrade your targetSdkVersion you should update it in build.gradle together with the compileSdkVersion (you will get an error if you don't update it as well).
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
}
}
Once you update the targetSdkVersion and the compileSdkVersion you'll also need to update the support library versions to 28.0.0, because the support library's major version needs to match the targetSdkVersion. For example:
implementation 'com.android.support:appcompat-v7:28.0.0' // instead of 26.1.0
Version 28.0.0 is the final version of the support library. To keep using the support libraries after you upgrade to API 29 you should migrate to AndroidX.
When updating you should also check if there have been relevant changes in Android 9 that you need to consider.
In your case you cannot update the targetSdkVersion without also updating the dependencies.
I tried changing it in project structure under app module but this did not work out. Nothing changed in gradle file My next option is to increase it by editing the gradle file itself and increasing to api level 28.
Changing it in project structure wouldn't do any magic for you, you still have to follow the same process as you would by changing the gradle file.
I am however scared of the effects this may have on the other libraries
Keep the 3rd party libraries up to date as well, using newer versions of libraries in older sdks will most likely cause issues as newer Android SDKs have behavior changes.
Here are some personal tips for migrating without being overwhelmed by the things it might break:
1. Increment your target api by 1 each time and then fully test it
For example, upgrade to api 27 from 26, test it and then upgrade it to 28 and so on. The reason for this is to narrow down and account for behavior changes in each api since the previous target api. Not only will it be less overwhelming, it would be easier to find solutions for the problems that might occur afterwards. If you migrate to 29 from 26 directly, it would be very difficult to identify the issue.
2. Read Migration Guides for each Android SDK
These pages were really helpful in identifying deprecated classes/features being used even by third party libraries:
Android 8.0 Migration Guide
Android 9.0 Migration Guide
3. If there is a problem in third party library after upgrading, it is most likely in Github Issues page. If it isn't there, create a new issue (following the guidelines listed there). There is no better place to get library specific answers than the repository site where the developers themselves can give you good suggestions.
4. Read the changelog starting from the previous version of libraries that have issues.
Some libraries have outdated documentation and newer versions can cause issues. For example, when I was migrating Dagger, there were some classes that were deprecated on newer versions but weren't documented and I only found out after reading the changelog.

Failed to find style 'coordinatorLayoutStyle' in current theme In Android Studio 3.1

The Android Studio 3.1 layout preview fails to find style 'coordinatorLayoutStyle' in the current theme.
Failed to instantiated one or more classes.
Exception shown are :-
java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener
This happened due to use of alpha version SDK 28
We need to change the android { ... } in "build.gradle" in app file
compileSdkVersion 28 to compileSdkVersion 27
targetSdkVersion 28 to targetSdkVersion 27
Also, try to change implementations like
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' to
implementation 'com.android.support:appcompat-v7:27.1.1'
Hope this works!
For Android Studio v3.1.*, in addition we need change:
implementation 'com.android.support:design:28.0.0-alpha3' to
implementation 'com.android.support:design:27.1.1'
you can fix this issue by adding this script to the app module build.gradle, in the android section
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == "com.android.support") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "27.+"
}
}
}
}
This appears to be a bug in the current version of Android Studio (3.1.3) as I was encountering the same thing. I downloaded the beta build from here and opened my existing project and the errors disappeared.
https://developer.android.com/studio/preview/?utm_source=android-studio
Not exactly a fix but hopefully it will get you back up and running.
Update
1. com.android.support:appcompat stable version 28.0.0 is released. So no need to downgrade version. Just use 28.0.0.
def supportVersion = "28.0.0"
implementation "com.android.support:appcompat-v7:$supportVersion"
implementation "com.android.support:design:$supportVersion"
You can ignore design library if you don't need it.
2: You also need to update your compileSdkVersion & targetSdkVersion to remove some gradle warnings.
compileSdkVersion 28
targetSdkVersion 28
Never use alpha versions of any library, because alpha, beta and rc
versions may have bugs. And you don't want to face these types of errors often.
Important Suggestion
I suggest you migrate to androidx because android will not update support library after 28.0.0, all updates will be available to androidx package only. Check related answer.
This is fixed in Android Studio 3.2.
Go to app/res/styles and change the Theme.AppCompat.Light.DarkActionBar for this one Base.Theme.AppCompat.Light.DarkActionBar

Android Studio Gradle Wont Compile

I have been working on an Android application for several months, and didn't realize until today that it was targeting the level 25 for "Compile SDK Version." I want my app to be usable on versions of Android starting at API level 22 (Android 5.1). I went into my gradle settings and set a couple things...
compileSdkVersion 22
targetSdkVersion 22
I also changed the versions of the libraries it compiles with..
compile project(':snakeyaml-1.14-android')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:support-vector-drawable:22.2.1'
I get the following error...
"Failed to resolve: com.android.support:support-vector-drawable:22.2.1"
What should I do?

Manifest Merger failed for minSDKVersion

With the following gradle script, I was wondering how to change the minSdkVersion from 7 to 3 (just so my app could run on more devices) without getting the error at the bottom of this post:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "dpark.cellular_automata"
minSdkVersion 3
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
... And then I would get the following error:
Error:Execution failed for task ':app:processDebugManifest'.
Manifest merger failed : uses-sdk:minSdkVersion 3 cannot be smaller than version 7 declared in library [com.android.support:appcompat-v7:23.0.1] C:\Users\Dave\AndroidStudioProjects\Cellular_Automata\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.0.1\AndroidManifest.xml
Suggestion: use tools:overrideLibrary="android.support.v7.appcompat" to force usage
... That said, is it even possible to lower my minimum SDK to 3 (version 1.5, Cupcake) if I wanted to publish my app with Google Play? And also, in case you're wondering, I also downloaded the Android Support Library in the SDK manager as follows, and yet it still doesn't work:
Thanks!
That error is telling you that one of your libraries (in this case, AppCompat) only supports back to SDK version 7. Since that library cannot support anything lower than 7 and you use that library, you can't support anything lower than 7.
Your options are either:
Remove AppCompat
Support a minimum of API level 7. Note that SDK version 8 currently has less than 0.1% of the market share globally, and all versions less than 8 have a combined total of 1%. It probably isn't worth your time.
That said, is it even possible to lower my minimum SDK to 3 (version 1.5, Cupcake) if I wanted to publish my app with Google Play?
Absolutely. You could upload an Android app supporting API level 1 if you really wanted to.
And also, in case you're wondering, I also downloaded the Android Support Library in the SDK manager as follows, and yet it still doesn't work:
The support library v4 only supports back to API level 4, so I'm not sure how that would help with supporting API level 3.

Replacing/Overriding classes of the android-sdk

I'm confused with the compilation under Android.
I've a library using those dependencies :
compile('org.apache.httpcomponents:httpmime:4.3') {
exclude module: 'httpclient'
}
//we need the org.apache.http.entity.ContentType class
compile('org.apache.httpcomponents:httpcore:4.3')
My misunderstanding is that it compiles and seems to run properly, but I would expect a compilation error since :
Android SDK contains many classes with exact same name as the classes in httpcore
Here are my questions :
Why is there no UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added:... during compilation ?
How can I know which version of the duplicate classes is used at runtime ?
I'm using build tools 19 :
buildToolsVersion "19.1.0"
compileSdkVersion 15
defaultConfig {
minSdkVersion 15
targetSdkVersion 15
}
Use the official Apache HttpClient port for Android instead of the stock version

Categories