Cannot invoke method multiply() on null object - android-studio

I'm trying to generate split apk in react-native and getting this error after setting these two properties:
def enableSeparateBuildPerCPUArchitecture = true
def enableProguardInReleaseBuilds = true
This is the error I'm getting:
Cannot invoke method multiply() on null object
Open File
On clicking the open file, it is pointing me to this code:
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
My configurations are like this:
defaultConfig {
applicationId PACKAGE_NAME
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode VERSION_CODE
multiDexEnabled true
versionName MY_VERSION_HERE
ndk {
abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a":3, "x85_64":4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
Please help me with this or at least give me a clue why this should be happening.

I ended creating another project, installing all the repository and it worked.
Still, don't know why it was happening in the first place.

you should change "x85_64" -> "x86_64" in the applicationVariants.all setting

Debuging here I added a line:
println abi
before the output.versionCodeOverride line and saw the abi needed. Then, added the abi on line def versionCodes. It ends like:
def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a":3, "x86_64":4]

Related

Android Studio 2.3.3 using gradle component model was creating .aar automagically; Android Studio 3.4.1 now using CMake or NDK-Build does not

My wavvoxlibrary/build.gradle is not only serving the main program (app/build.gradle) but also creating the shared library .aar.
And that is working nicely with Android Studio 2.3.3 and -- for the library -- gradle-experimental 0.9.3. The gradle component model, however, does not support abifilters, say, to build the JNI / c/c++ programs in 64-bit.
So I upgraded to Android Studio 3.4.1, and my wavvoxlibrary/build.gradle moved away form said component model and got the appropriate CMakeList.txt file to list the c/c++ programs and any compiler options that used to be part of the older build.gradle. And I also added the NDK-Build option with the Application.mk and Android.mk make files.
So far so good. My app is built and running correctly -- including the shared libraries for both, armabi-v7a (32-bit) and arm64-v8a (64-bit).
What's missing: to create the .aar file, I have to explicitly call the gradle task wavvoxlibrary .. assemble.
Is there some way to generate the .aar that during build of my, say, signed apk?
I have removed the gradle component model to move from gradle:2.3.3 and gradle-experimental:0.9.3 with "apply plugin: 'com.android.model.library' "
to gradle:3.4.1 with "apply plugin: 'com.android.library' " and also called the cmake (or, optionally ndkBuild instead) to list the c/c++ files and compiler options there.
See my blog with the details: http://jurgenmenge.com/blog/computer/migrate-a-library-in-android-studio/
import java.text.SimpleDateFormat
// migrate from Android Studio with gradle:2.3.3 + gradle-experimental:0.9.3
// to Android Studio with gradle:3.4.1 + Cmake + ndkBuild
// jm*20190614
apply plugin: 'com.android.library'
//apply plugin: 'com.android.model.library'
/**
* Each subversion code should be exactly 2 numeric digits or we will get wrong versions published
* in the Google Play Store; buildType to switch between "cmake" and "ndkBuild"
*/
ext.majorVersion = "03"
ext.minorVersion = "08"
ext.patchVersion = "03"
static String buildKind() { // neat trick I created :-) // jm*20190614
//return "gradle" // model - experimental
//return "cmake"
return "ndkBuild"
}
/**
* The name of the app version, using the Semantic Versioning format (major.minor.patch)
* #return
*/
String appVersionName() {
String appVersion = removeLeadingZeros(ext.majorVersion) + "." \
+ removeLeadingZeros(ext.minorVersion) + "." \
+ ext.patchVersion
System.out.println(" ************************************************")
System.out.println(" *** WavvoxLibrary version: " + appVersion + " ")
System.out.println(" *** buildKind " + buildKind() )
System.out.println(" ************************************************")
return appVersion
}
//model {
android {
compileSdkVersion 28
buildToolsVersion "28.0.3" // "25.0.3"
defaultConfig {
minSdkVersion 16 /*** was: 8 ***/
targetSdkVersion 28
//
versionName appVersionName() + "_" + buildKind()
setProperty("archivesBaseName", "wavvoxlibrary-$versionName")
buildConfigField "String", "VERSION_NAME_WAVVOX_FORMAT", versionNameWavvoxFormat()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//} // defaultConfig
ndk {
moduleName "wavvox-decoder"
ldLibs "log", "android"
// these platforms cover 99% percent of all Android devices
abiFilters "arm64-v8a", "armeabi-v7a"
//, "x86", "x86_64"
} // ndk
externalNativeBuild {
switch (buildKind()) {
case "ndkBuild":
ndkBuild {
cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon" // jm*20190616
// "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
abiFilters "armeabi-v7a", "arm64-v8a"
}
break
case "cmake":
cmake {
cFlags "-std=c99", "-O3", "-Ofast", "-mfpu=neon" // jm*20190616
// "-Wno-error=format-security", "-mfloat-abi=softfp:, "-g"
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
break
default:
println "**** unknown buildType value: " + buildType() + " ****"
}
} // externalNativeBuild
} // defaultConfig
buildTypes {
release {
minifyEnabled true
proguardFiles "proguard-rules.pro"
}
} // buildTypes
// } // android // jm*20190405
// android.ndk { // jm*20190405
// C source files to include in the build script
// android.sources.main.jni { // jm*20190405
/* if (buildKind() == "gradle") // using "model" in gradle_experimental
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jni'] // for pre-built .so files
jni {
//source {
include "wavvoxNativeApp.c"
// ... and all the other c/c++ programs
srcDir "jni"
//}
} // jni
} // main
} // source
*/
externalNativeBuild {
switch (buildKind()) {
case "ndkBuild":
ndkBuild {
path 'src/main/jni/Android.mk'
}
break
case "cmake":
cmake {
version '3.10.2'
path 'src/main/jni/CMakeLists.txt'
}
break
default:
println "**** unknown buildType value: " + buildType() + " ****"
}
} // externalNativeBuild
} // android // jm*20190405
//} // model
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'
}
/**
* Workaround for using ProGuard with the experimental Gradle plugin.
*
* Trouble seems to be that unless process(Debug|Release)Resources task is needed, it is not created
* and VariantOutputScope.setProcessResourcesTask in TaskManager.createProcessResTask() is not
* called so when TaskManager.applyProguardConfig() calls
* BaseVariantOutputData.processResouresTask.getProguardOutputFile() it does so on a null object.
* So, by making transformClassesAndResourcesWithProguardFor(Debug|Release) depend on
* process(Debug|Release)Resources I force the task to be created
*
* More discussions here: https://issuetracker.google.com/issues/37079003
*/
tasks.all { task ->
def match = task.name =~ /^transformClassesAndResourcesWithProguardFor(.*)$/
if (match) {
task.dependsOn "process${match.group(1)}Resources"
return
}
}
// ToDo: get automatic build of .aar libraries working;
// currently, double-click on Gradle "wavvoxlibrary / Tasks / build / assemble"
}
I wanted to get the .aar file created automagically while building the signed app (.apk) without the extra step running gradle task wavvoxlibrary assemble.
What do I miss?
Thanks, jm.
I think you might have tried this, but still adding this as answer, if it does not help, will delete this answer.
Add your library module to your settings.gradle file.
include ':app', ':my-library-module'
https://developer.android.com/studio/projects/android-library#AddDependency

Running debug on older Android 4.4.2 using Android Studio

I am trying to debug on an older Android device (Samsung Tab3 4.4.2). The strange thing is I am getting an error that I do not see when running on my new devices (Android 6+).
When I hit the debug button, a Gradle build is shot off and I get the following error:
06/21 15:55:35: Launching splashActivity
The APK file C:\Users\me\Documents\src\myapp\build\outputs\apk\MyApp\debug\10-MyApp.apk does not exist on disk.
Error while Installing APK
The file is not there, but is not there when I run against the newer versions, which successfully debug. Is Android Studio doing something different due to the older version? Do I need to make some sort of Gradle adjustment?
My Gradle build appends a version to the front of the APK, could that be the problem? Here is the Gradle file (I have removed the flavors, have a ton of them):
buildscript {
ext.kotlin_version = '1.1.1'
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.myapp.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 10
versionName "1.1.10"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7 }
}
aaptOptions {
cruncherEnabled = false
}
dexOptions {
javaMaxHeapSize "4g"
jumboMode true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors.whenObjectAdded { flavor ->
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
flavorDimensions "default"
productFlavors {
MyApp {
signingConfig signingConfigs.config
directoryPath = 'myapp'
}
Flavor1 {
applicationId 'com.flavor1.flavor'
signingConfig signingConfigs.config
directoryPath = 'flavor1'
}
}
applicationVariants.all { variant ->
variant.outputs.all {
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
//add here your logic to customize the name of the apk
outputFileName = "${variant.versionCode}-${flavor}.apk"
}
variant.assemble.doLast { assemble ->
//copy the apk in another directory, add here your
//logic to customize the destination folder
copy {
from variant.outputs*.outputFile
into "C:/AndroidBuilds/MyApp.Build/${variant.productFlavors[0].directoryPath}"
}
//if you don't want to delete the file after copying it comment the line below
delete variant.outputs*.outputFile
}
}
}
I have removed dependencies and such to make it smaller, and changed names to protect the innocent.
Doh, it was the following line of code in Gradle, that was deleting the variant. Apparently it is needed to run on older versions of Android:
//if you don't want to delete the file after copying it comment the line below
// delete variant.outputs*.outputFile

Android studio 3: Could not find the AndroidManifest.xml file

after migrating to Android studio 3 I'm unable to compile as I' have folowing errors:
Error:Could not find the AndroidManifest.xml file, using generation
folder
[/home/salacr/git/Evotech/app/build/generated/source/apt/debug])
Error:Parceler: Code generation did not complete successfully. For
more details add the compiler argument -AparcelerStacktrace
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
It might be connected with usage of android anotation my app/build.gradle looks like this:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
def AAVersion = '4.3.1'
def parcelerVersion = '1.1.9'
android {
compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
applicationId "com.my.app"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
multiDexEnabled true
}
signingConfigs {
release {
storeFile file("******")
storePassword "******"
keyAlias "******"
keyPassword "******"
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
splits {
abi {
enable true // enable ABI split feature to create one APK per ABI
universalApk true //generate an additional APK that targets all the ABIs
}
}
/*
// map for the version code
project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9]
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
}
}
*/
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "org.parceler:parceler-api:$parcelerVersion"
annotationProcessor "org.parceler:parceler:$parcelerVersion"
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.android.support:multidex:1.0.2'
}
I'm unable to find the root cause for this, any suggestions?
I tried diferent graddle versions as well as using different buildToolsVersion tools, but without effect.
Any suggestions?
Thanks!
EDIT 1: I have found out that problem is here:
splits {
abi {
enable true // enable ABI split feature to create one APK per ABI
universalApk true //generate an additional APK that targets all the ABIs
}
}
Without this everything works as expected. IT seams that this config isn't compatible with androidanotations in new Android studio
EDIT 2: There is already issue in androidanotation: https://github.com/androidannotations/androidannotations/issues/2034
I had similar issue and solved my problem when I updated android annotation version from 4.4.0 to 4.5.0.
Add the following code in build.gradle file under defaultConfig
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"androidManifestFile":"$projectDir/src/main/AndroidManifest.xml".toString()
]
}
}
I solved this problem changed graddle version:
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
I had similar issue and solved my problem when I updated android annotation version from 4.4.0 to 4.6.0.

