質問

I need instrumentTest build to have it's own assets.

Source set structure

├── build.gradle
├── libs
└── src
    ├── instrumentTest
    │   ├── assets
    │   └── java
    └── main
        ├── AndroidManifest.xml
        ├── assets
        ├── java
        └── res

What have I tried

apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.2'

    // There were no soureSets in my project in the first place
    // I have added sourceSets for instrumentTest, but that made no difference
    sourceSets {
        instrumentTest {
            java.srcDir 'src/instrumentTest/java'
            assets.srcDir 'src/instrumentTest/assets'
        }
    }

    buildTypes {
        debug {
            runProguard false
        }

        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

Running

./gradlew connectedCheck

Fails with

java.io.FileNotFoundException: busybox
at android.content.res.AssetManager.openAsset(Native Method)

At line

final InputStream is = context.getAssets().open("busybox");
役に立ちましたか?

解決

instrumentTest assets are packaged as part of the instrumentation test, not the application being tested - you are probably using the target application's context instead of the instrumentation context, which is why it can't find the asset.

You can get the instrumentation context using getInstrumentation().getContext() in any InstrumentationTestCase.

You should be able to read your instrumentTest assets by obtaining an AssetManager with getInstrumentation().getContext().getAssets().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top