user2223059 user2223059 - 5 months ago 49
Android Question

Android: Trying to work with new N features, getting weird messages in gradle

So, I'm trying to work with Android N. I'm a fairly new android developer, my goal is to make the app compatible with N features like multi-window, while still having it work on earlier versions.

Here is how I start my build.gradle:

compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
applicationId "io.craigmiller160.contacts5"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}


So, I set my compileSdkVersion, targetSdkVersion, and buildToolsVersion to 24.

When I do this, two things happen:

1) The buildTypes section in the code snippet I posted gets highlighted by AndroidStudio. There's an error message that says "buildTypes cannot be applied to '(groovy.lang.Closure)'" I'm not using groovy at all in this app, btw. This message also didn't start appearing until I changed my sdk version to 24.

2) I get another AndroidStudio error message with my support libraries:

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v13:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'


I can already tell that they're all targeted for sdk 23, and not 24. I would like the error highlighting to go away though. The thing is, I can't find anything for the specific version number for sdk 24. Could I just use 24.0.0? Or is there a place I can look that up, specifically.

3) This is a final, unrelated thing, but I'm a professional Java dev, and I never get to use Java 8 features at work because of the need for backwards compatibility with clients who run our app on old JVMs. Can I safely use the Java 8 features with the Jack toolchain with my app still working on pre-N versions of Android?

As anyone can see from my config, my app is being deliberately targeted at Lollipop and up (for specific reasons, despite the market share). But I don't want to break pre-N compatibility for this.

Thanks so much.

Edit: Ok, so weirdly, I reverted my project back to an earlier commit before I made these changes while I would wait for an answer. But because I've now downloaded the new support libraries when I downloaded sdk 24, I got a different alert telling me that version 24.0.0 was available. This only appeared when my targetSdk was 23. The moment I bump it up to 24, I just get an error with no hints as to what version I should switch to. Weird.

So ignore question 2, just 1 and 3 please.

Answer

There's an error message that says "buildTypes cannot be applied to '(groovy.lang.Closure)'"

Try adding a blank line before your buildTypes line. If that does not help, consider editing your question and posting the entire build.gradle file, rather than the subset.

I'm not using groovy at all in this app

Gradle is based on Groovy.

Could I just use 24.0.0?

Yes.

Or is there a place I can look that up, specifically.

Look in $ANDROID_HOME/extras/android/m2repository/com/android/support/appcompat-v7/, where $ANDROID_HOME is wherever your Android SDK is installed. Generally speaking, all of the com.android.support libraries get updated in unison, so looking up one (e.g., appcompat-v7) should give you a valid version number to use for the others.

You are also welcome to try setting up your dependencies via the Project Structure dialog in Android Studio. Personally, I work with the build.gradle file directly.

Can I safely use the Java 8 features with the Jack toolchain with my app still working on pre-N versions of Android?

You cannot use the new classes (e.g., java.util.stream), as they are new to API Level 24. Other features are supposed to work going back to API Level 7 IIRC. That being said, I have not tried them yet.