Android Studio gradle apk and run out of sync

I am using Android Studio and I have the following build.gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries = false
}
defaultConfig {
applicationId "com.domain.myapp"
minSdkVersion 17
targetSdkVersion 25
versionName "1.0"
versionCode 2
versionNameSuffix = ".alpha1"
def date = new Date().format('yyyyMMddHHmmss')
if (versionNameSuffix.equals("")) {
setProperty("archivesBaseName", "myapp.$versionName.$versionCode")
} else {
setProperty("archivesBaseName", "myapp.$versionName.$versionCode$versionNameSuffix." + date)
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField 'boolean', 'IS_DEBUG', 'false'
buildConfigField 'String', 'BUILD_DATE', '\"' + date + '\"'
buildConfigField 'String', 'VERSION_STRING', '\"' + "$versionName.$versionCode$versionNameSuffix" + '\"'
resValue "string", "BUILD_DATE", date
resValue "string", "VERSION", "$versionName.$versionCode$versionNameSuffix"
}
buildTypes {
release {
buildConfigField 'boolean', 'IS_DEBUG', 'false'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName = output.outputFile.name
newName = newName.replace("-release", "")
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
debug {
buildConfigField 'boolean', 'IS_DEBUG', 'true'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName = output.outputFile.name
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
}
productFlavors {
}
}
Basically when I Build->Rebuild Project or Build->Build APK, I generate an apk file like myapp.1.0.2.alpha1.20170224125933.apk
The last bit on the end is a time-stamp. (20170224125933 => 2017/2/24 12:59:33)
Everything works perfectly when running manually via the apk.
The issue I am having is that when I try to Run->Run myapp through Android Studio, it seems to always look for an earilier time-stamp.
For example, let's say I Rebuild Project and get the file: myapp.1.0.2.alpha1.20170224125933.apk. I make some code changes and want to Run it through Anroid Studio. When I go to Run, it rebuilds the project again (generating myapp.1.0.2.alpha1.20170224130417.apk) and deletes the previous file (myapp.1.0.2.alpha1.20170224125933.apk) as expected, but, tries to install the previous file. I then get an "error while installing apk. File myapp.1.0.2.alpha1.20170224125933.apk not found."
The only solution I've found is to drop the seconds marker (change the Date format to 'yyyyMMddHHmm'), click Tools->Android->Sync Project with Gradle Files, then immediately hit Run. This usually works unless the clock changes minutes during the few seconds this process takes.
I'm thinking I need a Gradle->Task Activitaion or something so that when I click Run, it generates a new apk, and knows to use the recently generated one.
Any ideas?

AndroidAnnotations + Android Studio - The generated null.R class cannot be found

I have setup ActiveAndroid as per the wiki instructions using latest version of AndroidStudio. I am using product Flavours. This is my gradle build file:
apply plugin: 'android'
apply plugin: 'android-apt'
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName android.defaultConfig.packageName
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
a {
packageName "com.a.a"
}
b {
packageName "com.a.b"
}
c {
packageName "com.a.c"
}
}
}
dependencies {
apt "org.androidannotations:androidannotations:3.0+"
compile "org.androidannotations:androidannotations-api:3.0+"
compile 'com.android.support:support-v4:19.0.1'
compile 'com.android.support:appcompat-v7:19.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Gradle build files but when I compile/go to debug on the device I receive two errors:
Error:: The generated null.R class cannot be found
and
Error:Execution failed for task ':ml:compileADebugJava'.
Compilation failed; see the compiler error output for details.
I have tried numerous setups for my build file but cannot for the life of me get it work. Also when I try and change my AndroidManifest from:
android:name="com.a.a.MainActivity"
to
android:name="com.a.a.MainActivity_"
it states that class cannot be found.
I am using latest version of Gradle and latest version of ActiveAndroid.
Any help would be much appreciated.
I know its late, but it might help someone.
This happens when you modify the applicationId. The script provided in example assumes that you have declared "android.defaultConfig.applicationId". In most of the cases its null, and hence it generated null.R. Either you can define the variable or change the code to following:
defaultConfig {
// Rest of Config
javaCompileOptions {
annotationProcessorOptions {
arguments = ["resourcePackageName": "<Original Package Name>"]
}
}
}
Note that original Package name should be same as the location of R in your activity.
Hope it helps!
I have just got into this same problem, solved by adding packageName to the defaultConfig:
Sample:
defaultConfig {
packageName "com.example.myapp"
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
Change com.example.myapp according to the name specified in your AndroidManifest.xml.
Hope this helps.
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.your.app"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName android.defaultConfig.applicationId
}
}
It works for me.
I was facing this problem, so I just removed this line:
resourcePackageName android.defaultConfig.packageName
and it worked.
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName android.defaultConfig.applicationId
}
In my case, It Worked.
change the applicationId==package in the manifest.
100% success.
I had the same problem. I've update the build.gralde file as follow.
For Java I've added:
android {
defaultConfig {
applicationId "<your.application.id>"
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
arguments = ["resourcePackageName": android.defaultConfig.applicationId]
//arguments = ['androidManifestFile': variant.outputs[0].processResources.manifestFile]
}
}
}
}
For Koltin I've added:
kapt {
arguments {
arg("resourcePackageName", android.defaultConfig.applicationId)
//arg("androidManifestFile", variant.outputs[0]?.processResources?.manifestFile)
}
correctErrorTypes = true // add this if you use data binding
}

Resources