Share source code android

Sharing Content between Android apps

Sharing is caring, as they say, but sharing on Android means something perhaps slightly different. ‘Sharing’ is really shorthand for sending content such as text, formatted text, files, or images between apps.

So if ‘sharing’ == sending content, it makes slightly more sense that it is implemented using ACTION_SEND (or ACTION_SEND_MULTIPLE) Intents and its dozen extras.

While that approach is perfectly valid, I prefer to use ShareCompat, a set of classes in the v4 Support Library designed to make it easy to build intents for sharing content.

Sharing text

Sharing plain text is, as you might imagine, a good place to start. In fact, there’s not a whole lot to it:

ShareCompat.IntentBuilder uses a fluent API where you can chain together multiple method calls, using only the ones you need. For sharing, one of the most important parts is picking the right mime type — this is how apps filter what type of content they can receive. By using text/plain, we signify that our Intent will only contain plain text. Then, of course, setText() is how we actually add the CharSequence to the Intent to send. And while you can certainly send styled text using setText(), there’s no guarantee that the receiving app will honor that styling, so you should ensure that the text is legible with or without styling.

You’ll note we then use resolveActivity() before calling startActivity(). As mentioned in Protecting Implicit Intents with Runtime Checks, this is critical to prevent an ActivityNotFoundException when there is no Activity available to handle the mime type you have selected. While probably not as much of a concern with text/plain, it may be much more common with other types.

Note: when you use startActivity(shareIntent), that respects any default apps the user has set (i.e., if they’ve previously selected sharing all “text/plain” items to a certain app). If you’d like to instead always show a disambiguation chooser, use the intent generated from IntentBuilder.createChooserIntent() as explained in the ACTION_CHOOSER documentation.

Sharing HTML text

Some apps, most notably email clients, also support formatting with HTML. The changes, compared to plain text, are fairly minor:

The differences here are that we use of setHtmlText() in place of setText() and a mime type of text/html replacing text/plain. Here ShareCompat actually does a little bit extra: setHtmlText() also uses Html.fromHtml() to create a fallback formatted text to pass along to the receiving app if you haven’t previously called setText() yourself.

Given that many of the apps that can receive HTML text are email clients, there’s a number of helper methods to set the subject, to:, cc:, and bcc: email addresses as well — consider adding at least a subject to any share intent for best compatibility with email apps.

Of course, you’ll still want to call resolveActivity() just as before — nothing changes there.

Receiving text

While the focus so far has been on the sending side, it is helpful to know exactly what is happening on the other side (if not just to build a simple receiving app to install on your emulator for testing purposes). Receiving Activities add an intent filter to the Activity:

The action is obviously the more critical part — without that there’s nothing that would denote this as an ACTION_SEND (the action behind sharing). The mime type, same as with our sending code, is also present here. What isn’t as obvious are the two categories. From the element documentation:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

So CATEGORY_DEFAULT is required for our use case. Then, CATEGORY_BROWSABLE allows web pages to natively share into apps without any extra effort required on the receiving side.

Читайте также:  Телеграм папка для сохранения файлов андроид

And to actually extract the information from the Intent, the useful ShareCompat.IntentReader can be used:

Similar to IntentBuilder, IntentReader is just a simple wrapper that make it easy to extract information.

Sharing files and images

While sending and receiving text is straightforward enough (create text, include it in Intent), sending files (and particularly images — the most common type by far) has an additional wrinkle: file permissions.

The simplest code you might try might look like

And that almost works — the tricky part is in getting a Uri to the File that other apps can actually read, particularly when it comes to Android 6.0 Marshmallow devices and runtime permissions (which include the now dangerous READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions).

My plea: don’t use Uri.fromFile(). It forces receiving apps to have the READ_EXTERNAL_STORAGE permission, won’t work at all if you are trying to share across users, and prior to KitKat, would require your app to have WRITE_EXTERNAL_STORAGE. And really important share targets, like Gmail, won’t have the READ_EXTERNAL_STORAGE permission — so it’ll just fail.

Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.

Once you have it set up, our code becomes:

Using FileProvider.getUriForFile(), you’ll get a Uri actually suitable for sending to another app — they’ll be able to read it without any storage permissions — instead, you are specifically granting them read permission with FLAG_GRANT_READ_URI_PERMISSION.

