带有Native C++ Library的Android Instant App无法发布到运行Android N的设备/模拟器

spa*_*mic 5 c++ android c++11 android-instant-apps android-studio-3.0

有没有办法让Android Instant App与本机C++库一起使用?

我正在尝试将Android Instant App发布到设备/模拟器,但遇到了我的本机C++库的问题.它作为可安装的应用程序发布,但在发布为Instant App时无法找到该库.

为了消除任何其他问题,我使用新项目向导在Android Studio 3.0(Canary 1 171.4010489)中启动了一个新项目,并选择了以下设置:

第一页:

  • 包括已选中的C++支持

第二页:

  • 选择手机和平板电脑
  • 已选中包含Android Instant App支持

第六页:

  • C++标准设置为'C++ 11'
  • 检查了例外支持(-fexceptions)
  • 运行时类型信息支持(-frtti)已选中

生成的项目将作为可安装的应用程序发布(显示"来自C++的Hello"屏幕),但不是即时应用程序......它会出现以下错误:无法找到库,这与我遇到的错误相同我的实际应用项目:

couldn't find "libnative-lib.so"

完整错误:

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"
     ...
Run Code Online (Sandbox Code Playgroud)

我正在粘贴下面的相关gradle文件(全部由Android Studio生成):

应用程序/的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')
}
Run Code Online (Sandbox Code Playgroud)

碱/的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'
}
Run Code Online (Sandbox Code Playgroud)

功能/的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'

}
Run Code Online (Sandbox Code Playgroud)

instantapp /的build.gradle:

apply plugin: 'com.android.instantapp'

dependencies {
    implementation project(':feature')
    implementation project(':base')
}
Run Code Online (Sandbox Code Playgroud)

更新:

我向Google提出了一个问题:

链接:Google问题跟踪器

虽然我觉得实现这一目标的工具已经可用(Gradle,CMake,NDK等)

还要感谢@Anirudh让我知道这是Android N上的一个已知问题.

发布没有C++库的Instant App是否适用于我的设备?

是的...如果我创建一个新的Android Studio项目,只有Include Android Instant App support它发布到我的三星Galaxy 7S并显示'Hello World!' 屏幕.

发布已签名的APK是否有效?

生成一个签名的APK工作,并在检查时本机C++库与feature-debug.apk但不捆绑base-debug.apk.这是我期望的gradle配置,但没有解释为什么它不会发布到设备/模拟器.

我还没有尝试过加载这些APK ...但是我怀疑甚至可能因为从未安装过即时应用程序...例如:你如何在加载它之后启动它(点击一个网址?)

向两个APK添加C++库是否有效?

我已经尝试将externalNativeBuildgradle属性添加到文件base/build.gradlefeature/build.gradle文件中,但仍然会出现相同的错误.我通过检查生成签名的APK feature-debug.apkbase-debug.apk生成签名的APK后验证了本地C++库随后包含在两个APK中.

修改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'
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*udh 3

发布签名的 APK 有效吗?

Android Studio 3.0 预览版生成签名 APK 功能目前存在一个错误,即最终 zip 不包含所有功能 apk。在每个功能模块的 gradle 文件中使用 Gradle SigningConfig 对功能 apk 进行签名

将 C++ 库添加到两个 APK 是否有效?

不需要。添加到基本功能 apk 应该就足够了

实际崩溃是 Android M/N 上的 Android Instant Apps 的 NDK 支持的已知问题。该应用程序可在 Android O 模拟器上运行