Android Gradle Build Resource.getIdentifier() always returns 0 - gradle

I'm getting started on porting my Game over to the new Gradle Build system for Android.
I'm using the Resource.getIdentifier() method to load some resources by name instead of ID. However, ever since switching to the Gradle build, this call always returns 0. The assets are included in the built jar, and the R.class in the dex file contains all of my resources and ID's as expected, so I am at a loss as to explain this. This code worked before I changed to the gradle build. My build.gradle is below:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:18.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "19"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
TIA.

D'oh! my problem was that Resources.getIdentifer() takes 3 parameters, one of which is the package name. The debug section of my gradle script was tacking on ".debug" to the package name on the device. Solution is to remove the .debug suffix in the build.gradle.

Related

OpenAPI Generator gradle plugin task not executed

In my android project I am trying to include the openapi-generator plugin to generate a client. I've copied the instructions from the readme for my project. Unfortunately, the task doesen't seem to be executed.
I am new to gradle and android. So I haven't a lot of knowledge on these subjects yet.
This is the projects build.gradle file:
buildscript {
ext {
nav_version = '2.4.2'
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath "org.openapitools:openapi-generator-gradle-plugin:5.4.0"
}
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is the build.gradle file of my module:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id "org.openapi.generator"
}
android {
compileSdk 31
defaultConfig {
applicationId "..."
minSdk 29
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
dataBinding true
}
sourceSets {
main {
kotlin {
srcDirs += 'build/generated/source/navigation-args/'
}
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
openApiGenerate {
generatorName = "kotlin"
inputSpec = "$rootDir/app/src/main/assets/swagger.yml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
In the output of the build was no hint that the openApiGenerate task was executed and on the filesystem I didn't find the generated client.
Probably there is a mistake in the configuration, but I don't know what is wrong.
Can you help me?
This command should generate the client :
./gradlew openApiGenerate
If you want to avoid manually running this every time your schema changes, then you can go to the drop down here:
Now choose edit configurations:
Now add a gradle task with the + button:
Then:
Enter the task in run:
Now edit the app configuration, by adding what you just created to the before launch section:
You will see the YourAppsName[openApiGenerate] configuration you created as an option, so select that, then apply changes, and now every time you run your application your client will be built.

Android subprojects with Kotlin DSL

I'm working on migrating by Gradle build files to the Kotlin DSL and I encounter an issue.
On my parent build.gradle, I have the following piece of code
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Version.kotlin}"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate { project ->
if (project.plugins.findPlugin('com.android.application') ?:
project.plugins.findPlugin('com.android.library')) {
android {
compileSdkVersion = Android.SDK_COMPILE
defaultConfig {
minSdkVersion Android.SDK_MIN
targetSdkVersion Android.SDK_TARGET
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
This allows me to configure in only one place all the modules that are android applications or libraries.
However this doesn't seem to work when I migrated to kotlin:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(Dependency.androidGradle)
classpath(Dependency.kotlinGradle)
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate {
if (project.plugins.findPlugin("com.android.application") != null ||
project.plugins.findPlugin("com.android.library") != null) {
android { <<<<------------ Unresolved reference: android
compileSdkVersion(Android.SDK_COMPILE)
defaultConfig {
minSdkVersion(Android.SDK_MIN)
targetSdkVersion(Android.SDK_TARGET)
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
The error is Unresolved reference: android and it looks like the android{} block is not recognized by the script compiler.
My theory is that the if checking for the subproject type is not enough, and I might have to cast or get a reference to some object in which I can call the android{} block, but honestly I do not know enough.
Any clues?
Gradle Kotlin DSL determines the dependencies for each script from the gradle classpath plus the applied plugins. That's why its recommended to use the plugins { ... } block in the Kotlin DSL.
You need to add the android and kotlin plugins to your root without applying it.
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
Unfortunately, that will still not generate the static accessors for the root build script but it will give you access to the plugin classes within the script and you can reference them like:
subprojects {
// BasePlugin is the common superclass of the AppPlugin and LibraryPlugin which are the plugin classes that "com.android.application" and "com.android.library" apply
plugins.withType<BasePlugin> {
// BaseExtension is the common superclass of the AppExtension and LibraryExtension which are the extension classes registered by the two plugins to the name "android"
configure<BaseExtension> {
// This block is typed correctly
defaultConfig {
// ...
}
}
}
}

Issue with bundling Android Wear1 app's with android gradle plugin 3.0.1

I cannot get the Wear1 signed APK into the res/raw folder of my signed mobile apk file since using the latest android gradle plugin 3.0.1
I have to use the latest gradle to get the google repo for certain dependencies.
I have checked the wear apk and it is signed with my production signature.
I know the preferred method going forward is to unbundle and I can successfully do that but Google's acknowledged latency issue on installing Wear 1 apps that way makes it unattractive at this time.
Below are my gradle files.
Project build.config
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Mobile build.config
apply plugin: 'com.android.application'
apply from: rootProject.file('shared-config/android-signing.gradle')
android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
applicationId "com.me.myapp"
minSdkVersion 18
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
flavorDimensions "environment"
productFlavors {
development {
dimension "environment"
}
production {
dimension "environment"
}
}
}
configurations {
developmentReleaseWearApp
productionReleaseWearApp
}
dependencies {
implementation 'com.google.android.gms:play-services-wearable:11.8.0'
implementation 'com.android.support:support-compat:27.1.0'
implementation 'com.android.support:percent:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
developmentReleaseWearApp project(path: ':wear', configuration: 'wear1Release')
productionReleaseWearApp project(path: ':wear', configuration: 'wear1Release')
implementation 'com.android.support:support-annotations:27.1.0'
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'com.android.support:preference-v14:27.1.0'
implementation 'com.android.support:design:27.1.0'
implementation 'com.android.support:support-v13:27.1.0'
provided 'com.google.android.wearable:wearable:2.2.0'
implementation 'com.google.android.support:wearable:2.2.0'
}
Wear build.config
apply plugin: 'com.android.application'
apply from: rootProject.file('shared-config/android-signing.gradle')
android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
publishNonDefault true
defaultConfig {
applicationId "com.me.myapp"
minSdkVersion 23
targetSdkVersion 27
versionCode 2
versionName "1.0"
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
flavorDimensions "wearVersion"
productFlavors {
wear1 {
dimension "wearVersion"
// Use the defaultConfig value
}
wear2 {
dimension "wearVersion"
minSdkVersion 24
}
}
configurations {
wear1Release
wear2Release
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.google.android.gms:play-services-wearable:11.8.0"
implementation "com.android.support:support-compat:27.1.0"
implementation "com.android.support:support-annotations:27.1.0"
implementation "com.android.support:appcompat-v7:27.1.0"
implementation "com.android.support:recyclerview-v7:27.1.0"
implementation "com.android.support:percent:27.1.0"
implementation "com.android.support:support-v4:27.1.0"
implementation "com.android.support:wear:27.1.0"
implementation "com.google.android.support:wearable:2.2.0"
implementation "com.android.support.constraint:constraint-layout:1.1.0-beta5"
provided 'com.google.android.wearable:wearable:2.2.0'
}
settings.gradle
include ':mobile'
include ':wear'
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
I was having the same problem this week. Today I finally managed to get the wear app correctly embedded into the mobile app APK using the 3.0.1 gradle. I added a productFlavor to my wear app build.gradle to match the productFlavor of the mobile app that I was generating the build for. Trying to specify a differently-named wear app configuration in the mobile app's build.gradle dependency list seemed to be the problem for me.
I would suggest trying to add development and production flavors to your wear app build.gradle:
development {
dimension "wearVersion"
// Use the defaultConfig value
}
production {
dimension "wearVersion"
// Use the defaultConfig value
}
and then change your mobile app build.gradle dependency to simply:
wearApp project(path: ':wear')
When you generate the build, it should match productFlavor to productFlavor by default and that is working for me.

Gradle cannot find method android() for arguments (Gradle 2.2.3)

Gradle for my project isn't working. I've checked other questions that have had the same problem, but this one is in a newer version and I'm not sure how to solve it. I'm trying to get the Android SDK up and running for the first time, so I'm really new to this. Thorough answers are appreciated.
Here is my Gradle error:
Error:(22, 0) Could not find method android() for arguments [build_3bh8pl96cp64yo421v01fuk9s$_run_closure3#4b7fff2f] on root project 'Meet-upProject' of type org.gradle.api.Project.
And here is my build.gradle in root:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
}
dependencies {
compile files('app/libs/junit-4.12-JavaDoc.jar')
}

Could not find property 'plugin' on project:

I have an SDK for which I've developed a number of small test applications. These are all stand alone android apps which have the following in common:
1) All dependencies are the same (i.e. same version of my sdk/android sdk/etc..)
2) They all share a common top level build.gradle file (which is currently empty):
3) They're build,test etc.. phases are all identical.
I want the projects to all exist as modules in Android studio, and I want them to share as much configuration as possible in the top level build.gradle so that I can update the one file and have everything working correctly.
When I add the following to my top level build.gradle:
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
}
I get
'could not find property 'plugin' on ':App1' where ':App1' is the first app in settings.gradle, and the build fails.
My goals would be to get the above block and as many other applicable elements in this top level file.
Edit: I've fixed the syntax error (i was missing the :), but I've that leads to a "plugin with id 'android' not found. Generally this would be resolved by adding:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
But that doesn't resolve it in this case.
Edit 2:
final form:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
}
}
}
That seems to be working correctly.
This is a simple syntactical error. The apply() method takes a Map as argument. Don't forget the colon (:).
apply plugin: 'android'

Resources