Gradle issue with Android Studio 2.3.3 - android

I am getting the following error:
"
The SDK platform tools version (25.0.6) is too old to check APIs
compiled with API 26; please update This check scans through all
ANDroid API calls in the application and warns about any calls that
are not available on all versions targeted by the application
(according to its minimum SDK attribute in the manifest). If you
really want to use this API and don’t need to support older devices
just set the minSdkVersion in the build.gradle or AndroidManifest.xml
files If your code is deliberately accessing newer APIs and you have
ensured (e.g. with conditional execution) that this code will only
ever be called on a supported platform, then you can annotate you
class or method with the #TargetApi annotation specifying the local
minimum SDK to Apply, such as #TargetApi(11), such that this check
considers 11 rather than you manifest file’s minimum SDK as the
required API level. If you are deliberately setting android:
attributes in style definitions, make sure you place this in
values-xxx folder in order to avoid running into runtime conflicts on
certain devices where manufacturers have added custom attributes whose
ids conflict with new ones or later platforms. Similarly, you can use
tooltargetApi=”11” in an XML file to indicate that the elements will
only be inflated in an adequate context."
I also get the package name up top as an error, but I just created the new App and even though it indicates an error. The source code compiles and runs on the emulator on the targeted device.

Go to your App Level Gradle file and enter the following:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'
defaultConfig {
applicationId ">>>YOURPACKAGENAME<<<"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

Related

How to get "CompileSdkversion" programmatically in Android

I have an About box in my App that displays information about the App, the phone and the data it uses. It's very useful when a user has a problem. I can get the phone's SDK version using "android.os.Build.VERSION.SDK_INT". However, I haven't found a way to get the value of "CompileSdkversion" which indicates the SDK version the App was compiled with. This is the value that is set in the build.gradle file.
While the Android OS version varies by user, the compileSdkVersion does not. For version X.Y.Z of your app, the compileSdkVersion is whatever you said it was when you compiled that app version. So long as your about box contains the app version, you know what compileSdkVersion that you used, if you keep track of that (e.g., check what it was in your version control system).
But, if you really want to have it be available to you at runtime, you have two options.
If your minSdkVersion is 31 or higher, you can use compileSdkVersion on ApplicationInfo. However, most likely, if you are reading this before the year 2026, your minSdkVersion is lower than that.
For older devices than Android 12, you could add a BuildConfig field for it, at least with newer versions of the Android Gradle Plugin:
android {
compileSdk 31
defaultConfig {
applicationId "com.commonsware.android.myapplication"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "int", "COMPILE_SDK_VERSION", "$compileSdk"
}
// other stuff goes here
}
This takes your defined value for compileSdk and hoists it into BuildConfig.COMPILE_SDK_VERSION, so you can reference it at runtime. This was tested using a scrap Arctic Fox project, using Gradle 7.0.2 and 7.0.3 of the Android Gradle Plugin.
Here is the relationship between the three values:
minSdkVersion (lowest possible) <=
targetSdkVersion == compileSdkVersion (latest SDK)
CompileSdkVersion has nothing to do with what devices can and cannot run your app. Usually, you set this to be the latest version of the Android SDK.
And the targetSdkVersion should be fully tested and less or equal to compileSdkVersion.(It depends on your app)
If you are using the features of API level of 26 then you need to use compileSdkVersion 26, the lower version will give you an error.
Android supports backward compatibility
(i.e. an app compiled on 26 can also run on a phone having API level 26 or lower).
Considering your use-case, wouldn't a better approach be just to show the current app version? If you know the version, you could look up how/when it was created (via git tags, for example) and then find out the SDK version it was compiled with.

Why are my supported android devices so low?

Why are my supported android devices so low? I've tried to target the lowest possible apk (13), and yet only 12,000 devices can use my app.
Any help would be much appreciated!
Here is my build.gradle.
android {
signingConfigs {
}
compileSdkVersion 23
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "thomas.surfaceviewtest"
minSdkVersion 13
targetSdkVersion 13
versionCode 5
versionName "1.0.5"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
}
}
dexOptions {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services-ads:9.4.0'
compile 'com.google.firebase:firebase-ads:9.4.0'
}
apply plugin: 'com.google.gms.google-services'
That number is comparting your manifest configuration and a list of devices know by Google (see the list of devices).
It is not a number of real devices, but a number of model supported, so 12000 models on the market is a good number I believe.
Its because your target sdk level is low
targetSdkVersion 13
Update it to 25 so it will support more devices
targetSdkVersion 25
You can check for more android sdk version here
My app is compatible with 12611 devices. That is basically the same as you have. 12000 devices isn't little, especially given the fact that there are "only" 1465 unsupported devices as a result of my minSdk version being high.
Targeting 12k devices isn't little, it is a lot. If you were only targeting say 8k that would be something you should look into.
You should still change compile and minSdk versions to 25 or 26 (7.1 or 8.0) to make sure your app can run the newest API's.
I'd like to clarify the fact that targeting API 13 doesn't exclude API 14-26 from installing the app. It indicates what version it is designed for. You compile against 23 so you have the new API's included, but you only use API 13.
This should help:
android:targetSdkVersion
An integer designating the API Level that the application targets. If not set, the default value equals that given to minSdkVersion.
This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions (down to minSdkVersion).
As Android evolves with each new version, some behaviors and even appearances might change. However, if the API level of the platform is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your app continues to work the way you expect. You can disable such compatibility behaviors by specifying targetSdkVersion to match the API level of the platform on which it's running. For example, setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher and also disables screen compatibility mode when running on larger screens (because support for API level 11 implicitly supports larger screens).
Source: https://developer.android.com/guide/topics/manifest/uses-sdk-element.html
You should your increase target sdk level.
targetSdkVersion 25

