Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
}
like image 651
stephenspann Avatar asked Oct 17 '22 11:10

stephenspann


1 Answers

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

like image 116
Anirudh Avatar answered Oct 29 '22 18:10

Anirudh