2

is it possible android app make an apk file of itself in runtime?

I want my app be able to send itself via bluetooth how can I do it?

Navid
  • 901
  • 9
  • 20
  • Do you mean beam your app to another device and have it automatically installed? – jaesanx Aug 29 '13 at 21:51
  • 1
    You could build your application and put it in the assets folder. This would mean you'd have to do it for every update, and would double apk. size. I can't help you on the whole sending it part. – MattMatt Aug 29 '13 at 21:55
  • 1
    but with this solution, app can send it just one time! the sent app does not have itself in its asset. – Navid Aug 31 '13 at 05:26
  • @jaesanx: I mean just send apk file, is it possible to make it automatically install? – Navid Aug 31 '13 at 05:46

5 Answers5

10

the best way is this:

// Get current ApplicationInfo to find .apk path
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;

Intent intent = new Intent(Intent.ACTION_SEND);

// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");

// Only use Bluetooth to send .apk
intent.setPackage("com.android.bluetooth");

// Append file and send Intent
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(intent, "Share app"));
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44
2

Finally I found the answer! There is no need to make the apk of the application because android saves the apk file in memory, you can access the apk file of the app using PackageManager, it will give the location where the apk is saved and then you can send it via bluetooth!

Navid
  • 901
  • 9
  • 20
  • this is not correct answer , accept @Erfan egtfi answer – Adnan Abdollah Zaki Feb 25 '16 at 08:56
  • @adnan9011: if u care you can see Erfan`s answer is the code of my answer! and if u care more you can see i have written the answer about 2 years ago!!! the Erfan`s answer is correct, and I appreciate him. – Navid Feb 26 '16 at 13:15
1

The best option is to put the .apk file in assets and send it from there.

To avoid problems with updates you can tell your app to download the .apk from some server before sending it. (but then why do this, the other user can download it from the market? I am sorry but maybe if you explained the reason behind this idea, we could give you a better explanation for your options? )

Your apk file is made from your project in eclipse for ex. so actually building an .apk from those files in your app would be absurd and probably impossible (you would have to code the process of compile, build...)

JanBo
  • 2,925
  • 3
  • 23
  • 32
  • the main reason is to make users be able to publish the app, without needs to connect to internet. – Navid Aug 31 '13 at 05:27
  • This is not a good option: The apk should be in the assets, then the apk should be built, This is a recursive build system. If you build an apk without itself in the assets folder, then put it in assets and rebuild, the resulting app has the ability to share the copy of itself that lacks sharing ability. – Alireza Jul 15 '15 at 09:57
1

Your best bet is to simply redirect to the Google Play Store, or your own hosted version of the apk.

You can't really bundle the app in itself, because then the bundled apk that gets shared doesn't have a bundled apk to share with the next person, so the feature breaks after one share.

If you really want to have this feature, you'd have to do something like download the apk (from your own server) to the shared storage on the first launch of the app. After it downloads, you can enable the "send via bluetooth" feature.

Krylez
  • 17,414
  • 4
  • 32
  • 41
0

You can use PackageManager to get the path to your app's APK, then use that in a ACTION_SEND Intent to send the file via Bluetooth.

Check here for an example: Bluetooth File transfer on Android(even restricted types)

Community
  • 1
  • 1