How to choose Android sdkVersion properly?

everyone!
I'm new to android programming, so simple things sometimes become a problem.
I have my application. It should work on devices with Android 5 and higher.
The question is what is proper strategy of sdkVersion defining?
What I mean.
For example, I need to acheive permision to use bluetooth.
If my target sdkVersion is 7 and minimum sdkVersion is 5 I should ask permission in manifest file and then acheive it in runtime. Like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
...
}
But if my target sdkVersion is 5 even Build.VERSION_CODES.M cannot be resolved.
So the question is : what is proper approach to choose sdkVersion? Where can I read about it?
I read here https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#target
but I didn't get what is best practice. So please share your experience.
Read this article from Ian Lake: Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion
compileSdkVersion:
compileSdkVersion is your way to tell Gradle what version of the
Android SDK to compile your app with. Using the new Android SDK is a
requirement to use any of the new APIs added in that level.
minSdkVersion:
If compileSdkVersion sets the newest APIs available to you,
minSdkVersion is the lower bound for your app. The minSdkVersion is
one of the signals the Google Play Store uses to determine which of a
user’s devices an app can be installed on.
targetSdkVersion:
The most interesting of the three, however, is targetSdkVersion.
targetSdkVersion is the main way Android provides forward
compatibility by not applying behavior changes unless the
targetSdkVersion is updated. This allows you to use new APIs (as you
did update your compileSdkVersion right?) prior to working through the
behavior changes.
Ideally, the relationship would look more like this in the steady state:
minSdkVersion (lowest possible) <=
targetSdkVersion == compileSdkVersion (latest SDK)
I suppose you are using Android Studio and build.gradle. If not, I recommend you to get it. All of the following is relevant for Android Studio and gradle build system.
Your main mistake is that SDK version in build.gradle of app module is not the same as Android Version. Here is the list of Platform Codenames, Versions, API Levels. What you need for SDK version is number in API level column of first table.
This is how android section of build.gradle for app targeting Android 5.0 and newer should look like.
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "net.eraga.myobjectives"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Read more about targetSdkVersion, minSdkVersion and compileSdkVersion here.
In this case, your minSdkVersion should be 21 (android 5.0) and targetSdkVersion along with compileSdkVersionshould be 25 (android 7.1).

