Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch application with Theme.Material on older devices

How can I run my app on pre-v21 devices?

compileSdkVersion  'android-L'
minSdkVersion 14
targetSdkVersion 'L'

I'm only using the Theme.Material on v21. I'm NOT using the v20 support library, I'm using com.android.support:support-v4:19.+

When running the application from Android Studio it says that the device in not compatible:

{compatible=NO, reason=minSdk(API 20, L preview) != deviceSdk(API 16)}

From where is it taking the minSdk 20?

EDIT:

The reason this is happening could be (if verified) that

If you compile against a preview SDK (android-L), the build tools will lock minSdkVersion and targetSdkVersion to that same API level. This results in the produced application being unable to be installed on devices running older releases of Android, even if your application isn't doing anything specific to L.

Source: Reddit

like image 305
David Corsalini Avatar asked Jun 27 '14 10:06

David Corsalini


1 Answers

If you compile against a preview SDK (android-L), the build tools will lock minSdkVersion and targetSdkVersion to that same API level. This results in the produced application being unable to be installed on devices running older releases of Android, even if your application isn't doing anything specific to L.

Version 0.11 of the Android Gradle Plugin turned on a new Manifest Merger by default, and it allows us to do some nifty stuff. So you have to add yo your AndroidManifest.xml file a uses-sdk node that simply specifies a tools:node attribute.
This specific configuration tells the manifest processor to replace any attributes from uses-sdk nodes in lower-priority manifests (such as library manifests) with the attributes in the uses-sdk node with the tools:node="replace" attribute. Since Gradle also inserts minSdkVersion and targetSdkVersion from your build.gradle into this uses-sdk node, that's all you really need to add.

So your AndroidManifest.xml file should look somehing like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="net.eringle.android.ldemo">

    <uses-sdk
        tools:node="replace" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Now you should be able to run your application on any device supported by your minSdkVersion, while taking advantage of the neat new views and utilities in the support lib!


Next, I've modified the Android L platform slightly to hide the fact that it's actually a preview platform from the build tools. You can download it from either of these two mirrors: mirror #1, mirror #2.

Extract the archive into the platforms directory where you have your Android SDK installed. You'll notice I've named it android-21 and set the API level to 21 as well. Instead of referencing L or android-L in your build.gradle, just use 21:

android {
    compileSdkVersion 21
    buildToolsVersion '20.0.0'
    defaultConfig {
        applicationId 'net.eringle.android.ldemo'
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    ...
}

Now when you build and try to run the application, you should be able to send it to older-platform devices without any issues. Have fun playing with L while retaining backwards-compatibility!


Obviously, the workarounds provided above are hacks at best. Please star this issue, as it seems to be the closest to the heart of the problem (if the Android team even deems it a problem at all).

All the credits go to EddieRingle

like image 173
Egor Neliuba Avatar answered Sep 22 '22 03:09

Egor Neliuba