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?
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?
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"));
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!
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...)
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.
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)