Note: we don’t call setType() anywhere when building our ShareCompat (even though in the video I did set it). As explained in the setDataAndType() Javadoc, the type is automatically inferred from the data URI using getContentResolver().getType(uriToImage). Since FileProvider returns the correct mime type automatically, we don’t need to manually specify a mime type at all.

If you’re interested in learning more about avoiding the storage permission, consider watching my Forget the Storage Permission talk or at least go through the slides, which covers this topic in depth at 14:55 (slide 11).

Receiving files

Receiving files isn’t too different from text because you’re still going to use ShareCompat.IntentReader. For example, to make a Bitmap out of an incoming file, it would look like:

Of course, you’re free to do whatever you want with the InputStream — watch out for images that are so large you hit an OutOfMemoryException. All of the things you know about loading Bitmaps still apply.

The Support Library is your friend

With both ShareCompat (and its IntentBuilder and IntentReader) and FileProvider in the v4 Support Library, you’ll be able to include sharing text, HTML text, and files in your app with the best practices by default.

Источник

Open Source Your Android Code — The Complete Guide

ADA | Adam Deconstructs Android

Aug 29, 2017 · 8 min read

9/17/17 Update: JitPack.io appears to be a fast and easy alternative to open source Android code vs. the method below by integrating directly with GitHub. I have not tested JitPack so please share your feedback in the comments if you’ve implemented it.

Читайте также:  Диктофоны для андроид с активацией голосом

You’ve spent hours building cool shit. What’s the next step? By open sourcing your work you’ll (hopefully) provide valuable code to the Android community, receive constructive feedback, and collaborate on building something better than what you originally had.

The current state of open sourcing for Android is unintuitive, involving integrating multiple services, waiting for manual approvals, and before this post, spending hours Googling obscure steps. That’s why I created a beginning-to-end guide to expedite the process.

The more open sourced code, the better.

Implementation

I will walk through each step of how I open sourced a CustomRippleView library for Android.

Place code inside an Android Archive Library (AAR) — Step 1 of 6

Besides open sourcing, AARs are useful when building multiple apps or versions with the same components.

About

  • Structurally the same as an Android app module
  • Includes source code, resource files, manifest (unlike JAR)
  • Compiles into Android Archive (AAR) rather than into APK
  • Post to some maven repository where devs can pull it as a dependency through Gradle (can also convert an app to a module)
  • Code Overlap — The app module will take precedence over a library if a resource ID is defined in both, library defined first will take precedence between libraries.

Implementation

If you’re creating a standalone library outside an existing app you’ll want to both create a new project to host the library module as well as test the library module in an existing app.

1. Build the open sourced library module in an existing project so that you can test the code as you go.

a) Create library module

Click the plus or File > New > Module > Android Library > provide unique Library Module Name ( customrippleview)

b) Ensure local library module shows in project and compile local library in the app module.

build.gradle ( app module)

Add tools:replace=”android:name” to the app module’s Manifest file.

2. Create a new Android project to host the open source code by itself so that it can be uploaded to bintray

Create the default app module with app following the name ( customrippleviewapp) to differentiate the app module name from the open source library module we’ll create in the next step.

  • Application name: CustomRippleViewApp
  • Company domain: com.ebay.customrippleviewapp ( needs to be a domain you own in order to get approved for open sourcing)
  • Package name: com.ebay.customrippleviewapp

3. Add your library module (refer to step 1A above)

4. Place the open source code inside new library module created

5. Remove original app module

Right-click on app module > Open Module Settings > remove original app module.

6. Choose resources to make public (Optional)

All resources default to public: By declaring at least one resource public it makes the rest private

res > values > public.xml

Publish library publicly on GitHub with licensing and Docs — Step 2 of 6

Apache License 2.0 is one of the most popular, similar to the MIT License, but provides grant of patent rights from contributors to users. Apache 2.0 is commonly found in Android, Apache, and Swift.

Make sure library module ( customrippleview/) and build.gradle are not in the ignore list list and edit .gitignore to only contain library module files added.

Bintray and Sonatype Setup — Step 3 of 6

