Native code debug is not working in Android Studio 3 - android

I've tried all the ways found on StackOverflow and still facing the issue.
I've created a demo Android project with native support, added a library to it and moved all native code into the library.
Now I'm unable to stop on breakpoints in native code, native debugger became active only after a SEGFAULT crash.
I've added defaultPublishConfig "debug" into mylibrary build.gradle and debuggable true to app build.fradle. There was enough for native debugging earlier. But it is not working since Android Studio upgrade.
Here are the full build.gradle files
app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.raistlin.myapplication"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation project(path: ':mylibrary')
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'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
mylibrary
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
defaultPublishConfig "debug"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
cppFlags "-fexceptions", "-std=c++11", "-DJSONCPP_NO_LOCALE_SUPPORT"
version "3.10.2"
}
}
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

First, check debug variant is selected for library.
As your build.gradle has a setting, path "src/main/cpp/CMakeLists.txt", it won't build I think. So set targets, rebuild and check again.
If breakpoints doesn't work after build, old garbage may be left in the build cache and causes problems. Open project directory in explorer and delete build cache (.externalNativeBuild folder) manually and build the project again. I also delete build folder, as it contains .so files in intermediate directory, but it's optional.
Android Studio does not clean libraries in the test device. They are overwritten basically, but clear them manually depending on needs. Files are in /data/app/(package name)/lib/(cpu arch.)/.
NB: Synchronize menu of device file explorer does not synchronize properly under lib or (cpu arch.) directory. To synchronize, select /data or /data/app and choose Synchronize.
NB.1 If targets is omitted, Android Studio seems to build no targets. The built output is in (project)/app/build/intermediates/cmake/(flavor)/obj/(cpu architecture). If it seems to work without any targets, check files on device. They confuse test results.
NB.2 debuggable true is for release build to enable debugging. No need to set it for debug build, as debuggable flag is set as default.
NB.3 Seems version dependent but Gradle in Android Studio does not clean .externalNativeBuild tree properly even if clean or rebuild is called, and confuses native code build configs. It was around AS3.0, as I remember.
NB.4 My environment is
Android Stuidio 3.2.1
classpath 'com.android.tools.build:gradle:3.2.1'
gradle-4.7-all
CMake: default(3.6.4111459)
I know there are newer versions for Android Studio, Gradle and CMake but they are buggy, so I chose current environment. As far as I've experienced, Android Studio 3.3, gradle:3.3.0, gradle-4.10.1-all have severe bug in VCS(git). Wrong file contents are shown in editor and build fails, sometimes. Setting CMake version to 3.10.x (3.10.2 for me) also seems buggy.
Here's a copy from my project as a sample, partially modified from original one but may work.
I've checked breakpoints in a library work in Android Studio 3.2.1.
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "0.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'proguard-rules.pro'
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_STL=c++_static"
targets "sample"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Update
targets is mentioned here Guide - Link Gradle to your native library - Specify optional configurations.

Related

Gradle build sync failed. Basic functionality (e.g. Editing Debugging) will not work properly

Error screenShot
`plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.helloworld"
minSdk 16
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
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}`
The Above code is my build. Gradle
The Version of Android Studio --> Android Studio Bumblebee | 2021.1.1 Patch 2 for Windows 64-bit
I am Facing this Gradle build error since morning I had tried all the things Including once from Previous Stack Overflow Users who said still I am stuck Please Help every Advice will we consider as a blessing please Help
I had tried everything from the following posts but sadly Nothing works:
Android Studio 3.0.1 Error: Gradle project sync failed. Basic functionality (e.g. editing, debugging) will not work properly
https://www.youtube.com/watch?v=U8YXxZXlcMY
https://www.youtube.com/watch?v=1_vVIi_AOew
https://www.youtube.com/watch?v=h90Y_KMk1Z4
Go to File -> Project Structure -> Project
Now make Android Gradle Plugin Version as 7.1.2
Gradle Version as 7.2
Update any other dependencies you are using also do Invalidate Caches and Restart.

views don't shows on design mode in Android Studio

