Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround permission issue on Android API30 emulators when pulling test results #64744

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public override string TestsResultsFinalPath
{
get
{
string? publicDir = Environment.GetEnvironmentVariable("DOCSDIR");
if (string.IsNullOrEmpty(publicDir))
throw new ArgumentException("DOCSDIR should not be empty");
string? testResultsDir = Environment.GetEnvironmentVariable("TEST_RESULTS_DIR");
if (string.IsNullOrEmpty(testResultsDir))
throw new ArgumentException("TEST_RESULTS_DIR should not be empty");

return Path.Combine(publicDir, "testResults.xml");
return Path.Combine(testResultsDir, "testResults.xml");
}
}
}
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/Templates/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<uses-permission a:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application a:label="%PackageName%"
a:largeHeap="true">
<activity a:name="net.dot.MainActivity">
<activity a:name="net.dot.MainActivity" a:exported="true">
<intent-filter>
<category a:name="android.intent.category.LAUNCHER"/>
<action a:name="android.intent.action.MAIN"/>
Expand Down
25 changes: 13 additions & 12 deletions src/tasks/AndroidAppBuilder/Templates/MonoRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class MonoRunner extends Instrumentation
System.loadLibrary("monodroid");
}

static String testResultsDir;
static String entryPointLibName = "%EntryPointLibName%";
static Bundle result = new Bundle();

Expand Down Expand Up @@ -68,24 +69,24 @@ public void onCreate(Bundle arguments) {
start();
}

private static String getDocsDir(Context ctx) {
File docsPath = ctx.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
if (docsPath == null) {
docsPath = ctx.getCacheDir();
}
return docsPath.getAbsolutePath();
}

public static int initialize(String entryPointLibName, String[] args, Context context) {
String filesDir = context.getFilesDir().getAbsolutePath();
String cacheDir = context.getCacheDir().getAbsolutePath();
String docsDir = getDocsDir(context);

File docsPath = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
Copy link
Member

@premun premun Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the value of Environment.DIRECTORY_DOCUMENTS?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am asking because before we were setting env var DOCSDIR and I don't understand whether this is the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the enum value? It's the string Documents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so it's something from Android

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's unrelated to the env var we set.


// on Android API 30 there are "adb pull" permission issues with getExternalFilesDir() paths on emulators, see https://github.com/dotnet/xharness/issues/385
if (docsPath == null || android.os.Build.VERSION.SDK_INT == 30) {
testResultsDir = context.getCacheDir().getAbsolutePath();
} else {
testResultsDir = docsPath.getAbsolutePath();
}

// unzip libs and test files to filesDir
unzipAssets(context, filesDir, "assets.zip");

Log.i("DOTNET", "MonoRunner initialize,, entryPointLibName=" + entryPointLibName);
return initRuntime(filesDir, cacheDir, docsDir, entryPointLibName, args);
return initRuntime(filesDir, cacheDir, testResultsDir, entryPointLibName, args);
}

@Override
Expand All @@ -103,7 +104,7 @@ public void onStart() {
result.putInt("return-code", retcode);

// Xharness cli expects "test-results-path" with test results
File testResults = new File(getDocsDir(getContext()) + "/testResults.xml");
File testResults = new File(testResultsDir + "/testResults.xml");
if (testResults.exists()) {
Log.i("DOTNET", "MonoRunner finished, test-results-path=" + testResults.getAbsolutePath());
result.putString("test-results-path", testResults.getAbsolutePath());
Expand Down Expand Up @@ -146,7 +147,7 @@ static void unzipAssets(Context context, String toPath, String zipName) {
}
}

static native int initRuntime(String libsDir, String cacheDir, String docsDir, String entryPointLibName, String[] args);
static native int initRuntime(String libsDir, String cacheDir, String testResultsDir, String entryPointLibName, String[] args);

static native int setEnv(String key, String value);
}
8 changes: 4 additions & 4 deletions src/tasks/AndroidAppBuilder/Templates/monodroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,23 @@ Java_net_dot_MonoRunner_setEnv (JNIEnv* env, jobject thiz, jstring j_key, jstrin
}

int
Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_dir, jstring j_cache_dir, jstring j_docs_dir, jstring j_entryPointLibName, jobjectArray j_args)
Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_dir, jstring j_cache_dir, jstring j_testresults_dir, jstring j_entryPointLibName, jobjectArray j_args)
{
char file_dir[2048];
char cache_dir[2048];
char docs_dir[2048];
char testresults_dir[2048];
char entryPointLibName[2048];
strncpy_str (env, file_dir, j_files_dir, sizeof(file_dir));
strncpy_str (env, cache_dir, j_cache_dir, sizeof(cache_dir));
strncpy_str (env, docs_dir, j_docs_dir, sizeof(docs_dir));
strncpy_str (env, testresults_dir, j_testresults_dir, sizeof(testresults_dir));
strncpy_str (env, entryPointLibName, j_entryPointLibName, sizeof(entryPointLibName));

bundle_path = file_dir;
executable = entryPointLibName;

setenv ("HOME", bundle_path, true);
setenv ("TMPDIR", cache_dir, true);
setenv ("DOCSDIR", docs_dir, true);
setenv ("TEST_RESULTS_DIR", testresults_dir, true);

int args_len = (*env)->GetArrayLength(env, j_args);
int managed_argc = args_len + 1;
Expand Down