You only need to go through this painful steps once to setup your bintray account. Praise the lord! As this isn’t difficult, but the most annoying step.

Bintray Implementation

2. Create new repository

a) Add New Repository → Type: Maven → Default Licenses: Apache 2.0

b) Use lowercase naming convention: customrippleview

3. Enable auto signing

Enter Repository → Edit → General Settings → select GPG sign uploaded files automatically

a) Generate keys (Only done once for bintray account)

In terminal for project:

Fill in Real name, Email address, and passphrase. If command does not work, run following command to install gpg and retry the command above.

Читайте также:  Магнитные зарядки для андроид что это

View keys created

Upload the public key to keyservers. Call the following command and replace PUBLIC_KEY_ID with value after 2048 in the pub line.

Export both public and private key.

Enter your passphrase when prompted for private key.

Copy and paste public and private keys into bintray: Under profile Edit > GPG Signing. Make sure to copy and paste from beginning and end tags or else bintray will not accept the keys.

Sonatype Implementation

This step requires filling a Jira ticket. If you thought you could escape Jira in your free coding time, you’re mistaken. It’s not too bad, as both times I’ve submitted a ticket they’ve approved it within the same day.

3. Provide bintray your Sonatype OSS username

In your bintray profile Edit > Accounts > Sonatype OSS User: _____________

Prepare Project for Upload — Step 4 of 6

Prepare Library Module With Bintray

1. Add Jcenter and Maven dependency

Add to project’s build.gradle (not app or library build.gradle)

2. Define your bintray username, api key, and GPG Passphrase.

This info should be secure, which is why we’re adding it to local.properties which should not be tracked in GitHub as it is commonly ignored at the start of an Android project in the .gitignore file.

3. Add repository information and build scripts

Update library’s build.gradle with repository information and add scripts for building library files and uploading the built files to bintray.

4. If using Kotlin in your code, disable Javadocs in library’s build.gradle

Upload to jcenter— Step 5 of 6

Why is jcenter better than maven central?

  • Delivers library through CDN → faster loading
  • Largest Java Repository on earth
  • “Friendly” UI (perhaps in comparison)

Implementation

1. Upload to bintray/jcenter (Once Sonatype Open Source Project Repository Hosting request is approved)

Expected Result: BUILD SUCCESSFUL

Expected Result: BUILD SUCCESSFUL

I kept getting the BUILD FAILED response when attempting to upload. After many hours cursing at my terminal I realized even with this message, the package was being uploaded, so check the bintray package UI.

2. Sync to Jcenter for easy one line implementation in Android

3 hrs — How is this not automated too?!

a) Under the uploaded package settings select Add to JCenter

b) Select Host my snapshot…, fill in group id for package, and Select Send

Once approved, you’ll receive an email.

In the meantime you can check by searching on bintray which will also show when your package is hosted.

3. Maintaining library

Linking to jcenter only needs to be done once. Moving forward, any package changes (updates, deletes), will be reflected in jcenter 2–3 min later.

  • Updates: Change the libraryVersion in library module and re-upload using Step 5, part 1.
  • Deleting: Remove each version from bintray before removing the entire package.

Use In Project — Step 6 of 6

Declare the library in gradle and call the desired files.

build.gradle ( app module)

  • Group_Id — com.ebay.customrippleview(package name followed by group name)
  • Artifact_Id: customrippleview
  • Version: 1.0

Resources

  • JitPack.io — JitPack is an Android specific open sourcing solution that appears to work seamlessly with GitHub repositories. For a premium, JitPack also provides private library hosting. I haven’t given it a try, but thanks to Rakshak and Vikarti for calling it out in the comments below!
  • The Cheese Factory Blog — How to distribute your own Android library through jCenter and Maven Central from Android Studio. BIG thanks! There were details that changed since this was published in 2015, but it was a great place to start.
  • Android Studio — Create an Android Library
  • bintray
  • sonatype
  • sonatype — request to host new open source project ( only works when logged in to sonatype)
  • bintray documentation — Including your Package in JCenter
  • CustomRippleView GitHub sample and JCenter hosting

I’m Adam Hurwitz — hit the clapping hands icon and check out the rest of my writing if you enjoyed the above | Thanks!

Источник

Оцените статью