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

前端 未结 6 1022
醉话见心
醉话见心 2021-01-03 20:43

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

相关标签:
6条回答
  • 2021-01-03 20:57

    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...

    0 讨论(0)
  • 2021-01-03 21:02

    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.

    0 讨论(0)
  • 2021-01-03 21:02

    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.

    0 讨论(0)
  • 2021-01-03 21:02

    You can create a new Android Virtual Device with the preferred API level and debug the application on it.

    0 讨论(0)
  • 2021-01-03 21:06

    Simply open module settings and change compileSdk and buildTools to v20

    0 讨论(0)
  • 2021-01-03 21:07

    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.

    0 讨论(0)
提交回复
热议问题