Views stopped showing on Design Mode in Android Studio out of the blue (including the "hello world" textView). I haven't updated anything or changed anything.
I tried the following solutions:
(changing the theme) This view is not constrained android studio. I can not see the button even if I use Infer Constraints
inferring constraints.
(can't find the link) but the answer marked as correct said my SDK is corrupt -> updating it didn't work.
I'm suspecting Gradle is the culprit somehow, so here's the app's build script (a brand new project):
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.cc.take6"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
my android Studio is version: 3.6.3.
Any ideas? thank you in advance.
Make sure you are using the latest stable version of Android Studio and all related tools
Update your project to use latest version of Gradle and everything
File -> Invalidate Caches and Restart
Build -> Clean Project
Build -> Rebuild Project
Run
I fixed the problem by changing the version of contraintLayout implementation that I was using. I added to the file build.gradle(:app):
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

Untiy android plugin class not found exception

I'm writing an android plugin for unity to be able to check if notifications are enabled for the game. I have one java class with a method for checking if notifications are enabled. When i build the plugin and then the .apk with unity everything works fine. But after installation, when calling the mehthod i get the following exception:
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/NotificationManagerCompat;
The Java class
package com.example.plugin;
import android.app.Activity;
import androidx.core.app.NotificationManagerCompat;
public class NotificationPlugin {
public static boolean areNotificationEnabled(Activity unityActivity) {
return NotificationManagerCompat.from(unityActivity).areNotificationsEnabled();
}
}
buld.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
gradle.properties
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
Haven't found a solution anywhere else yet. Thank in advance. It's probably a super simple fix that i miss.
Update
Custom gradle.properties in unity set with android.enableJetifier and android.useAndroidX set to true
Jetifier was enabled in unity
Androidx.Core libary was added to the dependencies of the gradle.build file of the plugin
None of the above solved the issue
Solution
As Hamid Yusifli suggested in his answer a custom gradle build template needs to be enabled (Project Settings>Player>Publishing Settings>Custom Main Gralde Template) and the dependencies for the libary need to be added (implementation 'androidx.core:core:1.5.0' in my case). This solved the issue.
So, seems like in your case unity ignores your library gradle dependencies,
there are many different reasons why this could happen. To force unity to include missing dependencies you must provide a custom Gradle build template and add your dependencies in that file.

Android Instant App with Native C++ Library not publishing to device/simulator running Android N

Is there a way to get an Android Instant App working with a native C++ library?
I'm attempting to publish an Android Instant App to a device/simulator, but ran into problems with my native C++ library. It publishes fine as an installable app, but fails to find the library when published as an Instant App.
To eliminate any other issues, I started a new project in Android Studio 3.0 (Canary 1 171.4010489) with the new project wizard and selected the following settings:
First Page:
Include C++ support checked
Second Page:
Phone and Tablet selected
Include Android Instant App support checked
Sixth Page:
C++ Standard set to 'C++11'
Exceptions Support (-fexceptions) checked
Runtime Type Information Support (-frtti) checked
The resulting project will publish as an installable app (showing the 'Hello from C++' screen), but not an instant app... it gives the following error that it can't find the library, which is the same error I get in my actual app's project:
couldn't find "libnative-lib.so"
Full error:
05-24 17:48:30.316 7519-7519/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mycompany.instantapp, PID: 7519
java.lang.UnsatisfiedLinkError: byc[DexPathList[[zip file "/data/user/0/com.google.android.instantapps.supervisor/files/atom-cache/com.mycompany.instantapp/atom-download--feature-1495662507463/feature.jar"],nativeLibraryDirectories=[/data/user/0/com.google.android.instantapps.supervisor/files/native-lib/com.mycompany.instantapp, /system/lib, /vendor/lib]]] couldn't find "libnative-lib.so"
...
I'm pasting the relevant gradle files below (all generated by Android Studio):
app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
defaultConfig {
applicationId "com.mycompany.instantapp"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation project(':feature')
implementation project(':base')
}
base/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
baseFeature true
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
feature project(':feature')
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
feature/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation project(':base')
testCompile 'junit:junit:4.12'
}
instantapp/build.gradle:
apply plugin: 'com.android.instantapp'
dependencies {
implementation project(':feature')
implementation project(':base')
}
Updates:
I've filed an issue with Google:
Link: Google Issue Tracker
Though I feel like the tools to make this happen are already available (Gradle, CMake, NDK, etc)
Also thanks #Anirudh for letting me know that this is a known issue on Android N.
Does publishing an Instant App with no C++ library work on my device?
Yes... if I create a new Android Studio project with only Include Android Instant App support it publishes to my Samsung Galaxy 7S and shows the 'Hello World!' screen.
Does publishing a signed APK work?
Generating a signed APK works, and upon inspection the native C++ library is bundled with the feature-debug.apk but not the base-debug.apk. This is what I would expect given the gradle configuration, but doesn't explain why it won't publish to a device/simulator.
I haven't tried sideloading these APKs... but I'm skeptical if that is even possible given that the Instant App is never installed... ex: how would you even launch it after sideloading it (click a url?)
Does adding the C++ library to both APKs work?
I've tried adding the externalNativeBuild gradle properties to both the base/build.gradle and the feature/build.gradle files, but the same error still occurs. I verified that the native C++ library is then included in both APKs by inspecting both the feature-debug.apk and the base-debug.apk after generating a signed APK.
modified base/build.gradle:
apply plugin: 'com.android.feature'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0 rc2"
baseFeature true
defaultConfig {
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "../feature/CMakeLists.txt"
}
}
}
dependencies {
feature project(':feature')
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
Does publishing a signed APK work?
Android Studio 3.0 preview Generate Signed APK feature has a bug currently where the final zip doesn't include all feature apks. Use Gradle SigningConfig in each feature module's gradle file to sign your feature apks
Does adding the C++ library to both APKs work?
Not required. Adding to base feature apk should be enough
The actual crash is known issue with NDK support for Android Instant Apps on Android M/N. The app works on Android O emulator

Android Studio Build Failed - Compiling local project errors

A few months ago, I decided to take some custom methods and classes that I was using in more than one project and put them in their own project called "FarmSoftLibraries" that all of my projects could reference. This allowed me to make changes or additions once. It has been working very well up until yesterday (8/23/2015) when i started getting an odd error on a new method that Gradle could not find. However Android Studio had no problem finding this method. See: https://stackoverflow.com/questions/32167734/cannot-find-symbol-method-for-custom-static-class-method. However older, non-new methods continued to work.
Today the entire build system stopped working and I'm getting hundreds of Gradle errors saying that packages and methods in FarmSoftLibraries do not exist. Still, Android Studio has no problem locating these.
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:12: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.FarmUtils;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:14: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.Logg;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\utils\KeyValueDB.java:6: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.CsvUtil;
etc. etc. etc...
I want to stress that I've changed NOTHING in the gradle files. I have been upgrading Android Studio on the Canary path - I'm on 1.4 Preview 3 now. When this issue began I was on 1.4 Preview 2. I have no idea if this is related to this version of Studio.
My gradle scripts:
project settings.gradle:
include ':app'
include ':farmsoftlibs'
project(':farmsoftlibs').projectDir = new File('../FarmSoftLibraries/app')
app build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.farmsoft.spellingtutor"
minSdkVersion 15
targetSdkVersion 22
versionCode 28
versionName "1.15"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
applicationIdSuffix '.debug'
versionNameSuffix '.debug'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services-gcm:7.8.0'
compile project(':farmsoftlibs')
}
farmsoftlibs build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/gson-2.3.1.jar')
compile 'com.google.android.gms:play-services-vision:7.8.0'
}
I do want to admit I pretty much am ignorant about gradle - I follow the instructions for how to set it up in Android Studio, but I really don't know what I'm doing and have gotten lost in the Gradle documentation I tried to read. So this is incredibly frustrating and more so knowing that this is probably due to some stupid mistake I've made out of ignorance.
EDIT:
This seems to have something to do with ProGuard - if I change the library's release settings to say minifyEnabled false, then the error goes away... for now. I'd like to know why.

Categories

Resources