'Calling new methods on older versions' lint check not finding higher than minimum api calls in Android Studio

I'm developing an Android app on Android Studio and I'm calling a method from API level 19. My build.gradle looks like this:
android {
compileSdkVersion 24
buildToolsVersion '24.0.2'
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion 16
}
}
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
multiDexEnabled true
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
}
I have no lint.xml files in my project, so no lint checks are overriden.
The problem is that the lint check "Calling new methods on older versions." doesn't find the method call for API level 19. If I open the build variants panel and select prodDebug or prodRelease the method gets underlined in red, but the project builds well. How can I get Android Studio to effectively show the aforementioned method in the analyze result or to block me from correctly building the project?
Edit
I found a way to search for methods not complying with the minimum SDK. By going to the Gradle panel on the right and running the lint task, an HTML report is generated, which finally shows the API 19 call along with other calls. Why is this not implemented into Android Studio?
Since this still does not work in Android Studio per se, but rather in Gradle, I'm not closing the question yet.
Gradle won't tell you if you are using the methods that are not supported in you minSDK if your compiled SDK version is not the minimum one. read more about it
So simply the solution is use lint feature i.e inspectcode
right click , either on project/class then => analyze=>inspectCode

Android Studio: How to debug older version of Android SDK step-by-step

I have an Android project targetting the Android SDK v21. Now I need to debug it on a device with Android 4.4 (i.e. SDK v20). How do I tell Android Studio to attach an older version of the source to the internal classes so that I can step through them?
Here the best solution is to set compile SDK version to 20. So that build tools will compile your project using SDK version 20 and you can debug your app.
When you use several modules in your project you need to set same compile SDK version to each module.
I can not confirm my answer works, but it is working for me.
I replace my compileSdkVersion, buildToolsVersion, minSdkVersion and targetSdkVersion with new one like following in my build.gradle file.
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.mytest"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
After changing it, you may not need to convert old to new or new to old sdk component.
You need to open module settings (Right click on your app module and select Open Module Settings or just select module and press ⌘ + ↓ on Mac)
And select compileSdk and buildTools to v20
Another way to do it is to open build.gradle file for the "app" module to change compileSdk and buildTools.
I would like to explain little before the solution.
Google provides the SDK APIs in the form of JAR (android.jar) files. The JAR files are stored at SDK LOCATION\platforms\android-API#.
For example the android.jar file for the Android 4.4(Kitkat) API-19 is located at YOUR SDK LOCATION\platforms\android-19\android.jar.
The Gradle compilation with the Android Studio uses the compileSdkVersion field in the build.gradle to select a specific android.jar for the compilation.
What is compileSdkVersion?
The version of the API the app is compiled against.
This means you can compile and use Android API features included in that version of the API
All the previous version features are also included during compilation.
If you try and use API 21 feature, such as public void openCamera (String cameraId, CameraDevice.StateCallback callback, Handler handler)
Added in API level 21 but set compileSdkVersion to 20, you will get a compilation error.
If you set compileSdkVersion to 21 you can still run the app on a API 20 device as long as your app's execution paths do not attempt to invoke any APIs specific to API 21.
How to set compileSdkVersion?
There are two ways
1. Selecting API from the GUI
1) Select "File->Project Structure..."
2) Select "app->Properties"
3) Set the "Compile Sdk Verion" from the drop down box to the required SDK version.
4) "compileSdkVersion" will be set automatically in the "app" module's
"build.gradle" file.
2. Setting the API in the gradle.build file
1) Open the build.gradle file of the "app" module
2) Set the compileSdkVersion to required value such as 19 for the API#19.
I hope that helps...
Happy Coding...
The best solution for me was to add a new module (Android Library) to the project and set the compile SDK to the requested api level. That way you can still compile your main app with the original SDK level but still get the sources integrated with your debugging Android version.
You can create a new Android Virtual Device with the preferred API level and debug the application on it.
Simply open module settings and change compileSdk and buildTools to v20

Categories

Resources